Learning objectives
By the end of this module, you should be able to:
- Use a Job for run-to-completion work.
- Understand how a CronJob creates Jobs on a schedule.
- Inspect completion and logs for batch workloads.
Jobs target completion
A Job creates one or more Pods and tracks successful completion. This is better suited to migrations, reports and finite processing than a Deployment whose controller tries to keep Pods continuously running.
CronJobs schedule Jobs
A CronJob creates Jobs according to a cron schedule. Scheduling is approximate and applications should be designed to tolerate delayed or duplicate execution according to the job’s business semantics.
History needs management
CronJob history limits and TTL-after-finished settings help keep completed objects from accumulating indefinitely.
Worked examples
See the idea in practice.
Create a one-shot Job
kubectl -n gnu-k8s-fundamentals create job hello-job --image=busybox:1.36 -- sh -c 'echo batch-complete'
kubectl -n gnu-k8s-fundamentals wait --for=condition=complete job/hello-job --timeout=90s
- The Job records completion.
- Logs come from its created Pod.
Hands-on lab
Lab — Run and schedule batch work
Goal: Complete a Job and create a CronJob, then manually instantiate a Job from the CronJob template so you do not need to wait for the clock.
Before you start
- kubectl is installed and can reach the lab cluster.
- Use the dedicated gnu-k8s-fundamentals namespace unless a step explicitly says otherwise.
STEP 1Run a Job
kubectl -n gnu-k8s-fundamentals create job batch-once --image=busybox:1.36 -- sh -c 'echo batch-complete'
kubectl -n gnu-k8s-fundamentals wait --for=condition=complete job/batch-once --timeout=90s
kubectl -n gnu-k8s-fundamentals logs job/batch-once
STEP 2Create a CronJob
kubectl -n gnu-k8s-fundamentals create cronjob batch-schedule --image=busybox:1.36 --schedule="*/15 * * * *" -- sh -c 'echo scheduled-run'
STEP 3Create a Job from the CronJob template now
kubectl -n gnu-k8s-fundamentals create job --from=cronjob/batch-schedule batch-manual
kubectl -n gnu-k8s-fundamentals wait --for=condition=complete job/batch-manual --timeout=90s
kubectl -n gnu-k8s-fundamentals logs job/batch-manual
STEP 4Clean up
kubectl -n gnu-k8s-fundamentals delete job batch-once batch-manual
kubectl -n gnu-k8s-fundamentals delete cronjob batch-schedule
Verify
- Both Jobs complete successfully.
- The CronJob exists with the requested schedule.
Expected outcome
- Logs show batch-complete and scheduled-run.
If it fails
- If a Job retries, inspect Pod termination reasons and Job backoff settings.
- Do not wait for a long cron interval in a lab; instantiate a Job from the CronJob template.
Real-world connectionBackups, reports, migrations and scheduled housekeeping are common batch-workload patterns.
Avoid these traps
Common mistakes
- Using a Deployment for a task that should finish and stay finished.
- Assuming scheduled tasks can never overlap or be duplicated.
- Leaving unlimited completed Job history in long-lived namespaces.
Knowledge check
Can you explain it without looking back?
Open each answer only after you have tried to answer the question yourself.
1What Kubernetes controller is designed for run-to-completion work?
Job.
2What does a CronJob create?
Jobs according to its schedule.
3Why should recurring work be idempotent when practical?
Schedules and retries can produce delayed, repeated or overlapping execution depending on conditions and policy.