Kubernetes and Cloud Native Associate (KCNA) free question 2 / 10 · Domain 1: Kubernetes Fundamentals
A Pod does not set restartPolicy, and its container exits normally with exit code 0. What happens to the container?
- It is not restarted because it succeeded, and the Pod moves to the Succeeded phase
- The default is OnFailure, so an exit code of 0 leaves the container stopped
- The default policy applies and the kubelet restarts the container
- The whole Pod is deleted and a controller creates a new Pod to replace it
Show the answer and explanation
Answer: C
Why this is correct
The Pod Lifecycle page says the spec of a Pod has a restartPolicy field with possible values Always, OnFailure, and Never, and that the default value is Always. It defines Always as automatically restarting the container after any termination, while OnFailure restarts it only when it exits with an error (a non-zero exit status), and Never does not restart it automatically. Because this Pod sets nothing, the default Always applies, and the exit code is irrelevant: the container ended, so the kubelet restarts it. That is why a one-off task needs restartPolicy OnFailure or Never, normally through a Job, if it is supposed to stay finished.
Why the other options are wrong
"It is not restarted because it succeeded, and the Pod moves to the Succeeded phase" … That is how Never and OnFailure would treat a clean exit, but the default is neither. With no restartPolicy set, a successful exit still leads to a restart.
"The default is OnFailure, so an exit code of 0 leaves the container stopped" … The page says the possible values are Always, OnFailure, and Never, and that the default value is Always, not OnFailure.
"The whole Pod is deleted and a controller creates a new Pod to replace it" … A restart policy applies to the containers inside the same Pod, and the kubelet restarts them there. Replacing a Pod is a different mechanism, done by a controller that manages Pods.
The default policy ignores exit codes
restartPolicy defaults to Always, so even a clean exit is restarted. Use OnFailure or Never for tasks meant to finish.