Kubernetes and Cloud Native Associate (KCNA)Kubernetes FundamentalsMedium

A DevOps engineer is configuring a Kubernetes Deployment for a new application. They want to ensure that if a Pod's container exits with an error, Kubernetes attempts to restart it. However, if the container exits successfully, it should not be restarted. Which `restartPolicy` value should be set in the Pod's specification?

  1. AOnFailure
  2. BUnlessTerminated
  3. CNever
  4. DAlways
Show answer & explanation

Correct answer: A. OnFailure

The `OnFailure` restart policy ensures that a container is only restarted if it terminates with a non-zero exit code (indicating an error). If it exits successfully (zero exit code), it will not be restarted.

Why the other options are wrong

  • B. `UnlessTerminated` is not a valid `restartPolicy` value in Kubernetes Pods.
  • C. `Never` would prevent any restarts, even upon failure, which is not desired.
  • D. `Always` would restart the container regardless of its exit code, which is not the desired behavior.

Pod restartPolicy: OnFailure

The `OnFailure` restart policy specifies that a container will only be restarted if it exits with a non-zero exit code, indicating an error.

  • Container is restarted if it fails.
  • Container is NOT restarted if it succeeds (exit code 0).
  • Commonly used for Jobs or other batch-like processes.
  • Can be set at the Pod level.

Memory trick: Restart On Failure, but Never if it's Always successful.

More Kubernetes Fundamentals questions