kubernetes: pod restart policy

2 min read | by Jordi Prats

When defining objects that contain a pod template (such a Deployment or a Job) but also when defining a plain pod we can control under which circumstances a pod will be restarted

The container restart policy can be controlled using restartPolicy on the spec at the same level where we define the containers:

apiVersion: batch/v1
kind: Job
  name: demo-restartPolicy-job
  backoffLimit: 2
      name: demo-restartPolicy-pod
      - name: demo
        image: sonarsource/sonar-scanner-cli
      restartPolicy: Never  

Since the restartPolicy is defined at the same level as all the containers, it applied at the Pod level. We can set it to:

  • Always: The pod needs to be always running so, every time stops, a new one will be spawned
  • OnFailure: Only if the container terminates with a non zero return code, it will be restarted. A container that returns a 0 (success) doesn't need to be restarted
  • Never: Do not attempt to restart the container

If we are not setting explicitly any value it will be set the default value: Always

We'll need to keep in mind that restartPolicy only refers to restarts of the containers by the kubelet on the same node.

If the container keeps failing, the restarts are going to be delayed with an exponential back-off delay: 10s, 20s, 40s, ..., up to five minutes. Once the container has been running for 10 minutes, the kubelet resets the backoff timer for that container

Posted on 01/06/2021