Set Pod Restart Limit

12/12/2019

Can we set Pod Restart Limit like Docker swarm in Kubernetes?

Docker swarm restart_policy:

    condition: any
    delay: 60s
    max_attempts: 2
    window: 60s         

or any other way because k8s kind Deployment doesn't support Restart Policy never or onfailure

My Deployment.yaml:-

apiVersion: apps/v1
kind: Deployment
metadata:
  name: xyz
  labels:
    app: xyz
spec:
  replicas: 1
  selector:
    matchLabels:
      app: xyz
  template:
    metadata:
      labels:
        app: xyz
    spec:
      containers:
      - name: xyz
        image: x.y.z
        imagePullPolicy: IfNotPresent
      imagePullSecrets:
      - name: x
      restartPolicy: Never

Error is invalid: spec.template.spec.restartPolicy: Unsupported value: "Never": supported values: "Always"

-- Ankit Singh
docker
kubernetes
kubernetes-pod
minikube

2 Answers

12/12/2019

run kubectl run --help | grep restart

you will see that there is a restartpolicy setting as follows

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

there is also a new option for maxRetries

$ cat pod_retry.yaml
apiVersion: v1
kind: Pod
metadata:
  name: test-pod
spec:
  restartPolicy: "OnFailure"
  maxRetries: "3"                // Max retries is 3
  containers:
  - image: nginx:1.7.9
    name: test-pod
    command:
    - /bin/ls
    - hello
-- julian
Source: StackOverflow

12/12/2019

Unfortunately, it is impossible. In official docs its mentioned that it allows only Always.

Only a .spec.template.spec.restartPolicy equal to Always is allowed, which is the default if not specified.

Same situation with DaemonSet and StatefulSet.

It is related to Deployment concept that all pods must be in Running state, pod cannot fail or be unresponsive.

Deployments represent a set of multiple, identical Pods with no unique identities. A Deployment runs multiple replicas of your application and automatically replaces any instances that fail or become unresponsive. In this way, Deployments help ensure that one or more instances of your application are available to serve user requests. Deployments are managed by the Kubernetes Deployment controller.

As default, Deployment automatically creates ReplicaSet which also taking care of pods status.

A ReplicaSet is defined with fields, including a selector that specifies how to identify Pods it can acquire, a number of replicas indicating how many Pods it should be maintaining, and a pod template specifying the data of new Pods it should create to meet the number of replicas criteria. A ReplicaSet then fulfills its purpose by creating and deleting Pods as needed to reach the desired number. When a ReplicaSet needs to create new Pods, it uses its Pod template.

Only pod and job kind is able to use restartPolicy with Never and onFailure values.

-- PjoterS
Source: StackOverflow