CronJob 介绍
CronJob 控制器以 Job 控制器资源为其管控对象,并借助它管理 pod 资源对象,job 控制器定义的作业任务在其控制器资源创建之后便会立即执行,但 CronJob 可以以类似于 Linux 操作系统的周期性任务作业计划的方式控制其运行时间及重复运行的方式。也就是说, CronJob 可以在特定的时间点(反复的)去运行 job 任务
CronJob 的资源清单文件
apiVersion: batch/v1beta1 # 版本号 kind: CronJob # 类型 metadata: # 元数据 name: # rs名称 namespace: # 所属命名空间 labels: #标签 controller: cronjob spec: # 详情描述 schedule: # cron格式的作业调度运行时间点,用于控制任务在什么时间执行 concurrencyPolicy: # 并发执行策略,用于定义前一次作业运行尚未完成时是否以及如何运行后一次的作业 failedJobHistoryLimit: # 为失败的任务执行保留的历史记录数,默认为1 successfulJobHistoryLimit: # 为成功的任务执行保留的历史记录数,默认为3 startingDeadlineSeconds: # 启动作业错误的超时时长 jobTemplate: # job控制器模板,用于为cronjob控制器生成job对象;下面其实就是job的定义 metadata: spec: completions: 1 parallelism: 1 activeDeadlineSeconds: 30 backoffLimit: 6 manualSelector: true selector: matchLabels: app: counter-pod matchExpressions: # 规则 - {key: app, operator: In, values: [counter-pod]} template: metadata: labels: app: counter-pod spec: restartPolicy: Never containers: - name: counter image: busybox:1.30 command: ["bin/sh","-c","for i in 9 8 7 6 5 4 3 2 1; do echo $i;sleep 20;done"]
有几个选项如下
需要重点解释的几个选项: # schedule: cron表达式,用于指定任务的执行时间 */1 * * * * <分钟> <小时> <日> <月份> <星期> 分钟 值从 0 到 59. 小时 值从 0 到 23. 日 值从 1 到 31. 月 值从 1 到 12. 星期 值从 0 到 6, 0 代表星期日 多个时间可以用逗号隔开; 范围可以用连字符给出;*可以作为通配符; /表示每... # concurrencyPolicy: Allow: 允许Jobs并发运行(默认) Forbid: 禁止并发运行,如果上一次运行尚未完成,则跳过下一次运行 Replace: 替换,取消当前正在运行的作业并用新作业替换它
CronJob 的使用
创建 pc-cronjob.yaml,内容如下:
apiVersion: batch/v1beta1 kind: CronJob # 类型为 CronJob metadata: name: pc-cronjob # CronJob 的名称 namespace: zouzou labels: # 标签 controller: cronjob spec: schedule: "*/1 * * * *" # 每分钟执行一次 jobTemplate: metadata: spec: template: spec: restartPolicy: Never containers: - name: counter image: busybox:1.30 command: ["bin/sh","-c","for i in 9 8 7 6 5 4 3 2 1; do echo $i;sleep 3;done"]
上面 cronjob 要完成的任务是,循环输入 9、8、7、6、5、4、3、2、1,每输入一个就等待 3 s
为了更好的查看效果,我们这里在开三个窗口,分别监听 cronjob、 job 和 pod
# 监听 cronjob,cj 是 cronjob 的简写 kubectl get cj -n zouzou -o wide -w # 监听 job kubectl get job -n zouzou -o wide -w # 监听 pod kubectl get pod -n zouzou -o wide -w
上面准备工作做好之后,我们创建 cronjob
# 创建 cronjob [root@dce-10-6-215-215 tmp]# kubectl create -f pc-cronjob.yaml cronjob.batch/pc-cronjob created
等个几分钟分别查看下面的数据
查看 cronjob
[root@dce-10-6-215-215 ~]# kubectl get cj -n zouzou -o wide -w NAME SCHEDULE SUSPEND ACTIVE LAST SCHEDULE AGE CONTAINERS IMAGES SELECTOR pc-cronjob */1 * * * * False 0 <none> 0s counter busybox:1.30 <none> pc-cronjob */1 * * * * False 1 1s 34s counter busybox:1.30 <none> pc-cronjob */1 * * * * False 0 41s 74s counter busybox:1.30 <none> pc-cronjob */1 * * * * False 1 1s 94s counter busybox:1.30 <none> pc-cronjob */1 * * * * False 0 41s 2m14s counter busybox:1.30 <none> pc-cronjob */1 * * * * False 1 1s 2m34s counter busybox:1.30 <none> pc-cronjob */1 * * * * False 0 41s 3m14s counter busybox:1.30 <none> pc-cronjob */1 * * * * False 1 1s 3m34s counter busybox:1.30 <none> pc-cronjob */1 * * * * False 0 41s 4m14s counter busybox:1.30 <none> pc-cronjob */1 * * * * False 1 1s 4m34s counter busybox:1.30 <none>
查看 job
[root@dce-10-6-215-215 ~]# kubectl get job -n zouzou -o wide -w NAME COMPLETIONS DURATION AGE CONTAINERS IMAGES SELECTOR pc-cronjob-1650180960 0/1 0s counter busybox:1.30 controller-uid=247a7822-a7dd-4f2e-9ae8-1c395a2d1759 pc-cronjob-1650180960 0/1 0s 0s counter busybox:1.30 controller-uid=247a7822-a7dd-4f2e-9ae8-1c395a2d1759 pc-cronjob-1650180960 1/1 32s 32s counter busybox:1.30 controller-uid=247a7822-a7dd-4f2e-9ae8-1c395a2d1759 pc-cronjob-1650181020 0/1 0s counter busybox:1.30 controller-uid=f1ac3b2d-d3e5-430d-b371-a32d22385a68 pc-cronjob-1650181020 0/1 0s 0s counter busybox:1.30 controller-uid=f1ac3b2d-d3e5-430d-b371-a32d22385a68 pc-cronjob-1650181020 1/1 31s 31s counter busybox:1.30 controller-uid=f1ac3b2d-d3e5-430d-b371-a32d22385a68 pc-cronjob-1650181080 0/1 0s counter busybox:1.30 controller-uid=aab49cf6-ea6f-46be-a7bb-e8cb194e720e pc-cronjob-1650181080 0/1 0s 0s counter busybox:1.30 controller-uid=aab49cf6-ea6f-46be-a7bb-e8cb194e720e pc-cronjob-1650181080 1/1 31s 31s counter busybox:1.30 controller-uid=aab49cf6-ea6f-46be-a7bb-e8cb194e720e pc-cronjob-1650181140 0/1 0s counter busybox:1.30 controller-uid=e5e87174-034d-4347-b65a-fde332b99644 pc-cronjob-1650181140 0/1 0s 0s counter busybox:1.30 controller-uid=e5e87174-034d-4347-b65a-fde332b99644 pc-cronjob-1650181140 1/1 32s 32s counter busybox:1.30 controller-uid=e5e87174-034d-4347-b65a-fde332b99644 pc-cronjob-1650180960 1/1 32s 3m40s counter busybox:1.30 controller-uid=247a7822-a7dd-4f2e-9ae8-1c395a2d1759 pc-cronjob-1650181200 0/1 0s counter busybox:1.30 controller-uid=65aff049-edc6-4ec6-ba00-fa25884c91cc pc-cronjob-1650181200 0/1 0s 0s counter busybox:1.30 controller-uid=65aff049-edc6-4ec6-ba00-fa25884c91cc
查看 pod,可以看到,pod 完成之后过一会就又创建了一个 pod,然后依次循环
[root@dce-10-6-215-215 ~]# kubectl get pod -n zouzou -o wide -w NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES pc-cronjob-1650180960-zrfjs 0/1 Pending 0 0s <none> <none> <none> <none> pc-cronjob-1650180960-zrfjs 0/1 Pending 0 0s <none> dce-10-6-215-200 <none> <none> pc-cronjob-1650180960-zrfjs 0/1 ContainerCreating 0 0s <none> dce-10-6-215-200 <none> <none> pc-cronjob-1650180960-zrfjs 0/1 ContainerCreating 0 2s <none> dce-10-6-215-200 <none> <none> pc-cronjob-1650180960-zrfjs 0/1 ContainerCreating 0 3s <none> dce-10-6-215-200 <none> <none> pc-cronjob-1650180960-zrfjs 1/1 Running 0 4s 172.29.34.228 dce-10-6-215-200 <none> <none> pc-cronjob-1650180960-zrfjs 0/1 Completed 0 32s 172.29.34.228 dce-10-6-215-200 <none> <none> pc-cronjob-1650181020-89zf6 0/1 Pending 0 0s <none> <none> <none> <none> pc-cronjob-1650181020-89zf6 0/1 Pending 0 0s <none> dce-10-6-215-200 <none> <none> pc-cronjob-1650181020-89zf6 0/1 ContainerCreating 0 0s <none> dce-10-6-215-200 <none> <none> pc-cronjob-1650181020-89zf6 0/1 ContainerCreating 0 2s <none> dce-10-6-215-200 <none> <none> pc-cronjob-1650181020-89zf6 0/1 ContainerCreating 0 2s <none> dce-10-6-215-200 <none> <none> pc-cronjob-1650181020-89zf6 1/1 Running 0 5s 172.29.34.229 dce-10-6-215-200 <none> <none> pc-cronjob-1650181020-89zf6 0/1 Completed 0 31s 172.29.34.229 dce-10-6-215-200 <none> <none> pc-cronjob-1650181080-k6sjp 0/1 Pending 0 0s <none> <none> <none> <none> pc-cronjob-1650181080-k6sjp 0/1 Pending 0 0s <none> dce-10-6-215-200 <none> <none> pc-cronjob-1650181080-k6sjp 0/1 ContainerCreating 0 0s <none> dce-10-6-215-200 <none> <none> pc-cronjob-1650181080-k6sjp 0/1 ContainerCreating 0 2s <none> dce-10-6-215-200 <none> <none> pc-cronjob-1650181080-k6sjp 0/1 ContainerCreating 0 3s <none> dce-10-6-215-200 <none> <none> pc-cronjob-1650181080-k6sjp 1/1 Running 0 5s 172.29.34.242 dce-10-6-215-200 <none> <none> pc-cronjob-1650181080-k6sjp 0/1 Completed 0 31s 172.29.34.242 dce-10-6-215-200 <none> <none> pc-cronjob-1650181140-sdpzm 0/1 Pending 0 0s <none> <none> <none> <none> pc-cronjob-1650181140-sdpzm 0/1 Pending 0 0s <none> dce-10-6-215-200 <none> <none> pc-cronjob-1650181140-sdpzm 0/1 ContainerCreating 0 0s <none> dce-10-6-215-200 <none> <none> pc-cronjob-1650181140-sdpzm 0/1 ContainerCreating 0 2s <none> dce-10-6-215-200 <none> <none> pc-cronjob-1650181140-sdpzm 0/1 ContainerCreating 0 3s <none> dce-10-6-215-200 <none> <none> pc-cronjob-1650181140-sdpzm 1/1 Running 0 5s 172.29.34.244 dce-10-6-215-200 <none> <none> pc-cronjob-1650181140-sdpzm 0/1 Completed 0 32s 172.29.34.244 dce-10-6-215-200 <none> <none> pc-cronjob-1650180960-zrfjs 0/1 Terminating 0 3m41s 172.29.34.228 dce-10-6-215-200 <none> <none> pc-cronjob-1650180960-zrfjs 0/1 Terminating 0 3m41s 172.29.34.228 dce-10-6-215-200 <none> <none> pc-cronjob-1650181200-xp8tn 0/1 Pending 0 0s <none> <none> <none> <none> pc-cronjob-1650181200-xp8tn 0/1 Pending 0 0s <none> dce-10-6-215-200 <none> <none> pc-cronjob-1650181200-xp8tn 0/1 ContainerCreating 0 0s <none> dce-10-6-215-200 <none> <none> pc-cronjob-1650181200-xp8tn 0/1 ContainerCreating 0 3s <none> dce-10-6-215-200 <none> <none> pc-cronjob-1650181200-xp8tn 0/1 ContainerCreating 0 3s <none> dce-10-6-215-200 <none> <none> pc-cronjob-1650181200-xp8tn 1/1 Running 0 5s 172.29.34.243 dce-10-6-215-200 <none> <none>
上面是通过监听来看的,可以直接看下运行后的
删除 cronjob
删除 cronjob 也有两种方式,一种是通过 yaml 文件,如下
kubectl delete -f pc-cronjob.yaml
还有一种是通过 cronjob 名称进行删除
kubectl delete cj pc-cronjob -n zouzou