In Kubernetes, a Pod's restartPolicy defines what Kubernetes should do when a container inside the Pod terminates. It is an important part of Pod lifecycle management and determines whether containers should be restarted automatically.
Kubernetes supports three restart policies:
Always
Always is the default policy. Kubernetes restarts a container whenever it terminates, regardless of whether it exited successfully or failed.
This policy is commonly used for long-running workloads such as applications, APIs, and services where the container is expected to keep running.
OnFailure
With OnFailure, Kubernetes restarts the container only when it terminates with a non-zero exit status, indicating a failure.
This can be useful for workloads where successful completion should not trigger another execution, such as certain batch-processing tasks.
Never
With Never, Kubernetes does not restart the container after it terminates. The Pod records the container's termination state, allowing the workload to complete or fail without automatically starting the container again.
Important Kubernetes Consideration
The restartPolicy belongs to the Pod, not an individual container. Also, restart behavior should not be confused with controllers such as Deployments or Jobs.
For example, a Deployment normally creates Pods with the Always restart policy because it manages continuously running applications. Jobs are designed for tasks that eventually complete and use different workload semantics.
Final Thoughts
A simple way to remember the three policies is:
Always → Restart whenever the container stops
OnFailure → Restart only when the container fails
Never → Don't restart the container
Choosing the appropriate policy depends on whether your workload is a continuously running service, a batch task, or a one-time process.