The time-based scheduler for Kubernetes batch workloads. A CronJob wraps a Job with a cron expression, creating a new Job (and therefore new Pods) on every schedule tick. Synthesized from CKA Day 12 — DaemonSet, Job & CronJob Explained.
What is a CronJob?
A CronJob is a Kubernetes controller that creates Jobs on a recurring schedule. It is the cluster-native equivalent of crontab — but with the full power of Kubernetes resource management, fault tolerance, and observability.
Every time the schedule triggers, the CronJob controller spawns a new Job object. That Job creates Pods, waits for them to complete, and then reports status. Because each scheduled run is an independent Job, a failed run does not block the next scheduled execution.
Key Insight: CronJob → Job → Pod. The CronJob never creates Pods directly. It is a meta-controller that manages Job lifecycles over time.
Multiple Jobs from the same CronJob can run simultaneously
Independent, idempotent tasks
Forbid
If a Job is still running, the next scheduled start is skipped
Tasks that must not overlap
Replace
If a Job is still running, it is terminated and a new one starts
Always need the latest data
Managing History and Cleanup
By default, Kubernetes keeps 3 successful Jobs and 1 failed Job. Older Jobs are automatically deleted. You can adjust these limits or use a ttlSecondsAfterFinished field inside the jobTemplate to delete individual Jobs (and their Pods) after a set time.
Workloads & Scheduling (~15%): Create a CronJob with a given schedule. Be precise with the cron expression.
Troubleshooting (~30%): If a CronJob missed a run, check startingDeadlineSeconds (was the controller down too long?), concurrencyPolicy (was it Forbid and a previous Job still running?), and suspend status.
Exam-style hands-on tasks for this topic. Complete each task before reviewing the solution. Time yourself — CKA tasks average 5–7 minutes.
Task 1: Create a Daily CronJob
You are asked to create a CronJob named db-backup that runs echo "backup done" every day at 2:30 AM.
Requirements: Use --dry-run=client -o yaml, save to a file, and apply. Use the correct 5-field cron expression.
Verification:kubectl get cronjob db-backup shows the schedule 30 2 * * *.
Solution:
Task 2: Set Job History Limits
You are asked to set successfulJobsHistoryLimit: 3 and failedJobsHistoryLimit: 2 on an existing CronJob named db-backup.
Requirements: Use a single patch command; verify the limits are updated.
Verification:kubectl get cronjob db-backup -o jsonpath='{.spec.successfulJobsHistoryLimit}' returns 3.
Solution:
Task 3: Troubleshoot a Non-Running CronJob
A CronJob named report-gen is not creating Jobs. Verify the schedule expression and startingDeadlineSeconds, then fix any issue.
Requirements: Check kubectl describe cronjob report-gen for last schedule time and any warnings. Fix the schedule or deadlines.
Verification:kubectl get jobs shows a new Job created by the CronJob.
Solution:
kubectl describe cronjob report-gen# Check that the schedule expression is valid and startingDeadlineSeconds is not too shortkubectl patch cronjob report-gen -p '{"spec":{"startingDeadlineSeconds":300}}'# Or fix the schedule expression if it is malformed