添加一个sidecar容器(使用busybox 镜像)到已有的pod
11-factor-app中,确保sidecar容器能够输出/var/log/11-factor-app.log的信息,使用volume挂载/var/log目录,确保sidecar能访问11-factor-app.log
文件
# 准备工作 创建一个 pod 11-factor-app
apiVersion: v1
kind: Pod
metadata:
name: 11-factor-app
spec:
containers:
- name: count
image: busybox
args:
- /bin/sh
- -c
- >
i=0;
while true;
do
echo "$i: $(date)" >> /var/log/11-factor-app.log;
done
volumeMounts:
- name: varlog
mountPath: /var/log
volumes:
- name: varlog
emptyDir: {}
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: count
image: busybox
args:
- /bin/sh
- -c
- >
i=0;
while true;
do
echo "$i: $(date)" >> /var/log/11-factor-app.log;
done
volumeMounts:
- name: varlog
mountPath: /var/log
- name: sidecar
image: busybox
args:
- /bin/sh
- -c
- "tail -n+1 -f /var/log/11-factor-app.log"
volumeMounts:
- name: varlog
mountPath: /var/log
volumes:
- name: varlog
emptyDir: {}
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