Relevant Documentation:

  1. Create a pod with the Always restart policy that completes after a few seconds.
vi always-pod.yml

apiVersion: v1
kind: Pod
metadata:
  name: always-pod
spec:
  restartPolicy: Always
  containers:
  - name: busybox
    image: busybox
    command: ['sh', '-c', 'sleep 10']

Run the following command to create the pod:

kubectl create -f always-pod.yml
  1. Check the pod status. You should see it restarting after every 10-second completion.
kubectl get pod always-pod
  1. Create a pod with the OnFailure restart policy.
vi onfailure-pod.yml

apiVersion: v1
kind: Pod
metadata:
  name: onfailure-pod
spec:
  restartPolicy: OnFailure
  containers:
  - name: busybox
    image: busybox
    command: ['sh', '-c', 'sleep 10']

Run the following command to create the pod:

kubectl create -f onfailure-pod.yml
  1. Check the pod status. Note that the pod does not restart because it completed successfully.
kubectl get pod onfailure-pod
  1. Delete, modify, and recreate the pod so that it fails.
vi onfailure-pod.yml

apiVersion: v1
kind: Pod
metadata:
  name: onfailure-pod
spec:
  restartPolicy: OnFailure
  containers:
  - name: busybox
    image: busybox
    command: ['sh', '-c', 'sleep 10; this is a bad command that will fail']

Run the following command to create the pod:

kubectl create -f onfailure-pod.yml
  1. Check the pod status. Note that the pod restarts because it exited with an error code.