What is the Pod restart policy in K8S?
In Kubernetes (K8S), Pod restart policy refers to how Kubernetes controller will handle the restart behavior of a Pod when it fails or is terminated. There are three common Pod restart policies in K8S.
- By default, Kubernetes will automatically restart a Pod when it terminates, ensuring that most applications remain running at all times.
- If a Pod terminates with a non-zero exit code, Kubernetes will restart the Pod, allowing applications that need to handle errors or exceptions to recovr. For example, if a Pod encounters an error related to network communication failure, it may be restarted to attempt to resolve the issue.
- Never: When a Pod is terminated, Kubernetes will not automatically restart it. This is suitable for one-time tasks or applications that do not need automatic restarts. Under this strategy, if a Pod terminates, it will remain terminated indefinitely until manually restarted.
You can specify the restart policy in the configuration file of the Pod by setting the restartPolicy field. For example:
apiVersion: v1
kind: Pod
metadata:
name: my-pod
spec:
restartPolicy: Always
containers:
- name: my-container
image: my-image
This will create a Pod using the default Always restart policy.