failover.io
  1. Home
  2. Guides
  3. Kubernetes CronJob

Kubernetes CronJob

A working YAML, the fields that matter, the kubectl commands and why runs get missed or doubled.

Updated 19 November 20265 min read

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

FieldDefaultWhat it does
concurrencyPolicyAllowAllow 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.
startingDeadlineSecondsNo deadlineHow 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.
successfulJobsHistoryLimit3How many finished Jobs to keep, with their pods and logs.
failedJobsHistoryLimit1How many failed Jobs to keep. Raise it to keep the logs of more failures.
suspendfalsetrue stops new runs. A Job already running keeps going.
backoffLimit (in the Job)6How many times a failing pod is retried before the Job counts as failed.
activeDeadlineSeconds (in the Job)NoneKills 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

TaskCommand
Last run and schedulekubectl get cronjob backup
Jobs it createdkubectl get jobs
Logs of one runkubectl logs job/<job-name>
Events and errorskubectl describe cronjob backup
Run it nowkubectl create job --from=cronjob/backup backup-manual
Pause itkubectl 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. Use Forbid for jobs that must not run in parallel.
  • If more than 100 runs were missed, for example after the controller was down, Kubernetes records a TooManyMissedTimes warning 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 describe on the Job and its pod shows why.
  • Failed Jobs beyond failedJobsHistoryLimit are 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.