<p>I am trying to add a side car container to an existing pod (webapp-1) to save the logs. However, I am getting error after creating the pod. The pod is crashing and the status changes to error..</p>
<p>For the below question i have added the yaml file. Please let me know if this is fine.</p>
<p> Add a side car container to the running pod logging-pod with the blow specification</p>
<p> The image of the sidecar container is busybox and the container writes the logs as below</p>
<p>tail -n+1 /var/log/k8slog/application.log</p>
<p> The container shares the volume logs with the application container the mounts to the</p>
<p>directory /var/log/k8slog</p>
<p> Do not alter the application container and verify the logs are written properly to the file</p>
<p>here is the yaml file.. I dont understand where I am making a mistake here.</p>
<pre><code>apiVersion: v1

kind: Pod

metadata:

  creationTimestamp: &quot;2021-10-25T07:54:07Z&quot;

  labels:

    name: webapp-1

  name: webapp-1

  namespace: default

  resourceVersion: &quot;3241&quot;

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

spec:

  containers:

  - image: kodekloud/event-simulator

    imagePullPolicy: Always

    name: simple-webapp

    - /bin/sh

    - -c

    - &gt;

      i=0;

      while true;

      do

        echo &quot;$i: $(date)&quot; &gt;&gt; /var/log/k8slog/application.log

        echo &quot;$(date) INFO $i&quot; &gt;&gt;;

        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: &quot;2021-10-25T07:54:07Z&quot;

    status: &quot;True&quot;

    type: Initialized

  - lastProbeTime: null
</code></pre>

<p>First of all, You could create a directory and the logfile itself. If the <code>count-log-1</code> container spin up first, it will have nothing to read and exit with an error. To to it, a good practise is to use an <strong>Init Container</strong>. <a href="https://kubernetes.io/docs/concepts/workloads/pods/init-containers/" rel="nofollow noreferrer">https://kubernetes.io/docs/concepts/workloads/pods/init-containers/</a></p>
<p>Second, containers need to have a shared volume, on which the logfile will be present. If there is no need to persist data, an <strong>emptyDir</strong> volume will enough. <a href="https://kubernetes.io/docs/concepts/workloads/pods/init-containers/" rel="nofollow noreferrer">https://kubernetes.io/docs/concepts/workloads/pods/init-containers/</a></p>
<p>Finally, You hade some errors in shell commands. Full <code>.yaml</code> file:</p>
<pre class="lang-yaml prettyprint-override"><code>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 &quot;$i: $(date)&quot; &gt;&gt; /var/log/k8slog/application.log
            echo &quot;$(date) INFO $i&quot;
            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: {} 

    
</code></pre>
