A Kubernetes CronJob creates a Job on a schedule, and the Job runs a pod until the command finishes. The schedule uses the same five fields as crontab. This one runs a backup every day at 3:00 UTC:
apiVersion: batch/v1
kind: CronJob
metadata:
name: backup
spec:
schedule: "0 3 * * *"
timeZone: "Etc/UTC"
concurrencyPolicy: Forbid
jobTemplate:
spec:
backoffLimit: 2
template:
spec:
containers:
- name: backup
image: registry.example.com/backup:1.4.0
command: ["/app/backup.sh"]
restartPolicy: OnFailure
Apply it with kubectl apply -f backup.yaml.
Schedule and time zone
schedule takes a five field cron expression, like */15 * * * * or 0 9 * * 1-5, or a macro: @hourly, @daily, @weekly, @monthly, @yearly. A ? means the same as *. Field by field: crontab syntax. To build one, use the cron expression generator.
Without timeZone, the schedule follows the time zone of the kube-controller-manager. Set timeZone to a name like Europe/Paris to pin it; the field is stable since Kubernetes 1.27. Writing CRON_TZ= or TZ= inside schedule is rejected with a validation error.
The fields that matter
| Field | Default | What it does |
|---|---|---|
concurrencyPolicy | Allow | Allow runs a new Job even if the last one is still running. Forbid skips the new run. Replace stops the old Job and starts the new one. |
startingDeadlineSeconds | No deadline | How late a run may still start, for example after the controller was down. Later than that, the run is skipped. Keep it at 10 or more: the controller checks every 10 seconds. |
successfulJobsHistoryLimit | 3 | How many finished Jobs to keep, with their pods and logs. |
failedJobsHistoryLimit | 1 | How many failed Jobs to keep. Raise it to keep the logs of more failures. |
suspend | false | true stops new runs. A Job already running keeps going. |
backoffLimit (in the Job) | 6 | How many times a failing pod is retried before the Job counts as failed. |
activeDeadlineSeconds (in the Job) | None | Kills the Job when it runs longer than this. |
restartPolicy in the pod must be OnFailure or Never. The CronJob's name can be 52 characters at most, because Kubernetes adds 11 characters to name each Job.
Check, run and pause it
| Task | Command |
|---|---|
| Last run and schedule | kubectl get cronjob backup |
| Jobs it created | kubectl get jobs |
| Logs of one run | kubectl logs job/<job-name> |
| Events and errors | kubectl describe cronjob backup |
| Run it now | kubectl create job --from=cronjob/backup backup-manual |
| Pause it | kubectl patch cronjob backup -p '{"spec":{"suspend":true}}' |
Why a CronJob runs twice, late or not at all
- Kubernetes creates a Job about once per scheduled time. In rare cases it creates two, or none, so write jobs that are safe to run twice.
- With the default
Allow, a slow run overlaps the next one. UseForbidfor jobs that must not run in parallel. - If more than 100 runs were missed, for example after the controller was down, Kubernetes records a
TooManyMissedTimeswarning and starts only the most recent one. - A pod that can't be scheduled, an image that can't be pulled or a node that runs out of memory all fail the run.
kubectl describeon the Job and its pod shows why. - Failed Jobs beyond
failedJobsHistoryLimitare deleted with their logs. Send logs to your log system if you need them later.
Get an alert when it stops
A CronJob that fails or never runs tells nobody: the cluster records it, and that's it. Ping a heartbeat URL at the end of a successful run, and you get an alert when a ping is late. If the image has curl:
command: ["/bin/sh", "-c", "/app/backup.sh && curl -fsS -m 10 -X POST https://api.failover.io/heartbeat/<your-monitor-id>"]
On a busybox based image, wget does the same:
command: ["/bin/sh", "-c", "/app/backup.sh && wget -q -O /dev/null --post-data='' https://api.failover.io/heartbeat/<your-monitor-id>"]
The && sends the ping only when the script exits with 0. Set the heartbeat interval to the schedule, with some grace for the run time. See how to monitor cron jobs.
Frequently asked questions
How do I run a Kubernetes CronJob manually?
Create a Job from it: kubectl create job --from=cronjob/backup backup-manual. It uses the same Job template and does not change the schedule.
What time zone does a Kubernetes CronJob use?
The time zone of the kube-controller-manager, unless you set spec.timeZone, for example Europe/Paris. The field is stable since Kubernetes 1.27.
How do I stop a CronJob from running twice at the same time?
Set concurrencyPolicy: Forbid. The next run is skipped while the previous one is still going.
How do I pause a CronJob?
Set spec.suspend to true, for example with kubectl patch cronjob backup -p '{"spec":{"suspend":true}}'. Running Jobs are not stopped.
Why is my CronJob not running?
Check kubectl describe cronjob for events: a suspended CronJob, an invalid schedule, a missed startingDeadlineSeconds, or pods that fail to start are the usual causes.