Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kubernetes - how to run job only once

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?

like image 669
esio Avatar asked Oct 06 '16 10:10

esio


People also ask

How do you stop a job in Kubernetes?

Delete the job with kubectl (e.g. kubectl delete jobs/pi or kubectl delete -f ./job. yaml ). When you delete the job using kubectl , all the pods it created are deleted too.

How do I delete a Cronjob Kubernetes?

You can delete them at once with kubectl delete jobs --all , if you want to delete all jobs in the current namespace (not just the ones created by "hello".)

What is backoff limit in Kubernetes?

capped at six minutes. The back-off count is reset if no new failed Pods appear before the Job's next status check. If new job is scheduled before Job controller has a chance to recreate a pod (having in mind the delay after previous failure), Job controller starts counting from one again.


3 Answers

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

like image 144
pHiL Avatar answered Oct 03 '22 17:10

pHiL


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
like image 28
Nebril Avatar answered Oct 03 '22 16:10

Nebril


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

like image 44
mkm Avatar answered Oct 03 '22 17:10

mkm