Kubernetes - how to run job only once

esio picture esio · Oct 6, 2016 · Viewed 20.9k times · Source

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
  paralleism: 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?

Answer

Nebril picture Nebril · Oct 6, 2016

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