CodeBlanks Logo Icon CodeBlanks

Job YAML

Learn the Job YAML structure.

Job YAML

A Job is in the batch/v1 group. In the pod template, restartPolicy must be Never or OnFailure (Always is invalid). backoffLimit is the upper limit of failed attempts:

apiVersion: batch/v1
kind: Job
metadata:
  name: goc
spec:
  backoffLimit: 3
  template:
    spec:
      restartPolicy: Never
      containers:
        - name: goc
          image: api:1.0
          command: ["python", "manage.py", "migrate"]

With Never each attempt is a new pod (logs are kept); with OnFailure the container is restarted inside the same pod.

Time limit and cleanup

activeDeadlineSeconds limits the Job's total run time; when it expires, all pods are terminated. ttlSecondsAfterFinished deletes a finished Job (with its pods) after a given time:

spec:
  backoffLimit: 3
  activeDeadlineSeconds: 600       # at most 10 minutes
  ttlSecondsAfterFinished: 3600    # delete 1 hour after finishing
  template:
    ...

Practice A migration Job

Write a Job YAML: name goc; backoffLimit: 3; restartPolicy: Never in the pod template; container named goc, image api:1.0, command: ["python", "manage.py", "migrate"].

Starter code

terminal
# buraya yaz

Sample solution

terminal
apiVersion: batch/v1
kind: Job
metadata:
  name: goc
spec:
  backoffLimit: 3
  template:
    spec:
      restartPolicy: Never
      containers:
        - name: goc
          image: api:1.0
          command: ["python", "manage.py", "migrate"]

Expected output

job.batch/goc created
$ kubectl logs job/goc
Applying migrations... OK

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 If a Job with backoffLimit: 3 fails four times, what happens?

  • It turns into a Deployment
  • It counts as successful
  • The Job is marked Failed and doesn't retry Correct answer
  • It retries forever

Explanation: The wait between attempts increases exponentially.

2 Turn off restarting in the Job pod.

Question
    spec:
      restartPolicy: ____
Answer
    spec:
      restartPolicy: Never

Explanation: Always is invalid in Jobs.

3 Match.

Term Meaning
batch/v1 Job and CronJob group
backoffLimit Retry limit
activeDeadlineSeconds Total time limit
ttlSecondsAfterFinished Automatic cleanup

All lessons in Advanced