Jobs
Every night at 2 a.m. the notes team copies the database to backup storage. That task should run, finish and stop. A Deployment would restart it forever. A Job is built for work that ends.
The rule
A Job runs pods until enough of them succeed, then stops.
A Job creates pods and watches them. When a pod exits with success, the Job counts it. When the count reaches completions (default 1), the Job is complete. If a pod fails, the Job retries, up to backoffLimit times (default 6).
apiVersion: batch/v1
kind: Job
metadata:
name: notes-backup
namespace: notes
spec:
backoffLimit: 3
template:
spec:
restartPolicy: Never
containers:
- name: backup
image: ghcr.io/example/notes-backup:1.0Two rules:
restartPolicymust beNeverorOnFailure. The pod default,Always, would restart a finished task forever, so Jobs reject it.parallelismsets how many pods run at the same time — useful for splitting big work.
To run it on a timetable, wrap it in a CronJob:
apiVersion: batch/v1
kind: CronJob
metadata:
name: notes-backup
namespace: notes
spec:
schedule: "0 2 * * *"
jobTemplate:
spec:
template:
spec:
restartPolicy: OnFailure
containers:
- name: backup
image: ghcr.io/example/notes-backup:1.0The five schedule fields are minute, hour, day of month, month, day of week. 0 2 * * * means 02:00 every day.
The shortcut
Ask: "Should this ever finish?" No → Deployment. Yes, once → Job. Yes, again and again on a clock → CronJob.
notes-web
Serves users all day and should never finish: Deployment.
The trap is a backup script inside a Deployment: when it exits, Kubernetes treats that as a crash and starts it again, looping backups forever.
Drills
Answer fast — trust the rule.
Which restartPolicy is not allowed in a Job's pod template?
A Job has completions: 5 and parallelism: 2. How many pods run at the same time at most?
Which schedule runs at 02:30 every day?
A one-off database migration must run before release 1.3. Which object fits?
Remember
- A Job runs pods until enough succeed, then stops; a CronJob starts Jobs on a schedule.
- Ask "should this ever finish?": no → Deployment, once → Job, on a clock → CronJob.
- Trap: finite work in a Deployment gets restarted forever.
