Batch Work That Terminates
Batch Work That Terminates
Deployments model endless servers; Jobs model work that finishes. The Job controller creates pods until the desired completions succeed, retrying failures up to backoffLimit (default 6).
apiVersion: batch/v1
kind: Job
metadata: { name: nightly-report }
spec:
backoffLimit: 4
activeDeadlineSeconds: 600
ttlSecondsAfterFinished: 86400
template:
spec:
restartPolicy: Never
Jobs allow only Never or OnFailure restart policies. activeDeadlineSeconds kills the whole Job when wall-clock time runs out, and ttlSecondsAfterFinished garbage-collects finished Jobs. Because a pod may be recreated after a node dies mid-task, downstream effects must be idempotent — the contract is at-least-once.
Scheduling with CronJobs
Scheduling with CronJobs
apiVersion: batch/v1
kind: CronJob
metadata: { name: db-backup }
spec:
schedule: "17 3 * * *"
timeZone: "Etc/UTC"
concurrencyPolicy: Forbid
startingDeadlineSeconds: 300
successfulJobsHistoryLimit: 3
failedJobsHistoryLimit: 1
jobTemplate:
spec:
backoffLimit: 2
template:
spec:
restartPolicy: OnFailure
Fields are minute, hour, day of month, month, day of week. The controller creates a Job per schedule; a missed start still fires if it arrives within startingDeadlineSeconds. timeZone (stable since 1.27) pins interpretation — otherwise the kube-controller-manager's local zone applies.
Pitfalls and Interview Angles
Pitfalls and Interview Angles
Pitfall 1 — overlapping runs. The default concurrencyPolicy: Allow lets a slow run collide with the next schedule, double-writing results. Use Forbid for locks or Replace when only the newest run matters.
Pitfall 2 — assuming exactly-once. A Job retries after node failure; write effects that tolerate duplicates (idempotent upserts, unique run IDs), externalize state, and watch activeDeadlineSeconds so zombie runs cannot linger.
Interview-ready: distinguish Job from CronJob (one-shot controller vs scheduler of Jobs), explain each concurrency policy, and describe how you verify a run: kubectl create job --from=cronjob/db-backup manual-1 for a dry rehearsal, then check logs and status.succeeded.
Quiz
1. Which restart policies are allowed in a Job pod?
2. What does concurrencyPolicy: Forbid do in a CronJob?
3. Which field garbage-collects a finished Job automatically?
Flashcards
Question
Job vs Deployment
Click to reveal answer
Answer
Job runs to completion with retries; Deployment keeps replicas alive indefinitely.
Question
backoffLimit
Click to reveal answer
Answer
Maximum pod retries before the Job is marked Failed (default 6).
Question
Cron fields
Click to reveal answer
Answer
minute hour day-of-month month day-of-week — five fields, evaluated in spec.timeZone.
Question
Idempotency requirement
Click to reveal answer
Answer
Jobs are at-least-once; design effects to tolerate duplicate executions.
Revision Notes
Key Takeaways
- 1. Batch = finite pods with backoffLimit and deadline fields
- 2. CronJobs schedule Jobs; concurrencyPolicy controls overlap
- 3. At-least-once means idempotent writes or unique run identifiers
Interview Tips
- • Rehearse manually with kubectl create job --from=cronjob before trusting the schedule
- • Explain which concurrency policy fits a backup job versus a metric push
Cheat Sheet
Job: backoffLimit, activeDeadlineSeconds, ttlSecondsAfterFinished, restartPolicy Never/OnFailure. CronJob: five-field schedule + timeZone + concurrencyPolicy.