添加一个sidecar容器(使用busybox 镜像)到已有的pod
11-factor-app中,确保sidecar容器能够输出/var/log/11-factor-app.log的信息,使用volume挂载/var/log目录,确保sidecar能访问11-factor-app.log
文件
# 准备工作 创建一个 pod 11-factor-appapiVersion: v1
kind: Pod
metadata:name: 11-factor-app
spec:containers:- name: countimage: busyboxargs: - /bin/sh- -c- >i=0;while true;doecho "$i: $(date)" >> /var/log/11-factor-app.log;done volumeMounts:- name: varlogmountPath: /var/log volumes:- name: varlogemptyDir: {}
controlplane ~ ➜ kubectl apply -f 11-factor-app.yml
pod/11-factor-app created
# 查看容器输出controlplane ~ ➜ kubectl exec 11-factor-app -it -- tail -n 5 /var/log/11-factor-app.log
0: Sun Jul 28 14:33:01 UTC 2024
0: Sun Jul 28 14:33:01 UTC 2024
0: Sun Jul 28 14:33:01 UTC 2024
0: Sun Jul 28 14:33:01 UTC 2024
0: Sun Jul 28 14:33:01 UTC 2024
# 添加sidecar
apiVersion: v1
kind: Pod
metadata:name: 11-factor-app
spec:containers:- name: countimage: busyboxargs: - /bin/sh- -c- >i=0;while true;doecho "$i: $(date)" >> /var/log/11-factor-app.log;done volumeMounts:- name: varlogmountPath: /var/log - name: sidecarimage: busyboxargs:- /bin/sh- -c- "tail -n+1 -f /var/log/11-factor-app.log"volumeMounts:- name: varlogmountPath: /var/log volumes:- name: varlogemptyDir: {}
controlplane ~ ➜ kubectl replace -f 11-factor-app-sidecar.yml --force
pod "11-factor-app" deleted
pod/11-factor-app replaced#查看sidecar是否能读取到日志 打印最近五条信息
controlplane ~ ➜ kubectl logs 11-factor-app sidecar --tail=5
0: Sun Jul 28 14:37:48 UTC 2024
0: Sun Jul 28 14:37:48 UTC 2024
0: Sun Jul 28 14:37:48 UTC 2024
0: Sun Jul 28 14:37:48 UTC 2024
0: Sun Jul 28 14:37:48 UTC 2024