Run a Kubernetes Jobs only Once

9/16/2019

I want to run a kubernetes Job exactly once. This may by a script that will do some database updates. We dont want for any reason, that script to be executed more than once.

Any suggestion, how can we do it with kubernetes.

-- nimit garg
kubernetes

1 Answer

9/16/2019

You need to use a Job resource, with the backoffLimit set to 0, like the following:

apiVersion: batch/v1
kind: Job
metadata:
  name: example
spec:
  backoffLimit: 0
  template:
    spec:
      containers:
        - name: example
          image: alpine:3
          command: ["sh", "-c", "exit 1"]
      restartPolicy: Never

Afthe the job has run once, if it fails, it will reach the backoffLimit, and will not retry. If you describe the job, you can verify it:

Warning  BackoffLimitExceeded  117s  job-controller  Job has reached the specified backoff limit
-- Utku Ă–zdemir
Source: StackOverflow