CronJob 总结


v1.21 stable

CronJob used for performing regular scheduled actions such as backups, report generation…

Schedule syntax

# ┌───────────── minute (0 - 59)
# │ ┌───────────── hour (0 - 23)
# │ │ ┌───────────── day of the month (1 - 31)
# │ │ │ ┌───────────── month (1 - 12)
# │ │ │ │ ┌───────────── day of the week (0 - 6) (Sunday to Saturday)
# │ │ │ │ │                                   OR sun, mon, tue, wed, thu, fri, sat
# │ │ │ │ │
# │ │ │ │ │
# * * * * *

Deadline for delayed Job start

spec.startingDeadlineSeconds

  • After missing the deadline, the CronJob skips that instance of the job (future occurrences are still scheduled)
  • For Jobs that miss their configured deadline, Kubernetes treats theme as failed Jobs. No startingDeadlineSeconds, no deadline for this CronJob.
  • If the spec.startingDeadlineSeconds field is set, the CronJobController measures the time between a Job is expected to be created and now. If the difference is higher than that limit, it will ship this execution.

注意:如果 startingDeadlineSeconds 的设置值低于 10 秒钟,CronJob 可能无法被调度。 这是因为 CronJob 控制器每 10 秒钟执行一次检查。

Concurrency policy

spec.concurrencyPolicy specifies how to treat concurrent executions of a Job that is created.

  • Allow (default): allow concurrently run.
  • Forbid: Do not allow concurrent runs.
  • Replace: old and new Job may run at the same time, the new one will replace the old one.

Schedule suspension

spec.suspend = true for this case. When spec.suspend changes from true to false on an existing CronJob without a spec.startingDeadlineSeconds, the missed Jobs are scheduled immediately.

Jobs history limits

spec.successfulJobsHistoryLimit = 3, spec.failedJobsHistoryLimit = 1. These fields specify how many completed and failed jobs should be kept.

Time zones (v1.27 stable)

spec.timeZone instructs Kubernetes to interpret the schedule relative to Coordinated Universal Time.

CronJob limitations

  • Unsupported TimeZone specification

    CRON_TZ or TZ is not officially supported, and starting with v1.29, Kubernetes will fail to create the resource with a validation error.

  • Modify a CronJob

    Modify an existing CronJob will have no effect to existing Jobs, and new Jobs will take effect.

  • Job creation

    The Jobs that user defined are idempotent. If the scheduled time from last time until now, the missed count > 100, then new Job will not be created from now.