0

我正在尝试将边车容器添加到现有 pod(webapp-1)以保存日志。但是,创建 pod 后出现错误。吊舱正在崩溃,状态更改为错误..

对于以下问题,我添加了 yaml 文件。请让我知道这是否可以。

 在运行的pod logging-pod 中添加一个side car 容器,带有blow 规范

 sidecar 容器镜像是busybox,容器写入日志如下

tail -n+1 /var/log/k8slog/application.log

 容器与挂载到的应用容器共享卷日志

目录 /var/log/k8slog

 不要更改应用程序容器并验证日志是否正确写入文件

这是yaml文件。我不明白我在哪里犯了错误。

apiVersion: v1

kind: Pod

metadata:

  creationTimestamp: "2021-10-25T07:54:07Z"

  labels:

    name: webapp-1

  name: webapp-1

  namespace: default

  resourceVersion: "3241"

  uid: 8cc29748-7879-4726-ac60-497ee41f7bd6

spec:

  containers:

  - image: kodekloud/event-simulator

    imagePullPolicy: Always

    name: simple-webapp

    - /bin/sh

    - -c

    - >

      i=0;

      while true;

      do

        echo "$i: $(date)" >> /var/log/k8slog/application.log

        echo "$(date) INFO $i" >>;

        i=$((i+1));

       sleep 1;

      done

    volumeMounts:

    - name: varlog

      mountPath: /var/log

  - name: count-log-1

    image: busybox

    args: [/bin/sh, -c, 'tail -n+1 /var/log/k8slog/application.log']

    volumeMounts:

    - name: varlog

      mountPath: /var/log

    ports:

    - containerPort: 8080

      protocol: TCP

    resources: {}

    terminationMessagePath: /dev/termination-log

    terminationMessagePolicy: File

    volumeMounts:

    - mountPath: /var/run/secrets/kubernetes.io/serviceaccount

      name: default-token-fgstk

      readOnly: true

  dnsPolicy: ClusterFirst

  enableServiceLinks: true

  nodeName: controlplane

  preemptionPolicy: PreemptLowerPriority

  priority: 0

  restartPolicy: Always

  schedulerName: default-scheduler

  securityContext: {}

  serviceAccount: default

  serviceAccountName: default

  terminationGracePeriodSeconds: 30

  tolerations:

  - effect: NoExecute

    key: node.kubernetes.io/not-ready

    operator: Exists

    tolerationSeconds: 300

  - effect: NoExecute

    key: node.kubernetes.io/unreachable

    operator: Exists

    tolerationSeconds: 300

  volumes:

  - name: varlog

    mountPath: /var/log

  - name: default-token-fgstk

    secret:

      defaultMode: 420

      secretName: default-token-fgstk

status:

  conditions:

  - lastProbeTime: null

    lastTransitionTime: "2021-10-25T07:54:07Z"

    status: "True"

    type: Initialized

  - lastProbeTime: null
4

1 回答 1

1

首先,您可以创建一个目录和日志文件本身。如果count-log-1容器先启动,它将没有任何内容可读取并退出并出现错误。为此,一个好的做法是使用Init Containerhttps://kubernetes.io/docs/concepts/workloads/pods/init-containers/

其次,容器需要有一个共享卷,日志文件将存在于该卷上。如果不需要持久化数据,一个emptyDir卷就足够了。https://kubernetes.io/docs/concepts/workloads/pods/init-containers/

最后,您在 shell 命令中遇到了一些错误。完整.yaml文件:

apiVersion: v1
kind: Pod
metadata:
  labels:
    name: webapp-1
  name: webapp-1
  namespace: default

spec:
# Init container fo creating the log directory and file
# on the emptyDir volume, which will be passed to the containers
  initContainers:
  - name: create-log-file
    image: busybox
    command:
      - sh 
      - -c
      - |
          #!/bin/sh
          mkdir -p /var/log/k8slog
          touch /var/log/k8slog/application.log
# Mount varlog volume to the Init container
    volumeMounts:
    - name: varlog
      mountPath: /var/log

  containers:
  - image: kodekloud/event-simulator
    imagePullPolicy: Always
    name: simple-webapp
    command:
      - sh
      - -c
      - |
          i=0
          while true; do
            echo "$i: $(date)" >> /var/log/k8slog/application.log
            echo "$(date) INFO $i"
            i=$((i+1))
          sleep 1
          done
# Mount varlog volume to simple-webapp container
    volumeMounts:
    - name: varlog
      mountPath: /var/log
  - name: count-log-1
    image: busybox
    command:
      - sh
      - -c
      - |
          tail -f -n 1 /var/log/k8slog/application.log
# Mount varlog volume to count-log-1 container
    volumeMounts:
    - name: varlog
      mountPath: /var/log

# Define na emptyDir shared volume
  volumes:
  - name: varlog
    emptyDir: {} 

    
于 2021-10-25T13:54:28.530 回答