configuring kubernetes restart policy
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Kubernetes restart policy controls what the kubelet does when a container in a Pod exits. The confusing part is that Pod-level restart policy and controller behavior are related but not identical: a container restart inside one Pod is different from a Deployment or Job deciding whether to create or keep Pods.
The Three Restart Policies
At the Pod spec level, Kubernetes supports three values:
- '
Always' - '
OnFailure' - '
Never'
They mean:
- '
Always: restart the container whenever it exits' - '
OnFailure: restart only when the exit status is nonzero' - '
Never: do not restart the container inside that Pod'
A simple Pod example:
With OnFailure, the kubelet keeps restarting the container because it exits with failure.
Pod Restarts Versus Controller Replacement
This is the distinction many people miss.
restartPolicy tells the kubelet what to do with containers inside the same Pod. Controllers such as Deployments, StatefulSets, Jobs, and CronJobs decide whether Pods themselves should exist and how many of them there should be.
So there are two layers:
- kubelet restarts containers inside a Pod
- controllers create, replace, or stop Pods according to higher-level rules
That is why restart behavior can look different depending on whether you are running a naked Pod, a Job, or a Deployment.
Deployments Usually Mean Always
For workloads managed by a Deployment, the Pod template uses restartPolicy: Always. That is the normal setting for long-running services.
If the container crashes, Kubernetes restarts it inside the Pod. If the Pod disappears entirely, the Deployment creates a replacement Pod to maintain the desired replica count.
Jobs and OnFailure or Never
Batch workloads are different. A Job is meant to run to completion, so OnFailure or Never is more appropriate.
With Jobs:
- '
OnFailurerestarts failed containers inside the Pod' - '
Neverleaves the failed Pod as-is and lets the Job controller decide about new Pods'
This is useful when you want failed batch work to be visible and analyzable rather than repeatedly retried in place.
How Crash Loops Relate to Restart Policy
If a container repeatedly exits under Always or OnFailure, Kubernetes backs off between restarts and the Pod may show CrashLoopBackOff.
That status is not itself a restart policy. It is the observable result of a failing container being restarted repeatedly under a policy that allows restarts.
So when you see CrashLoopBackOff, the real debugging question is usually:
- why is the container exiting?
not:
- how do I get Kubernetes to stop reporting the symptom?
Choosing the Right Policy
A practical rule is:
- long-running services:
Always - retryable batch work:
OnFailure - one-shot runs where you want the terminated state preserved:
Never
Do not choose based only on whether restarting sounds nice. Choose based on workload intent.
Common Pitfalls
The biggest mistake is assuming restartPolicy controls everything about workload recovery. It only governs container restarts within a Pod; controllers add another layer of behavior.
Another mistake is using service-style thinking for Jobs. A batch task that should finish successfully is not the same as a web server that should run forever.
People also confuse CrashLoopBackOff with a configurable restart policy value. It is a status that appears when the actual process inside the container keeps failing.
Finally, avoid setting Never just to stop restarts while debugging and then forgetting to restore the correct production behavior. That can leave a service dead after its first real crash.
Summary
- Kubernetes restart policy applies at the Pod level and controls container restarts.
- '
Always,OnFailure, andNeverare the three valid values.' - Deployments typically use
Alwaysfor long-running services. - Jobs often use
OnFailureorNeverfor batch semantics. - Controller behavior and Pod restart behavior are related, but they are not the same thing.

