Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between always and on failure for kubernetes restart policy?

Tags:

kubernetes

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

http://kubernetes.io/docs/user-guide/pods/multi-container/#restartpolicy

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?

like image 836
jonalv Avatar asked Nov 10 '16 15:11

jonalv


People also ask

What is the default restart policy in Kubernetes?

The default value is Always. The restartPolicy applies to all containers in the Pod. restartPolicy only refers to restarts of the containers by the kubelet on the same node. After containers in a Pod exit, the kubelet restarts them with an exponential back-off delay (10s, 20s, 40s, …), that is capped at five minutes.

Which of the following is not a possible value of restart policy of a container?

The possible values of restartPolicy are Always, OnFailure, Never. restartPolicy refers to restart of the container by the kubelet on same node.

What happens if a pod fails Kubernetes?

If a Pod's init container fails, the kubelet repeatedly restarts that init container until it succeeds. However, if the Pod has a restartPolicy of Never, and an init container fails during startup of that Pod, Kubernetes treats the overall Pod as failed.


1 Answers

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.

like image 110
Pixel Elephant Avatar answered Oct 12 '22 18:10

Pixel Elephant