How can I block Kubernetes pod restarting after ended process

4/11/2021

I have a python script and I used on Kubernetes. After process ended on python script Kubernetes restart pod. And I don't want to this.

I tried to add a line of code from python script like that:

text = input("please a key for exiting")

And I get EOF error, so its depends on container has no EOF config on my Kubernetes.

After that I tried to use restartPolicy: Never. But restartPolicy is can not be Never and I get error like that:

error validating data: ValidationError(Deployment.spec.template): unknown field \"restartPolicy\" in io.k8s.api.core.v1.PodTemplateSpec;

How can I make this? I just want to no restart for this pod. Its can be on python script or Kubernetes yaml file.

-- akasaa
kubernetes
python
python-3.x

3 Answers

4/11/2021

A PodSpec has a restartPolicy field with possible values Always, OnFailure, and Never. The default value is Always. could you please try OnFailure you have only one container it should work.

-- Rakesh Singh Rana
Source: StackOverflow

4/11/2021

you have a single process which ends due to exception you need to correct that logic. and can set OnFailure. If you want to debug your code then you need to catch this error and log it. so that your process should not end or you can run this logic inside a new thread. which will be child thread and in main thread you can have sleep or wait condition.

-- Rakesh Singh Rana
Source: StackOverflow

4/12/2021

You get unknown field \"restartPolicy\" in io.k8s.api.core.v1.PodTemplateSpec; because you most probably messed up some indentation.

Here is an example deploymeny with incorrect indentation of restartPolicy field:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.14.2
        ports:
        - containerPort: 80
    restartPolicy: Never   # <-------

Here is a deploymet with correct indentation:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.14.2
        ports:
        - containerPort: 80
      restartPolicy: Never   # <-------

But this will result in error:

kubectl  apply -f deploy.yaml
The Deployment "nginx-deployment" is invalid: spec.template.spec.restartPolicy: Unsupported value: "Never": supported values: "Always"

Here is an explaination why: restartpolicy-unsupported-value-never-supported-values-always


If you want to run a one time pod, use a k8s job or use pod directly:

apiVersion: v1
kind: Pod
metadata:
  labels:
    run: ngx
  name: ngx
spec:
  containers:
  - image: nginx
    name: ngx
  restartPolicy: Never
-- Matt
Source: StackOverflow