Self-Healing pods in Kubernetes & their Restart Policies

Gaurav Kaushik
3 min readMay 19, 2021

Kubernetes provides this capability that can automatically restart containers whenever they fail. By the use of Restart Policies the user can customize this behavior by defining when the user want a pod’s containers to be automatically restarted.

Restart policies are an important component of self-healing applications, which are automatically repaired when a problem arises.

There are three possible values for a pod’s restart policy in Kubernetes:
Always
OnFailure &
Never

In Kubernetes, Always is the default restart policy. With this policy, containers will always be restarted if they stop, even if they completed successfully. This policy should be used for applications that always needs to be running.

The OnFailure Policy will always restart containers only if the container process exits with an error code or the container is determined to be unhealthy by a liveness probe. Use this policy for applications that need to run successfully & then stop.

Never restart policy cause the pod’s containers to never be restarted, even if the container exits or a liveness probe fails. This Policy should be used for applications that should run once and never be automatically restarted.

Lets have a demo to check the working of all of these policies:

  1. Login to Control Node & create a pod definition with ‘restartPolicy’ as Always:
This container will always restart if it will stop

2. Create a pod definition with ‘restartPolicy’ as OnFailure:

Container restart if container process exits with an error code or container is unhealthy

3. Change the pod definition a bit to check the working of OnFailure:

added a bogus command which will fail & will restart a pod

4. Create a pod definition with ‘restartPolicy’ as Never:

Pod will remain in error state, but will never restart

This was an overview of the Restart Policy of Kubernetes which basically gives the self-healing capability to the containers.
This reduces human-intervention into Compute Infrastructure & makes it less error-prone.

In the next post we will discuss about Multi-Container pods.
Thank You!

--

--