Ultimate mock CKA exam 5, Q 4- pod is not running

Problem:

Solve this question on: ssh cluster2-controlplane

In the cka-multi-containers namespace, proceed to create a pod named cka-sidecar-pod that adheres to the following specifications:

The first container, labeled main-container, is required to run the nginx:1.27, which writes the current date along with a greeting message Hi I am from Sidecar container to /log/app.log.

The second container, identified as sidecar-container, must use the nginx:1.25 image and serve the app.log file as a webpage located at /usr/share/nginx/html.

Note: Do not rename app.log to index.html. The file name should remain app.log and be available at /app.log via the Nginx server.

Solution

pod.yaml

apiVersion: v1
kind: Pod
metadata:
  name: cka-sidecar-pod
  namespace: cka-multi-containers
spec:
  containers:
    - name: main-container
      image: nginx:1.27
      command: ["/bin/sh"]
      args:
        - -c
        - |
          while true; do
            echo "$(date) Hi I am from Sidecar container" >> /log/app.log;
            sleep 5;
          done
      volumeMounts:
        - name: shared-logs
          mountPath: /log
    - name: sidecar-container
      image: nginx:1.25
      volumeMounts:
        - name: shared-logs
          mountPath: /usr/share/nginx/html
  volumes:
    - name: shared-logs
      emptyDir: {}

This is the exact solution provided in solution category
However,the pods are keep on getting β€œCrashBackLoop” error and not in β€œrunning” status.

cluster2-controlplane ~ ➜  k get pod cka-sidecar-pod
NAME              READY   STATUS             RESTARTS       AGE
cka-sidecar-pod   1/2     CrashLoopBackOff   22 (96s ago)   89m
cluster2-controlplane ~ ➜  k logs cka-sidecar-pod  -c main-container
sleep 2; done: 1: Syntax error: end of file unexpected (expecting "done")

Please let me know, whats wrong in the pod definition file.

I’m not sure what the posted solution was – I got the problem right from first principles. My solution was:

apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: cka-sidecar-pod
  name: cka-sidecar-pod
  namespace: cka-multi-containers
spec:
  volumes:
    - name: log
      emptyDir: {}
  containers:
  - command:
    - sh
    - -c
    - while true; do date; echo 'Hi I am from Sidecar container' >> /log/app.log; done
    image: nginx:1.27
    name: main-container
    resources: {}
    volumeMounts:
      - name: log
        mountPath: /log
  - name: sidecar-container
    image: nginx:1.25
    volumeMounts:
      - name: log
        mountPath: /usr/share/nginx/html

  dnsPolicy: ClusterFirst
  restartPolicy: Always
status: {}

I would guess that the entire script should be under command; command + args isn’t typically the way to go. Besides that, your solution looks pretty similar.

1 Like

Thanks @rob_kodekloud that worked. May be we can rectify the proposed solution posted