题目
Context
Without Changing its existing containers,an existing Pod needs to be integrated into Kubernetes's built-in logging architecture(e.g.kubectl logs).Adding a streaming sidecar container is a good and common way to accomplish this requirement .
Task
Add a busybox sidecar container to the existing Pod legacy-app.The new sidecar container has to run the following command:
/bin/sh -c tail -n+1 -f /var/log/legacy-app.log
use a volume mount named logs to make the file /var/log/legacy-app.log available to the sidecar container
答案
官网:Kubernetes 文档/概念/集群管理/日志架构
apiVersion: v1
kind: Pod
metadata:
name: legacy-app
spec:
containers:
- name: count
image: busybox:1.28
args:
- /bin/sh
- -c
- >
i=0;
while true;
do
echo "$(date) INFO $i" >> /var/log/legacy-app.log;
sleep 1;
done
volumeMounts:
- name: varlog
mountPath: /var/log
- name: count-log-1
image: busybox:1.28
args: [/bin/sh, -c, 'tail -n+1 -f /var/log/legacy-app.log']
volumeMounts:
- name: varlog
mountPath: /var/log
volumes:
- name: varlog
emptyDir: {}
执行
$ kubectl config use-context k8s
$ kubectl get po legacy-app -o yaml > 15.yaml
#查看日志
$ kubectl logs legacy-app count-log-1