nimit garg
nimit garg

Reputation: 31

Run a Kubernetes Jobs only Once

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.

Upvotes: 1

Views: 4015

Answers (1)

Utku Özdemir
Utku Özdemir

Reputation: 7725

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

Upvotes: 2

Related Questions