Kubernetes · Chapter 17 of 28
Jobs and CronJobs
By the end of this chapter you can run work that must finish (a Job), choose how many pods it needs and how often it may fail, find out why a Job failed, run work on a schedule (a CronJob), decide what happens when runs overlap, and run a scheduled job right now by hand. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: More workloads and traffic
- 4 tasks
- Kubernetes
- In the all labs plan
The first 2 chapters of Kubernetes are free. This one comes with the all labs plan.
Why you need this
Every night Jhatpat makes the invoices for the day before: about 1,200 PDFs, which customers download and the accountants send to GST filing by 7 am. On a festival night it is four times that. Today Tushar runs a script on his laptop at 2 am, when he remembers; twice in October he did not, and the accountants waited. Every dark store also has to send its stock counts to the catalog every 5 minutes, or the app sells milk the Aliganj store has run out of.
Everything you have run so far is meant to run forever. A Deployment whose pod finishes its work and exits starts it again (chapter 3's CrashLoopBackOff); there is no "done". Nothing you know yet runs a task to the end, retries it if it fails, and stops; and nothing starts one at 2 am every night.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
invoices-oct25.yaml: yesterday's invoices
Run the Job that makes yesterday's invoices, once, until it finishes.
1. Open invoices-oct25.yaml. Add the env var BATCH_DATE with the value "2026-10-25" (in quotes) to the container.
2. Keep the image jhatpat/invoice-batch:1.1 and backoffLimit 2. Do not add ttlSecondsAfterFinished.
3. Apply the file. Wait until the Job is Complete (about 6 seconds).
4. Read the Job's logs. The last line names the PDF it wrote.
To pass: the Job invoices-oct25 in ch17 is Complete, and its pod's logs say it wrote invoices-2026-10-25.pdf. A Job cannot be changed after it is made: if you got it wrong, delete the Job and apply the file again.
- Task 2
invoices-oct24.yaml: fix Tushar's failing Job
Tushar's Job invoices-oct24 keeps failing. Find out why and run it again.
1. List the Job's pods. Read a failed pod's logs with kubectl logs.
2. Fix invoices-oct24.yaml: add BATCH_DATE with the value "2026-10-24". Keep backoffLimit 3.
3. Delete the old Job invoices-oct24.
4. Apply the fixed file. Wait until the Job is Complete.
To pass: you ran kubectl logs in the terminal. The Job invoices-oct24 is new (made after the chapter started), has BATCH_DATE 2026-10-24 and backoffLimit 3, and is Complete. You must delete the old Job first: it cannot be changed in place.
- Task 3
stock-sync.yaml: every 5 minutes, and once now
Run the stock sync every 5 minutes, and once now by hand.
1. Open stock-sync.yaml. Make the CronJob stock-sync with the schedule /5 * * * and timeZone Asia/Kolkata.
2. Set concurrencyPolicy Forbid and successfulJobsHistoryLimit 3.
3. Its pod runs jhatpat/stock-sync:1.0 with the env var STORE set to LKO-ALIGANJ-2. Apply the file.
4. Make one Job from it now with kubectl create job --from=cronjob/stock-sync and any name. Wait until it is Complete.
To pass: the CronJob stock-sync in ch17 has all these values and is not suspended. A Job made by hand from it, after your STORE change, is Complete.
- Task 4 · interview question
Interview: a run longer than its interval
Interview question: a CronJob run is still going when the next scheduled time comes. What happens?
1. Open answer.txt. Write below the lines that start with #.
2. In two or three sentences of your own, say what happens for each concurrencyPolicy.
3. Name all three: Allow, Forbid and Replace.
To pass: your answer names Allow, Forbid and Replace, and says what each one does with the old run and the new run. Lines that start with # do not count. Ammar will ask: "The 9:00 stock sync is still running at 9:05. What does the CronJob do?"
What you will learn
- Jobs: pods that run to completion, retried by the Job controller.
- completions and parallelism: how many must finish, how many at once.
- backoffLimit, activeDeadlineSeconds, ttlSecondsAfterFinished: when to give up, and when to clean up.
- Why a Job failed: its pods' logs and its events.
- CronJobs: schedule, timeZone, concurrencyPolicy, history limits, suspend.
- kubectl create job --from=cronjob/...: run a scheduled job now.
More in More workloads and traffic
Build "Jobs and CronJobs" yourself
Jhatpat delivers groceries in ten minutes from 14 dark stores across Lucknow, and its hand-run servers fall over on festival evenings. You move it onto Kubernetes. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Kubernetes are free. This one comes with the all labs plan.