35

I have the following configuration for my pod:

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: my-app
  labels:
    app: my-app
spec:
  serviceName: my-app
  replicas: 1
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      restartPolicy: Never
      containers:
      - name: my-app
        image: myregistry:443/mydomain/my-app
        imagePullPolicy: Always

And it deploys fine without the restartPolicy. However, I do not want the process to be run again once finished, hence I added the 'restartPolicy: Never'. Unfortunately I get the following error when I attempt to deploy:

Error from server (Invalid): error when creating "stack.yaml": StatefulSet.apps "my-app" is invalid: spec.template.spec.restartPolicy: Unsupported value: "Never": supported values: "Always"

What am I missing?

Thanks

João Matos
  • 6,102
  • 5
  • 41
  • 76

2 Answers2

25

Please see https://github.com/kubernetes/kubernetes/issues/24725

It appears that only "Always" is supported.

snowdesert
  • 274
  • 3
  • 3
  • 2
    To anyone that is interested, here is the [link](https://stackoverflow.com/questions/39893238/kubernetes-how-to-run-job-only-once/39894448#39894448) on how to create a pod to run once. – max23_ Aug 22 '19 at 11:46
  • 2
    I was also expecting to be able to set `deployment.spec.template.spec.restartPolicy ` to "Never" or "OnFailure"! I read the link provided by @max23_. I suppose one could use `backoffLimit: 0`, though it doesn't seem like a robust solution as the reason why the pod terminated could very well be an actual failure! – mhyousefi Jan 21 '20 at 11:15
  • 2
    This is insane and always has been. Please change this. – nroose Jun 18 '22 at 02:31
  • 1
    The most insane part is that, to this day, the documentation clearly states that `Never` is supported: https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle/#restart-policy – Julien Debache Oct 24 '22 at 07:36
  • 1
    The documentation is not fixed. I guess its only for deployments. For "Pod" it should still work. – PraveenMak Nov 17 '22 at 02:23
12

You should use a Job controller instead of a StatefulSet:

A Job creates one or more Pods and ensures that a specified number of them successfully terminate. As pods successfully complete, the Job tracks the successful completions.

Take a look at Handling Pod and Container Failures section, which explains the effects of using restartPolicy with values OnFailure or Never, combined with another configs such as parallelism, completions and backoffLimit.

Eduardo Baitello
  • 10,469
  • 7
  • 46
  • 74