App
Jobs and CronJobs
Run to completion
A Pod that is supposed to exit: completions, retries and schedules. Everything so far assumed a Pod that runs forever - a Job flips that, and exit 0 becomes the success condition. Get the query wrong and the controller retries on a doubling clock until backoffLimit gives up; wrap the Job in a CronJob and a controller manufactures one per tick, on a schedule.
- Job
- report · completions 1 · parallelism 1
- Retries
- backoffLimit 5 · restartPolicy Never
- Cron
- 0 3 * * * · UTC · concurrencyPolicy Forbid
- Cleanup
- history limits · ttlSecondsAfterFinished 3600
Read the whole lesson as text
1. The deal
A Deployment's Pod that exits has failed; a Job's Pod that exits 0 has succeeded - that's the whole inversion. The spec says so: completions 1 means one success is enough, parallelism 1 means one at a time, backoffLimit 5 allows five retries after the first try, and restartPolicy Never is the load-bearing field. Never matters because a crashing container must not be restarted in place: retries belong to the Job controller, which counts them. activeDeadlineSeconds 600 caps the whole run.
- kubectl posts the Job: one Pod, one success, up to five retries.
- The Job controller hears about it: one completion wanted, none reached.
- It creates exactly one Pod. Nobody promises to keep it alive.
2. Run once
Zoom into node-1. The kubelet starts the container like any other - a Job Pod is still a Pod - and the report runs its SQL for 42 seconds. Then the process exits 0. For an ordinary Pod that would be a crash; here the kubelet checks restartPolicy, sees Never, and does nothing but report upward. The container just sits there, finished, its logs still readable.
- node-1's kubelet sees the Pod assigned to it.
- containerd starts it; for 42 seconds it's an ordinary Pod.
- The report finishes and exits 0. Success, not a crash.
- The kubelet reports the Pod Succeeded - and restarts nothing.
3. Job complete
The Job controller watches the Pod object, sees Succeeded, and updates its own scoreboard: succeeded 1 of 1, condition Complete, 43.3 seconds after the start. The Job and the finished Pod both stay put - a succeeded Pod is evidence, and its logs stay readable - until the TTL cleans up an hour later.
- The Job controller hears the Pod finished successfully.
- It marks the Job Complete: one success of one wanted.
4. When it fails
Three weeks later the 03:00 run ships a typo in the SQL, and the Pod dies in 3 seconds with exit 1. restartPolicy Never means the kubelet leaves the Failed Pod alone, and the Job controller makes a brand-new Pod after a 10-second backoff. The backoff doubles with every failure - 10, 20, 40, 80, 160 s - and the sixth failure takes the count past backoffLimit, so the Job itself is declared Failed. The 600-second deadline never even came into it.
- A new Job is created for the 03:00 run - this one carries the broken query.
- The bad SQL kills the Pod in three seconds.
- The Pod is Failed and stays that way - Never forbids an in-place restart.
- Ten seconds later the Job controller creates a fresh Pod for attempt two.
- Attempts 3 to 6 each exit 1 on the doubling clock.
- Six failures pass backoffLimit 5, so the Job itself is marked Failed.
5. On a clock
Where did that 03:00 Job come from? The CronJob controller works out when schedule 0 3 * * * next fires - every day at 03:00, UTC unless timeZone says otherwise - sleeps until then, and creates a Job from its template, named after the tick's time in minutes since the epoch. concurrencyPolicy Forbid skips a tick if the previous Job is still running; Replace would delete it and start fresh; Allow would run both. startingDeadlineSeconds decides how long a missed tick still counts after an outage.
- The controller wakes at the time it computed for the next tick.
- The tick gets its own Job, stamped from the template.
- If a run is still going when the next tick comes, Forbid skips that tick instead of starting a second one.
6. Cleaning up
A Job a day is 365 objects a year, so something prunes. The CronJob controller keeps successfulJobsHistoryLimit 3 and failedJobsHistoryLimit 1, deleting older Jobs - and a Job delete cascades to its Pods. The modern lever is ttlSecondsAfterFinished on the Job itself: the first run set 3600, so an hour after it completed, the TTL controller removes it and its Pod. One caveat runs the other way: without activeDeadlineSeconds, a hung Job runs forever.
- The oldest success is past the limit of 3, so the CronJob controller deletes it.
- An hour after completing, the first Job hits its TTL and goes, Pod and all.
Summary
A Job inverts the deal: the Pod's purpose is to exit, and the controller counts outcomes instead of keeping replicas alive. Failure is the same machinery seen from the bad side - new Pods on a doubling clock until backoffLimit calls it. A CronJob adds only a clock: every tick, a fresh Job from the same template, with history limits to keep the pile from growing.
Next lesson: RBAC and ServiceAccounts