K8s工作负载-CronJob

1.25

什么是CronJob

CronJob按照预定时间计划创建Job

CronJob的限制

CronJob在时间计划中执行,但是Job依旧还可能失败,所以Job不是唯一的,因此Job必须是幂等的,当满足以下俩个条件,Job至少运行一次

  • StartingDeadlineSeconds:设置为一个较大的值,或者不设置该值(有默认值)
  • ConcurrencyPolicy:设置为Allow
    • 如果设置了StaringDeadlineSeconds,CronJob控制器按照从StaringDeadlineSeconds之前到现在为止的时间段计算错过的执行次数

CronJobSpec

// CronJobSpec describes how the job execution will look like and when it will actually run.
type CronJobSpec struct {

// The schedule in Cron format, see https://en.wikipedia.org/wiki/Cron.
// Cron时间格式
Schedule string `json:"schedule" protobuf:"bytes,1,opt,name=schedule"`

// The time zone name for the given schedule, see https://en.wikipedia.org/wiki/List_of_tz_database_time_zones.
// If not specified, this will default to the time zone of the kube-controller-manager process.
// The set of valid time zone names and the time zone offset is loaded from the system-wide time zone
// database by the API server during CronJob validation and the controller manager during execution.
// If no system-wide time zone database can be found a bundled version of the database is used instead.
// If the time zone name becomes invalid during the lifetime of a CronJob or due to a change in host
// configuration, the controller will stop creating new new Jobs and will create a system event with the
// reason UnknownTimeZone.
// More information can be found in https://kubernetes.io/docs/concepts/workloads/controllers/cron-jobs/#time-zones
// This is beta field and must be enabled via the `CronJobTimeZone` feature gate.
// +optional
// 给定时间表时区名称
// beta字段 需要开启CrobJobTimeZone
TimeZone *string `json:"timeZone,omitempty" protobuf:"bytes,8,opt,name=timeZone"`

// Optional deadline in seconds for starting the job if it misses scheduled
// time for any reason. Missed jobs executions will be counted as failed ones.
// +optional
// 由于某种时间错过预定时间,开始运行Job的截止时间
// 错过Job是认为失败了
StartingDeadlineSeconds *int64 `json:"startingDeadlineSeconds,omitempty" protobuf:"varint,2,opt,name=startingDeadlineSeconds"`

// Specifies how to treat concurrent executions of a Job.
// Valid values are:
// - "Allow" (default): allows CronJobs to run concurrently;
// - "Forbid": forbids concurrent runs, skipping next run if previous run hasn't finished yet;
// - "Replace": cancels currently running job and replaces it with a new one
// +optional
// 如何制定Job的并发执行
// 可选值有三个
// Allow:默认值,允许
// Forbid:禁止并发运行,上一次没完成,跳过
// Replace:取消当前正在运行的Job 替换为新的Job
// CronJobs:同时运行
ConcurrencyPolicy ConcurrencyPolicy `json:"concurrencyPolicy,omitempty" protobuf:"bytes,3,opt,name=concurrencyPolicy,casttype=ConcurrencyPolicy"`

// This flag tells the controller to suspend subsequent executions, it does
// not apply to already started executions. Defaults to false.
// +optional
// 标志控制器暂停后序运行,不适用于已经开始的运行,默认值false
Suspend *bool `json:"suspend,omitempty" protobuf:"varint,4,opt,name=suspend"`

// Specifies the job that will be created when executing a CronJob.
// 执行创建的Job
JobTemplate JobTemplateSpec `json:"jobTemplate" protobuf:"bytes,5,opt,name=jobTemplate"`

// The number of successful finished jobs to retain. Value must be non-negative integer.
// Defaults to 3.
// +optional
// 保留成功的已经完成Job数量,默认1,非负数
SuccessfulJobsHistoryLimit *int32 `json:"successfulJobsHistoryLimit,omitempty" protobuf:"varint,6,opt,name=successfulJobsHistoryLimit"`

// The number of failed finished jobs to retain. Value must be non-negative integer.
// Defaults to 1.
// +optional
// 保留失败的已经完成Job数量,默认1,非负数
FailedJobsHistoryLimit *int32 `json:"failedJobsHistoryLimit,omitempty" protobuf:"varint,7,opt,name=failedJobsHistoryLimit"`
}

Cron时间表达式

Schedule是必须的准许Cron语法

*****
第一位:分组(0~59)
第二位:小时(0~23)
第三位:月的某天(1~31)
第四周:月份(1~12)
第五位:周的某天(0~6,周日到周一,某些系统7是周日),或者sun.mon.tue.wed.thu.fri.sat

任务延迟开始的最后期限

StartingDeadlineSeconds表述由于一些原因错过调度

  • 过了截止时间,不会开启任务
  • 如果没有设置截止时间,就不会有截止,不会跳过这一次任务

并发性规则

ConcurrencyPolicy声明了CronJob创建的任务在执行发生重叠的规则,有三个值

  • Allow(默认):CronJob允许并发执行
  • Forbid:Cron.Job不允许并发执行任务。如果新任务执行时间到了,旧任务还没结束,会忽略新任务
  • Replace:如果执行时间到了,旧任务还没结束,会用新任务替换旧任务

并发规则适用于一个CrobJob的Job,CronJob本身是并发的

时区

没有指定时区的CronJob,使用kube-controller-manager时区,使用Master时区

CrobJobTimeZone可以指定时区

  • 处于beta阶段,需要启用CronJobTimeZone
  • 开启之后通过timeZone:”Etc/UTC” 表示K8s的时区