Jobs & CronJobs
Run tasks to completion and schedule recurring work.
Time: ~10 minutes Difficulty: Beginner
What You Will Learn
Section titled “What You Will Learn”- Jobs: run-to-completion workloads (not long-running services)
- Parallel jobs with
completionsandparallelism - Failure handling with
backoffLimitandactiveDeadlineSeconds - CronJobs: scheduled, recurring tasks
- Concurrency policies and history limits
Deploy
Section titled “Deploy”Navigate to the demo directory:
cd demos/jobskubectl apply -f manifests/namespace.yamlSimple Job
Section titled “Simple Job”Calculate 2,000 digits of pi and exit:
kubectl apply -f manifests/simple-job.yamlkubectl get jobs -n jobs-demo -wWhen the job shows 1/1 completions, check the result:
kubectl logs job/pi-calculator -n jobs-demoParallel Job
Section titled “Parallel Job”Run 5 tasks with 2 workers at a time:
kubectl apply -f manifests/parallel-job.yamlkubectl get pods -n jobs-demo -l job-name=batch-processor -wWatch pods run in batches of 2 until all 5 completions are done:
kubectl get jobs batch-processor -n jobs-demoFailing Job
Section titled “Failing Job”A job that randomly fails, demonstrating retry behavior:
kubectl apply -f manifests/failing-job.yamlkubectl get pods -n jobs-demo -l job-name=flaky-job -wThe job retries up to 3 times (backoffLimit: 3) and gives up after 60 seconds (activeDeadlineSeconds: 60). Check which attempts failed:
kubectl get pods -l job-name=flaky-job -n jobs-demokubectl logs -l job-name=flaky-job -n jobs-demo --prefixCronJob
Section titled “CronJob”Run a health report every 2 minutes:
kubectl apply -f manifests/cronjob.yamlkubectl get cronjobs -n jobs-demoWait 2 minutes, then check the output:
kubectl get jobs -n jobs-demo -l job-namekubectl logs -l job-name -n jobs-demo --prefix --tail=10What is Happening
Section titled “What is Happening”manifests/ namespace.yaml # jobs-demo namespace simple-job.yaml # Single-pod compute task (pi digits) parallel-job.yaml # 5 completions, 2 workers at a time failing-job.yaml # Random failures with backoff and deadline cronjob.yaml # Health report every 2 minutesKey fields:
| Field | Meaning |
|---|---|
completions | Total number of successful pod runs needed |
parallelism | Max pods running at the same time |
backoffLimit | Max retries before marking the job as failed |
activeDeadlineSeconds | Hard timeout for the entire job |
schedule | Cron expression for CronJob timing |
concurrencyPolicy | Forbid skips a run if the previous one is still active |
successfulJobsHistoryLimit | How many completed Jobs to keep |
Experiment
Section titled “Experiment”-
Trigger a CronJob run manually:
Terminal window kubectl create job manual-report --from=cronjob/health-reporter -n jobs-demokubectl logs job/manual-report -n jobs-demo -
Suspend a CronJob:
Terminal window kubectl patch cronjob health-reporter -n jobs-demo -p '{"spec":{"suspend":true}}' -
Increase parallelism on the batch job:
Terminal window kubectl delete job batch-processor -n jobs-demo# Edit parallel-job.yaml: parallelism: 5kubectl apply -f manifests/parallel-job.yaml
Cleanup
Section titled “Cleanup”kubectl delete namespace jobs-demoFurther Reading
Section titled “Further Reading”See docs/deep-dive.md for a detailed explanation of Job controller internals, indexed jobs, TTL-after-finished, CronJob timezone handling, and real-world patterns like fan-out processing.
Next Step
Section titled “Next Step”Move on to DaemonSet to learn how to run a pod on every node.