A CronJob is Kubernetes’ answer to “I want this to run on a schedule.” Under the hood, the CronJob controller creates a Job at each scheduled tick, and the Job creates a Pod. The contract is straightforward, but the defaults are surprising: concurrency is allowed, history piles up, time zones default to the kube-controller-manager’s, and missed schedules can stampede when the cluster wakes back up. This article walks the working pattern.
How to verify
What CronJobs exist and what they’ve produced:
kubectl get cronjob -A
kubectl get jobs -A --sort-by=.metadata.creationTimestamp | tail
kubectl get cronjob nightly-backup -n ops -o yaml | yq '.spec | {schedule,concurrencyPolicy,startingDeadlineSeconds,successfulJobsHistoryLimit,failedJobsHistoryLimit,timeZone}'
The CronJob’s status shows the last and next scheduled times:
kubectl get cronjob nightly-backup -n ops -o jsonpath='{.status.lastScheduleTime}{"\n"}{.status.lastSuccessfulTime}{"\n"}'
For a specific Job that ran, the logs are on its Pod (CronJob does not aggregate them):
kubectl logs job/nightly-backup-28548320 -n ops
What’s happening
A CronJob is a controller that materializes Jobs based on a cron schedule. When the scheduled time comes, the controller creates a Job; the Job creates a Pod; the Pod runs to completion. The CronJob object owns the Jobs (ownerReference); the Job owns its Pods.
Three behaviour controls dominate operation:
concurrencyPolicy:Allow(default — overlapping runs are fine),Forbid(skip the new run if the previous is still going), orReplace(kill the previous, start the new). PickForbidfor anything that touches shared state.startingDeadlineSeconds: if the controller missed the schedule (cluster was down, controller paused, scheduler stalled), should it still try to run if the window has passed? Default is unset = “always try”; setting it to, say,600means “if I’m more than 10 minutes late, skip this tick.”successfulJobsHistoryLimit(default 3) andfailedJobsHistoryLimit(default 1): how many completed/failed Jobs to keep around for inspection.
timeZone (autoscaling/v1 beta and GA from Kubernetes 1.27) lets you write the schedule in a sensible zone instead of UTC. Without it, the schedule is interpreted in the kube-controller-manager’s local timezone — which is usually UTC on managed Kubernetes.
The Job itself has a backoffLimit (default 6) for retries on failure, and an activeDeadlineSeconds to put a hard ceiling on how long the Pod can run before being killed.
The procedure
-
A nightly backup CronJob — Forbid concurrency, time zone explicit, history limits set, and a sensible deadline:
apiVersion: batch/v1 kind: CronJob metadata: name: nightly-backup namespace: ops spec: schedule: "0 2 * * *" timeZone: "America/Toronto" concurrencyPolicy: Forbid startingDeadlineSeconds: 600 successfulJobsHistoryLimit: 7 failedJobsHistoryLimit: 3 jobTemplate: spec: backoffLimit: 2 activeDeadlineSeconds: 3600 template: spec: restartPolicy: OnFailure serviceAccountName: backup-runner containers: - name: dump image: registry.example.com/backup:1.4.0 command: ["/usr/local/bin/run-backup"] env: - name: BACKUP_TARGET value: "s3://prod-backups/postgres" resources: requests: { cpu: "200m", memory: "256Mi" } limits: { memory: "512Mi" } -
Suspend a CronJob without deleting it — useful during maintenance windows:
kubectl patch cronjob nightly-backup -n ops \ --type=merge -p '{"spec":{"suspend":true}}' -
Trigger a one-off run of the same template — for testing, or to re-run after a fix:
kubectl create job -n ops nightly-backup-manual-$(date +%s) \ --from=cronjob/nightly-backup -
Inspect history — successful and failed Jobs (within the configured limits):
kubectl get job -n ops -l 'cronjob-name=nightly-backup' kubectl logs -n ops job/nightly-backup-28548320 --previous 2>/dev/null \ || kubectl logs -n ops job/nightly-backup-28548320 -
Tighten history when you’re keeping logs elsewhere — keep 1 success and 3 failures (failures matter more than successes for diagnosis):
successfulJobsHistoryLimit: 1 failedJobsHistoryLimit: 3 -
Idempotent jobs only. Schedule-driven jobs should be safe to run twice — once because of
concurrencyPolicy: Allow, and once because of clock skew on the controller. Lock at the storage layer (a row in Postgres, a marker file in S3) rather than relying on Kubernetes to never double-fire.
Common pitfalls
- The default
concurrencyPolicy: Allowlets a backup that takes 90 minutes overlap with the next one — pickForbidfor anything that writes to shared state. startingDeadlineSecondsunset means “always catch up” — after a controller outage, every missed tick is fired immediately; set a deadline.- Without
timeZone, “0 2 * * *” runs at 2 a.m. UTC, which is the middle of the afternoon in Auckland and late evening in Toronto — set the zone explicitly. - The default
failedJobsHistoryLimit: 1means you only ever see one failure before it’s garbage-collected; raise it to 3-5 if you’re not shipping Pod logs to a log aggregator. - A CronJob with
concurrencyPolicy: Forbidand a long-running Job that hangs will silently skip every subsequent tick — alert onlastSuccessfulTimefalling behind the schedule.
We operate scheduled work for clients with Forbid concurrency, time zones explicit, deadlines on every Job, and alerts that fire when the last successful run drifts past the schedule. See /en/services/managed-operations/.