LAB-15:Sidecar代理

LAB-15:Sidecar代理

LAB 概述

添加一个名为 busybox 且镜像为 busybox 的 sidecar 到一个已经存在的名为 legacy-app 的 Pod 上,这个 sidecar 的启动命令为 /bin/sh, -c, 'tail -n+1 -f /var/log/legacy-app.log'。
并且这个 sidecar 和原有的镜像挂载一个名为 logs 的 volume,挂载的目录为 /var/log/

LAB 预配

# 编写 pod 资源清单
user1@k8s-master:~/cka-2022-05-01/15$ cat legacy-app.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: legacy-app
spec:
  containers:
  - name: count
    image: busybox
    imagePullPolicy: IfNotPresent
    args:
    - /bin/sh
    - -c
    - >
      i=0;
      while true;
      do
        echo "$i: $(date)" >> /var/log/legacy-app.log;
        sleep 1;  # 可以把时间修改 5 秒间隔
      done
    volumeMounts:
    - name: varlog
      mountPath: /var/log
  volumes:
  - name: varlog
    emptyDir: {}

# 创建 pod
user1@k8s-master:~/cka-2022-05-01/15$ kubectl apply -f legacy-app.yaml 
pod/legacy-app created

# 查看 pod
user1@k8s-master:~$ kubectl get pod legacy-app 
NAME         READY   STATUS    RESTARTS   AGE
legacy-app   1/1     Running   0          26s 

 LAB 答案

# 切换 content
$ kubectl config use-context k8s

# 导出后修改 legacy-app.yaml 文件,考试需要使用“-o”参数导出编辑,然后执行
$ cat legacy-app.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: legacy-app
spec:
  containers:
  - name: count
    image: busybox
    imagePullPolicy: IfNotPresent
    args:
    - /bin/sh
    - -c
    - >
      i=0;
      while true;
      do
        echo "$i: $(date)" >> /var/log/legacy-app.log;
        sleep 1; # 可以把时间修改 5 秒间隔
      done
    volumeMounts:
    - name: varlog
      mountPath: /var/log
  - name: busybox
    image: busybox
    args: [/bin/sh, -c, 'tail -n+1 -f /var/log/legacy-app.log']
    volumeMounts:
    - name: varlog
      mountPath: /var/log
  volumes:
  - name: varlog
    emptyDir: {}

# 执行 yaml
$ kubectl delete -f legacy-app.yaml 
pod "legacy-app" deleted

$ kubectl apply -f legacy-app.yaml 
pod/legacy-app created

 LAB 验证

# 查看 pod 资源,发现这个 pod 里面是 2 个容器
user1@k8s-master:~$ kubectl get pod legacy-app 
NAME         READY   STATUS    RESTARTS   AGE
legacy-app   2/2     Running   0          67s

# 测试访问日志
user1@k8s-master:~$ kubectl logs legacy-app -c busybox
0: Tue May  3 06:28:39 UTC 2022
0: Tue May  3 06:28:40 UTC 2022
0: Tue May  3 06:28:41 UTC 2022

 参考资料

  • https://kubernetes.io/zh/docs/concepts/cluster-administration/logging/
posted @ 2022-11-22 13:51  武装小灰灰  阅读(44)  评论(0编辑  收藏  举报