Day 52: Jobs and CronJobs
Run-to-completion workloads
A Job runs a Pod until it successfully completes (unlike a Deployment, which keeps a Pod running forever and restarts it if it exits) — for batch work: a data migration, a one-off script, a report generation.
A CronJob creates Jobs on a schedule, using the same cron syntax from Phase 1, Day 11 — Kubernetes's answer to "run this every night at 2am" but managed and observable through kubectl instead of a server's local crontab.
apiVersion: batch/v1
kind: CronJob
metadata:
name: nightly-backup
spec:
schedule: '0 2 * * *'
jobTemplate:
spec:
template:
spec:
containers:
- name: backup
image: myapp/backup:latest
restartPolicy: OnFailureKey terms
- Job
- Runs a Pod until successful completion, rather than keeping it running indefinitely.
- CronJob
- Creates Jobs on a cron schedule.
A Pod inside a Job exits with status 0 (success). What happens next, and how does this differ from a Deployment?