Job YAML
- Docker & Kubernetes course
- Advanced
- Lesson 26/70
- 4 min
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
# buraya yazSample solution
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.
spec:
restartPolicy: ____
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 |