Kubernetes and Cloud Native Associate (KCNA)Kubernetes FundamentalsHard
A developer needs to configure a Pod to ensure that its containers are restarted only if they exit with a non-zero status code. If a container exits successfully (status code 0), it should not be restarted. Which field in the Pod's specification controls this behavior?
- A`spec.containers[].image`
- B`spec.restartPolicy`
- C`spec.volumes`
- D`spec.nodeName`
Show answer & explanationAnswer & explanation
Correct answer: B. `spec.restartPolicy`
The `spec.restartPolicy` field in a Pod's specification defines the policy for restarting containers in the Pod. The 'OnFailure' value ensures containers are only restarted if they exit with a non-zero status code, while 'Never' means no restarts, and 'Always' means restarts regardless of exit code.
Why the other options are wrong
- A. `image` specifies the container image, not its restart policy.
- C. `volumes` defines storage for the Pod, unrelated to container restart behavior.
- D. `nodeName` specifies a target node for the Pod, not its restart policy.
Pod `restartPolicy`
A field in a Pod's specification that defines the restart behavior for all containers within the Pod.
- Can be 'Always', 'OnFailure', or 'Never'.
- 'Always' is the default for Deployments, ReplicaSets, etc.
- 'OnFailure' is common for Jobs to retry failed tasks.
- 'Never' is used for one-off tasks that should not restart.
Memory trick: Restart policy dictates the container's 'second chance'.