Course contentsShow
← Cloud and Infrastructure
Lesson 1154 of 2,98923. Kubernetes WorkloadsFree lesson

Pod Restart Policies

Configuring how Kubernetes handles container failures with Always, OnFailure, and Never policies.

Pod Restart Policies

You'll learn: How to tell Kubernetes what to do when a container inside your pod crashes or exits.

What Are Restart Policies?

Containers sometimes fail—they might crash due to a bug, exit after completing a task, or encounter an error. Kubernetes needs to know whether you want it to automatically restart those containers or leave them stopped. That's where restart policies come in.

Think of a restart policy like instructions you'd give a babysitter: "If the kid falls asleep, let them rest (Never). If they're sick, check on them and help them recover (OnFailure). If they wake up at all, always get them back to playing (Always)."

The Three Policies

Kubernetes offers three restart policies you configure in your pod's YAML manifest:

Always (default): Kubernetes will restart the container no matter why it stopped—whether it crashed, exited successfully, or was killed. This is ideal for long-running services like web servers that should keep running indefinitely.

OnFailure: Kubernetes only restarts the container if it exits with an error (non-zero exit code). If the container completes successfully, it stays stopped. Perfect for batch jobs that should retry on failure but stop when successful.

Never: Kubernetes never restarts the container, regardless of how it exits. Use this when you want manual control or for one-time tasks where you'll investigate failures yourself.

Where to Set It

You specify the restart policy in your pod manifest's spec section:

spec:
  restartPolicy: OnFailure

Remember: the restart policy applies to all containers in the pod and cannot be changed after pod creation.

Key Takeaway: Restart policies (Always, OnFailure, Never) tell Kubernetes how to handle container exits, automating recovery for services or controlling behavior for jobs.