杨梅冲
每天在想什么呢?
官方文档:https://kubernetes.io/zh-cn/docs/concepts/cluster-administration/logging/


题目:Sidecar代理 设置配置环境kubectl config use-context k8s 将一个现有的 Pod 集成到 Kubernetes 的内置日志记录体系结构中(例如 kubectl logs)。添加 streaming sidecar 容器是实现此要求的一种好方法。 使用busybox Image来将名为sidecar的sidecar容器添加到现有的Pod 11-factor-app上,新的sidecar容器必须运行以下命令: /bin/sh -c tail -n+1 -f /var/log/11-factor-app.log 使用volume挂载/var/log/目录,确保sidecar能访问/var/log/
注意:
由于pod: 11-factor-app已经建好了,需要将此pod导出然后,加入sidecar后再运行
kubectl get pod 11-factor-app -o yaml > sidecar.yaml

在原来的pod上挂载
    volumeMounts:
    - name: varlog
      mountPath: /var/log
# 新增sidecar
  - name: sidecar
    image: docker.io/library/busybox:latest
    imagePullPolicy: IfNotPresent
    args: [/bin/sh, -c, 'tail -n+1 -f /var/log/11-factor-app.log']
    volumeMounts:
    - name: varlog
      mountPath: /var/log

# 在volumes:中新增一个挂载
  volumes:
    - name: varlog
      emptyDir: {}

 

.log文件
解答:
# 1.切换环境
kubectl config use-context k8s

# 2.编写yaml
[root@master2 sidecar]# cat side.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: 11-factor-app
spec:
  containers:
  - name: count
    image: docker.io/library/busybox:latest
    imagePullPolicy: IfNotPresent
    args:
    - /bin/sh
    - -c
    - >
      i=0;
      while true;
      do
        echo "$i: $(date)" >> /var/log/11-factor-app.log;
        i=$((i+1));
        sleep 1;
      done
    volumeMounts:
    - name: varlog
      mountPath: /var/log
  - name: sidecar
    image: docker.io/library/busybox:latest
    imagePullPolicy: IfNotPresent
    args: [/bin/sh, -c, 'tail -n+1 -f /var/log/11-factor-app.log']
    volumeMounts:
    - name: varlog
      mountPath: /var/log

  volumes:
    - name: varlog
      emptyDir: {}

# 3.运行检测
删除之前的pod 
kubectl delete pod 11-factor-app
[root@master2 sidecar]# kubectl apply -f side.yaml 
pod/11-factor-app created

# 查看日志
kubectl logs 11-factor-app -c sidecar

 

posted on 2024-01-12 13:01  杨梅冲  阅读(358)  评论(0编辑  收藏  举报