Platforms

Cron in Kubernetes

Kubernetes CronJobs create Jobs on a schedule. The schedule field uses cron syntax and the controller manages job creation.

CronJob YAML example

apiVersion: batch/v1
kind: CronJob
metadata:
  name: nightly-report
spec:
  schedule: "0 2 * * *"
  concurrencyPolicy: Forbid
  startingDeadlineSeconds: 300
  successfulJobsHistoryLimit: 3
  failedJobsHistoryLimit: 1
  jobTemplate:
    spec:
      template:
        spec:
          restartPolicy: OnFailure
          containers:
            - name: report
              image: example/report:latest

Important fields

  • schedule defines when Jobs are created.
  • concurrencyPolicy controls overlapping runs.
  • successfulJobsHistoryLimit controls retained successful Jobs.
  • failedJobsHistoryLimit controls retained failed Jobs.
  • startingDeadlineSeconds allows late starts within a deadline.

Common mistakes

  • Leaving concurrencyPolicy unset for non-idempotent jobs.
  • Keeping too many old Jobs.
  • Forgetting cluster timezone and controller behavior.

FAQ

Does Kubernetes CronJob use Linux cron syntax?

It uses a cron-like 5-field schedule for the schedule property.

How do I avoid overlapping Kubernetes CronJobs?

Set concurrencyPolicy to Forbid for jobs that should not overlap.