panchalhimself

Restart policies in kubernetes

20 June 2023

Control what happens when the container is detected to be unhealthy.

Always

Default restart policy. It will always restart the container on unhealthy/failure/stopped.

OnFailure

It will always restart the container on unhealthy or due to any error code.

Never

It will never restart the container. Will stay in the error state for ever incase there is error that causes failure.

These restart policies are under the spec:

kubectl run pod-restart-always --image=nginx --dry-run=client -o yaml

creates a template and has already defined restartPolicy

apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: pod-restart-always
  name: pod-restart-always
spec:
  containers:
  - image: nginx
    name: pod-restart-always
    resources: {}
  dnsPolicy: ClusterFirst
  restartPolicy: Always
status: {}

modify the restartPolicy according to the requirement.