72

The best source for restart policies in Kubernetes I have found is this:

https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle/#restart-policy

But it only lists the possible restartPolicy values and does not explain them.

What is the difference between Always and OnFailure? Mustn't the thing fail before it can be restarted?

2 Answers 2

103

Always means that the container will be restarted even if it exited with a zero exit code (i.e. successfully). This is useful when you don't care why the container exited, you just want to make sure that it is always running (e.g. a web server). This is the default.

OnFailure means that the container will only be restarted if it exited with a non-zero exit code (i.e. something went wrong). This is useful when you want accomplish a certain task with the pod, and ensure that it completes successfully - if it doesn't it will be restarted until it does.

Never means that the container will not be restarted regardless of why it exited.

These different restart policies basically map to the different controller types as you can see from kubectl run --help:

--restart="Always": The restart policy for this Pod. Legal values [Always, OnFailure, Never]. If set to 'Always' a deployment is created for this pod, if set to 'OnFailure', a job is created for this pod, if set to 'Never', a regular pod is created. For the latter two --replicas must be 1. Default 'Always'

And the pod user-guide:

ReplicationController is only appropriate for pods with RestartPolicy = Always. Job is only appropriate for pods with RestartPolicy equal to OnFailure or Never.

5
  • Hello Pixel, can I understand that, If I use RS/deployment to create pods, I must set "RestartPolicy = Always" in yaml?
    – James Pei
    Apr 10, 2020 at 2:41
  • 1
    @JamesPei not really!!. There is nothing to set RestartPolicy=_something_. It is an imperative command. All you need to do is you need to create a separate resource like RS or Deployment in your resource-definition file.
    – Gupta
    Apr 25, 2020 at 9:52
  • When would a container with a web-server exit with a zero exit code? Are there any scenarios where this could happen?
    – rahulmohan
    Jan 12, 2021 at 18:33
  • Thanks for this explanation, simplier than the official documentation.
    – d3vpasha
    May 6, 2021 at 20:06
  • 2
    Update: "If set to 'Always' a deployment is created for this pod, if set to 'OnFailure', a job is created for this pod, if set to 'Never', a regular pod is created. For the latter two --replicas must be 1. Default 'Always'" This is not shown anymore in current kubernetes version 1.21 and is no longer valid. Deleting a pod that has set restart Always won't get brought back like a deployment, for example. Sep 8, 2021 at 18:03
0

Take a practical example:

kubectl run never --image=alpine --restart=Never  -- echo "Hello" 

It will spin up a Pod that runs and terminates successfully (echo hello) and will never be restarted, therefore the status will be completed.

enter image description here

kubectl run onfail --image=alpine --restart=OnFailure  -- echo "Hello" 

It will spin up a Pod that runs and terminates successfully (will echo hello) therefore it will not be restarted, and the status will also completed: enter image description here

kubectl run onfail1 --image=alpine --restart=OnFailure  -- exit 0

It will spin up a Pod that runs and terminates but with an error therefore it will be restarted, and the pod will be in CrashLoopBackOff:

enter image description here

kubectl run always --image=alpine --restart=Always  -- echo "Hello"

It will spin up a Pod that runs and terminates successfully (will echo hello), but it will always get restarted: enter image description here

Using Always as a restart policy you might consider that it behaves like a deployment and using OnFailure as a restart policy it behaves like a job

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.