52

I have a job definition based on example from kubernetes website.

apiVersion: batch/v1
kind: Job
metadata:
  name: pi-with-timeout-6
spec:
  activeDeadlineSeconds: 30
  completions: 1
  parallelism: 1
  template:
    metadata:
      name: pi
    spec:
      containers:
      - name: pi
        image: perl
        command: ["exit", "1"]
      restartPolicy: Never

I would like run this job once and not restart if fails. With comand exit 1 kubernetes trying to run new pod to get exit 0 code until reach activeDeadlineSeconds timeout. How can avoid that? I would like run build commands in kubernetes to check compilation and if compilation fails I'll get exit code different than 0. I don't want run compilation again.

Is it possible? How?

Mark Reid
  • 2,611
  • 3
  • 23
  • 45
esio
  • 1,592
  • 3
  • 17
  • 30
  • Do you want to run it zero or once; exactly once; or at least once? These all have different potential implementations. – deed02392 Jan 19 '22 at 12:33

3 Answers3

70

By now this is possible by setting backoffLimit: 0 which tells the controller to do 0 retries. default is 6

Ed Randall
  • 6,887
  • 2
  • 50
  • 45
pHiL
  • 1,722
  • 18
  • 19
46

If you want a one-try command runner, you probably should create bare pod, because the job will try to execute the command until it's successful or the active deadline is met.

Just create the pod from your template:

apiVersion: v1
kind: Pod
metadata:
  name: pi
spec:
  containers:
  - name: pi
    image: perl
    command: ["exit", "1"]
  restartPolicy: Never
Nebril
  • 3,153
  • 1
  • 33
  • 50
  • But how to stop/kill the pod when the command completes? In case of Jobs the running pod stops. – rishabh.bhardwaj Feb 28 '17 at 10:39
  • 5
    @rishabh.bhardwaj if you set `restartPolicy` to `Never`, like in my answer, after finishing the pod will just stop. – Nebril Feb 28 '17 at 11:19
  • 3
    the problem with using the pod instead of the job is that the pod will also fail for spurious reasons like the node being killed or other issues not under the user's control. – mkm Aug 21 '17 at 21:39
  • Pods are not a safe suggestion because Pod resources are not guaranteed to eventually succeed, unlike Jobs. – deed02392 Jan 19 '22 at 12:32
2

Sadly there is currently no way to prevent the job controller to just respawn new pods when they fail, but the kubernetes community is working on a solution, see:

"Backoff policy and failed pod limit" https://github.com/kubernetes/community/pull/583

mkm
  • 1,545
  • 1
  • 14
  • 21