题干
For this question, please set this context (In exam, diff cluster name)
kubectl config use-context kubernetes-admin@kubernetes
Create a Kubernetes Pod configuration to facilitate real-time monitoring of a log file. Specifically, you need to set up a Pod named alpine-pod-pod that runs an Alpine Linux container.
Requirements:
- Name the Pod alpine-pod-pod .
- Use alpine:latest image
- Configure the container to execute the tail -f /config/log.txt command using /bin/sh to continuously monitor and display the contents of a log file.
- Set up a volume named config-volume that maps to a ConfigMap named log-configmap , this log-configmap already available.
- Ensure the Pod has a restart policy of Never .
创建一个Kubernetes Pod配置,以便于实时监控日志文件。具体来说,您需要设置一个名为alpine-pod-pod
的Pod,它运行一个Alpine Linux容器。
要求:
- 将Pod命名为alpine-pod-pod。
- 使用alpine:latest映像
- 配置容器,使用/bin/sh执行尾部-f /config/log.txt命令,以持续监控和显示日志文件的内容。
- 设置一个名为config-volume的卷,该卷映射到名为
log-configmap
的ConfigMap,该log-configmap已经可用。 - 确保Pod的重启策略为Never 。
解题思路
- 切换K8S集群环境
kubectl config use-context kubernetes-admin@kubernetes
- 按照要求编写Pod的资源清单,如下
apiVersion: v1 kind: Pod metadata: name: alpine-pod-pod spec: containers: - name: alpine-container image: alpine:latest command: ["/bin/sh", "-c"] args: ["tail -f /config/log.txt"] volumeMounts: - name: config-volume mountPath: /config restartPolicy: Never volumes: - name: config-volume configMap: name: log-configmap
configMap不需要创建已经存在,直接使用即可
- 提交资源清单
controlplane $ k apply -f alpine-pod-pod.yaml pod/alpine-pod-pod created
- 验证结果
controlplane $ k get pod NAME READY STATUS RESTARTS AGE alpine-pod-pod 1/1 Running 0 3s controlplane $ k logs alpine-pod-pod <LOG DATA>