Mock Exam Question: Volume subpath

Below is the question and solution in its entirety. I am having trouble understanding the need for a subpath here if the volume mount is directly to the conf file. Can you please explain why if we are mounted to the .conf file it is having trouble with the .config file, how the two are related and why mount the conf and not config or vice versa. If there is a simple example or a example of this in CKA course that would be very helpful.

For this question, please set the context to cluster1 by running:
kubectl config use-context cluster1
The blue-dp-cka09-trb deployment is having 0 out of 1 pods running. Fix the issue to make sure that pod is up and running.
Solution
List the pods
kubectl get pod
Most probably you see Init:Error or Init:CrashLoopBackOff for the corresponding pod.

Look into the logs
kubectl logs blue-dp-cka09-trb-xxxx -c init-container
You will see an error something like

sh: can’t open ‘echo ‘Welcome!’’: No such file or directory
Edit the deployment
kubectl edit deploy blue-dp-cka09-trb
Under initContainers: → - command: add -c to the next line of - sh, so final command should look like this
initContainers:

  • command:
    • sh
    • -c
    • echo ‘Welcome!’
      If you will check pod then it must be failing again but with different error this time, let’s find that out

kubectl get event --field-selector involvedObject.name=blue-dp-cka09-trb-xxxxx
You will see an error something like

Warning Failed pod/blue-dp-cka09-trb-69dd844f76-rv9z8 Error: failed to create containerd task: failed to create shim task: OCI runtime create failed: runc create failed: unable to start container process: error during container init: error mounting “/var/lib/kubelet/pods/98182a41-6d6d-406a-a3e2-37c33036acac/volumes/kubernetes.io~configmap/nginx-config” to rootfs at “/etc/nginx/nginx.conf”: mount /var/lib/kubelet/pods/98182a41-6d6d-406a-a3e2-37c33036acac/volumes/kubernetes.io~configmap/nginx-config:/etc/nginx/nginx.conf (via /proc/self/fd/6), flags: 0x5001: not a directory: unknown
Edit the deployment again
kubectl edit deploy blue-dp-cka09-trb
Under volumeMounts: → - mountPath: /etc/nginx/nginx.conf → name: nginx-config add subPath: nginx.conf and save the changes.
Finally the pod should be in running state.

If you look inside the configmap, you can see it has a file (surrounded in red box) whose name is nginx.conf, pointed to by the arrow

The nginx web server requires a file /etc/nginx/nginx.conf to be able to run, so we are mounting it there with a volume mount from the configmap.

The directory /etc/nginx contains other files and directories besides this file. We use subPath to mount only this file into the existing /etc/nginx without knocking out the entire contents of the existing directory.
Furthermore, and the cause of the specific error, is that a file nginx.conf will already exist in the container as it is part of the nginx default image. The error is a bit misleading, I will give you that.

So subPath will overwrite any existing file of the same name, and will not clear other files out of the directory.

1 Like

That is a really excellent explanation and helps me understand subPath. Thank you!