Skip to content

Pod restart policy

Pod restart policy is a configuration setting in Kubernetes that dictates how the kubelet handles the restarting of containers within a Pod when they terminate.^[400-devops-06-kubernetes-k8s-ithelp-day10-readme.md] This policy is defined in the Pod specification under spec.template.spec.restartPolicy and applies to all containers within that Pod^[400-devops-06-kubernetes-k8s-ithelp-day10-readme.md].

Policy Values

The restart policy can be set to one of three possible values^[400-devops-06-kubernetes-k8s-ithelp-day10-readme.md]:

  • Always: The container will be restarted whenever it terminates. This is the default value^[400-devops-06-kubernetes-k8s-ithelp-day10-readme.md].
  • OnFailure: The container will only be restarted if it terminates with an error (non-zero exit status)^[400-devops-06-kubernetes-k8s-ithelp-day10-readme.md].
  • Never: The container will not be restarted under any circumstances^[400-devops-06-kubernetes-k8s-ithelp-day10-readme.md].

When a restart is triggered, the kubelet restarts the container on the same node with an exponential backoff delay (10s, 20s, 40s, etc.), capped at 5 minutes. The backoff timer resets if the container runs successfully for 10 minutes^[400-devops-06-kubernetes-k8s-ithelp-day10-readme.md].

Controller Constraints

Not all restart policies are valid for every type of Kubernetes controller. Different controllers have specific requirements based on their operational goals^[400-devops-06-kubernetes-k8s-ithelp-day10-readme.md]:

  • Job: Designed for one-time tasks (e.g., batch processing). Valid policies are OnFailure or Never^[400-devops-06-kubernetes-k8s-ithelp-day10-readme.md].
  • Deployment, ReplicaSet, Replication Controller: Designed for long-running services. These controllers typically require the Always policy to ensure continuous availability^[400-devops-06-kubernetes-k8s-ithelp-day10-readme.md].
  • DaemonSet: Ensures a Pod runs on every node. This controller also requires the Always policy^[400-devops-06-kubernetes-k8s-ithelp-day10-readme.md].

Example Configuration

The following example demonstrates a Deployment explicitly setting the restart policy to Always^[400-devops-06-kubernetes-k8s-ithelp-day10-readme.md]:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app
spec:
  replicas: 1
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      restartPolicy: Always
      containers:
      - name: my-app
        image: myregistry:443/mydomain/my-app

Sources

^[400-devops-06-kubernetes-k8s-ithelp-day10-readme.md]