Kubernetes CKA sample exam question 94 with answer

Question
Deploy a pod called log-demo with following YAML manifest:

apiVersion: v1
kind: Pod
metadata:
  name: log-demo
spec:
  volumes:
  - name: varlog
    emptyDir: {}
  containers:
  - name: counter
    image: busybox
    args:
    - '/bin/sh'
    - '-c'
    - 'i=0; while true; do echo "$i: $(date)" >> /var/log/demoapp.log; i=$((i+1)); sleep 1; done'
    volumeMounts:
    - name: varlog
      mountPath: /var/log
Now, edit the manifest and add a new container called monitor which will display the logs generated by the container called counter.
Use command /bin/sh -c tail -n+1 -f /var/log/app/demoapp.log to display logs.

Answer
Deploy manifest and check if logs are written:
kubectl apply -f po.yaml
kubectl exec -it log-demo -- tail -f /var/log/demoapp.log
Adjust the pod manifest:
kubectl edit po log-demo
you can mount the volume at different paths in different containers:
apiVersion: v1
kind: Pod
metadata:
  name: log-demo
spec:
  volumes:
  - name: varlog
    emptyDir: {}
  containers:
  - name: counter
    image: busybox
    args:
    - '/bin/sh'
    - '-c'
    - 'i=0; while true; do echo "$i: $(date)" >> /var/log/demoapp.log; i=$((i+1)); sleep 1; done'
    volumeMounts:
    - name: varlog
      mountPath: /var/log
  - name: monitor
    image: busybox
    args: ['/bin/sh', '-c', 'tail -n+1 -f /var/log/app/demoapp.log']
    volumeMounts:
    - name: varlog
      mountPath: /var/log/app
Deploy it and verify:
kubectl replace -f /tmp/kubectl-xxxxxxx.yaml --force
kubectl logs -f log-demo -c monitor
logs should come up