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 fails, it is automatically restarted, but only if the failure was due to an internal application error, not if the container exited cleanly. Which `restartPolicy` should be specified in the Pod's configuration?

  1. AUnlessStopped
  2. BNever
  3. COnFailure
  4. DAlways
Show answer & explanation

Correct answer: C. OnFailure

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

Why the other options are wrong

  • A. `UnlessStopped` is not a valid Kubernetes `restartPolicy` value.
  • B. `Never` prevents any restarts, even upon failure.
  • D. `Always` restarts the container regardless of the exit code.

Pod `restartPolicy`

The `restartPolicy` field in a Kubernetes Pod or container specifies how to handle container restarts after termination. It applies to all containers in the Pod.

  • Can be Always, OnFailure, or Never.
  • Default is Always for Deployments, StatefulSets, DaemonSets.
  • Default is OnFailure for Jobs.

Memory trick: Restart 'Policy' dictates the 'P'rotocol for 'P'od problems.

More Kubernetes Fundamentals questions