1.题目
Create a pod name log, container name log-pro use image busybox, output the important information at /log/data/output.log. Then another container name log-cus use image busybox, load the output.log at /log/data/output.log and print it. Note, this log file only can be share within the pod.
2.解析
本题目考测pod概念和volume,题意是创建具有两个容器的pod,其中一个容器向/log/data/output.log写入一些信息,另外一个容器则要把该log文件加载到应用中并输出。需要注意的是,题目有说到这log文件仅允许在当前pod中使用,因此我们用emptyDir来解决问题。题目没有提及namespace,则默认选择default。
3.答案
首先通过run命令输出yaml文件,再在其基础上修改
kubectl run log --image=busybox --dry-run=client -oyaml > log-pod.yaml
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: log
name: log
spec:
containers:
- image: busybox
name: log
resources: {}
dnsPolicy: ClusterFirst
restartPolicy: Always
status: {}
根据题目修改yaml
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: log
name: log
spec:
containers:
- image: busybox
name: log-pro
resources: {}
command: ["sh","-c","echo important information >> /log/data/output.log;sleep 1d"]
volumeMounts:
- name: data-log
mountPath: /log/data
- image: busybox
name: log-cus
command: ["sh","-c","cat /log/data/output.log;sleep 1d"]
volumeMounts:
- name: data-log
mountPath: /log/data
volumes:
- name: data-log
通过命令查看log-cus是否有print出期望的日志
# 创建pod
kubectl apply -f log.pod.yaml
# 查看日志
kubectl logs log -c log-cus
# 查看容器内是否创建了log文件
kubectl exec -it log -c log-pro -- cat /log/data/output.log