Skip to content

Jobs & CronJobs

Run tasks to completion and schedule recurring work.

Time: ~10 minutes Difficulty: Beginner

  • Jobs: run-to-completion workloads (not long-running services)
  • Parallel jobs with completions and parallelism
  • Failure handling with backoffLimit and activeDeadlineSeconds
  • CronJobs: scheduled, recurring tasks
  • Concurrency policies and history limits

Navigate to the demo directory:

Terminal window
cd demos/jobs
Terminal window
kubectl apply -f manifests/namespace.yaml

Calculate 2,000 digits of pi and exit:

Terminal window
kubectl apply -f manifests/simple-job.yaml
kubectl get jobs -n jobs-demo -w

When the job shows 1/1 completions, check the result:

Terminal window
kubectl logs job/pi-calculator -n jobs-demo

Run 5 tasks with 2 workers at a time:

Terminal window
kubectl apply -f manifests/parallel-job.yaml
kubectl get pods -n jobs-demo -l job-name=batch-processor -w

Watch pods run in batches of 2 until all 5 completions are done:

Terminal window
kubectl get jobs batch-processor -n jobs-demo

A job that randomly fails, demonstrating retry behavior:

Terminal window
kubectl apply -f manifests/failing-job.yaml
kubectl get pods -n jobs-demo -l job-name=flaky-job -w

The job retries up to 3 times (backoffLimit: 3) and gives up after 60 seconds (activeDeadlineSeconds: 60). Check which attempts failed:

Terminal window
kubectl get pods -l job-name=flaky-job -n jobs-demo
kubectl logs -l job-name=flaky-job -n jobs-demo --prefix

Run a health report every 2 minutes:

Terminal window
kubectl apply -f manifests/cronjob.yaml
kubectl get cronjobs -n jobs-demo

Wait 2 minutes, then check the output:

Terminal window
kubectl get jobs -n jobs-demo -l job-name
kubectl logs -l job-name -n jobs-demo --prefix --tail=10
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 minutes

Key fields:

FieldMeaning
completionsTotal number of successful pod runs needed
parallelismMax pods running at the same time
backoffLimitMax retries before marking the job as failed
activeDeadlineSecondsHard timeout for the entire job
scheduleCron expression for CronJob timing
concurrencyPolicyForbid skips a run if the previous one is still active
successfulJobsHistoryLimitHow many completed Jobs to keep
  1. Trigger a CronJob run manually:

    Terminal window
    kubectl create job manual-report --from=cronjob/health-reporter -n jobs-demo
    kubectl logs job/manual-report -n jobs-demo
  2. Suspend a CronJob:

    Terminal window
    kubectl patch cronjob health-reporter -n jobs-demo -p '{"spec":{"suspend":true}}'
  3. Increase parallelism on the batch job:

    Terminal window
    kubectl delete job batch-processor -n jobs-demo
    # Edit parallel-job.yaml: parallelism: 5
    kubectl apply -f manifests/parallel-job.yaml
Terminal window
kubectl delete namespace jobs-demo

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.

Move on to DaemonSet to learn how to run a pod on every node.