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.答案

  1. 首先通过run命令输出yaml文件,再在其基础上修改

    1. kubectl run log --image=busybox --dry-run=client -oyaml > log-pod.yaml
    1. apiVersion: v1
    2. kind: Pod
    3. metadata:
    4. creationTimestamp: null
    5. labels:
    6. run: log
    7. name: log
    8. spec:
    9. containers:
    10. - image: busybox
    11. name: log
    12. resources: {}
    13. dnsPolicy: ClusterFirst
    14. restartPolicy: Always
    15. status: {}
  2. 根据题目修改yaml

    1. apiVersion: v1
    2. kind: Pod
    3. metadata:
    4. creationTimestamp: null
    5. labels:
    6. run: log
    7. name: log
    8. spec:
    9. containers:
    10. - image: busybox
    11. name: log-pro
    12. resources: {}
    13. command: ["sh","-c","echo important information >> /log/data/output.log;sleep 1d"]
    14. volumeMounts:
    15. - name: data-log
    16. mountPath: /log/data
    17. - image: busybox
    18. name: log-cus
    19. command: ["sh","-c","cat /log/data/output.log;sleep 1d"]
    20. volumeMounts:
    21. - name: data-log
    22. mountPath: /log/data
    23. volumes:
    24. - name: data-log
  3. 通过命令查看log-cus是否有print出期望的日志

    1. # 创建pod
    2. kubectl apply -f log.pod.yaml
    3. # 查看日志
    4. kubectl logs log -c log-cus
    5. # 查看容器内是否创建了log文件
    6. kubectl exec -it log -c log-pro -- cat /log/data/output.log