Sub topic 6
CronJobs: Jobs on a Schedule
This chapter assumes you can write and apply a Job manifest, and that you know how restartPolicy, backoffLimit, and ttlSecondsAfterFinished behave in a Job. A CronJob adds only one thing to a Job: a clock.
What a CronJob does
A CronJob is a Kubernetes object that creates Job objects on a repeating schedule. The CronJob does not run containers itself. Each time the schedule fires, the CronJob controller (part of the control plane) creates a new Job from a template. That Job then creates Pods and handles retries as usual.
The result is three levels of objects:
Because each run is a separate Job, one run's logs, status, and failures stay separate from the others.
A first CronJob
apiVersion: batch/v1
kind: CronJob
metadata:
name: nightly-report
spec:
schedule: "30 2 * * *"
jobTemplate:
spec:
backoffLimit: 2
template:
spec:
restartPolicy: OnFailure
containers:
- name: report
image: busybox:1.36
command: ["sh", "-c", "date; echo generating report"]The manifest has two key fields:
spec.scheduleis a standard five-field cron expression: minute, hour, day of month, month, day of week.30 2 * * *means 02:30 every day. The next chapter covers the syntax and time zones.spec.jobTemplateis a Job spec. Everything you know about Jobs applies inside it, includingbackoffLimit,completions, andttlSecondsAfterFinished.
Note the nesting. The Pod template sits under jobTemplate.spec.template, one level deeper than in a plain Job. Misplaced indentation is the most common mistake when converting a Job into a CronJob.
Names and ownership
The controller names each Job <cronjob-name>-<number>, where the number is the scheduled time in minutes since the Unix epoch. Pods in turn get the Job name plus a random suffix. Each Job has the CronJob as its owner. Deleting the CronJob therefore deletes its Jobs and their Pods.
Working with a CronJob
kubectl apply -f cronjob.yaml
kubectl get cronjob nightly-report
kubectl get jobs
kubectl create job nightly-report-manual --from=cronjob/nightly-reportkubectl get cronjob shows the schedule, whether it is suspended, how many Jobs are active, and the time of the last run. The create job --from command copies the job template into a new one-off Job. Use it to test a CronJob without waiting for the schedule. The manual Job is not tied to the schedule, so it does not interfere with scheduled runs.
What the controller guarantees
Scheduling is at-least-once in intent but not exact in practice. A run can start a little late. It can be skipped if the control plane was down or if the previous run is still going and the overlap policy forbids a second one. In rare cases, two Jobs can be created for the same tick. The settings that control these cases are covered in later chapters:
| Concern | Setting |
|---|---|
| Overlapping runs | `concurrencyPolicy` |
| Late or missed runs | `startingDeadlineSeconds` |
| Old Jobs piling up | `successfulJobsHistoryLimit`, `failedJobsHistoryLimit` |
| Pausing the schedule | `suspend` |
Design the task itself so that a skipped or duplicated run does no harm.
Exercise
You apply a CronJob with schedule "0 " at 10:20. When does the first Job appear, and which field of the manifest holds the Pod's containers?
Show answer
The first Job appears at 11:00, the next time the schedule matches, not at 10:20. The containers are under spec.jobTemplate.spec.template.spec.containers.