目录

一.Job控制器

1.简介

2.Jobs较完整解释

3.示例演示

4.注意:如上例的话,执行“kubectl delete -f myJob.yaml”就可以将job删掉

二.CronJob(简写为cj)

1.简介

2.CronJob较完整解释

3.案例演示

4.如上例的话,执行“kubectl delete -f myCronJob.yaml”就可以将cj删掉,jobs一并被删除


一.Job控制器

1.简介

他主要是用于批量地去执行一次性任务,确保在pod上让指定的任务能够正确完成,我们可以在配置中指定期望其完成的数量,执行成功后会记录下成功的数量,执行完成后pod会标识为completed(该pod的主进程已经完成并退出)。

2.Jobs较完整解释

截取至edit Jobs的spec部分

spec:backoffLimit: 6#执行失败后的重试次数,不指定就默认6completionMode: NonIndexedcompletions: 18#期望job成功运行的数量,不指定就默认为1manualSelector: true#是否可以使用selector选择器,不指定就默认false那么将无法应用selector部分parallelism: 6#job执行的并发数量,不指定的话默认1selector: matchLabels:app: job-podsuspend: falsetemplate: metadata:creationTimestamp: nulllabels: app: job-pod spec:containers:- command: - /bin/sh - -c - for i in [1..10];do /bin/echo $i;sleep 2; done; image: busybox imagePullPolicy: Always name: my-job-container resources: {} terminationMessagePath: /dev/termination-log#这些参数之前介绍过 terminationMessagePolicy: FilednsPolicy: ClusterFirstrestartPolicy: Never#这里的重启策略算是比较特殊的,尤其注意这里template.spec的restartPolicy会覆盖模板外设置的restartPolicy#若为Never,pod出现故障时job会创建新的pod,故障pod不消失,且不重启,failed次数加1。#若为OnFailure,pod出现故障时job会重启容器,不创建pod,failed次数不变。#若为Always,一直重启,job就会重复执行,不使用。置为AlwaysschedulerName: default-schedulersecurityContext: {}terminationGracePeriodSeconds: 30

3.示例演示

这里创建一个Job,执行指定遍历命令,期望运行成功18个pod,每次并发运行6个pod。最后可以观察到是运行了18个pod,pod的最后状态也变为Completed。

[root@k8s-master pod]# cat myJob.yaml apiVersion: batch/v1kind: Jobmetadata:name: my-jobnamespace: mynsspec:completions: 18parallelism: 6manualSelector: trueselector: matchLabels:app: job-podtemplate: metadata:labels: app: job-pod spec:restartPolicy: Nevercontainers:- name: my-job-container image: busybox command: ["/bin/sh","-c","for i in [1..10];do /bin/echo $i;sleep 2; done;"]​[root@k8s-master pod]# kubectl apply -f myJob.yamljob.batch/my-job created[root@k8s-master pod]# kubectl get pods -n myns -wNAMEREADYSTATUSRESTARTSAGEmy-job-b6s8s0/1 ContainerCreating02smy-job-jc4zz0/1 ContainerCreating02smy-job-kts5m0/1 ContainerCreating02smy-job-ppltd0/1 ContainerCreating02smy-job-shn8q0/1 ContainerCreating02smy-job-zb7570/1 ContainerCreating02smy-job-kts5m0/1 ContainerCreating02smy-job-shn8q0/1 ContainerCreating02smy-job-ppltd0/1 ContainerCreating02smy-job-jc4zz1/1 Running 05smy-job-kts5m1/1 Running 06smy-job-jc4zz0/1 Completed07smy-job-zb7571/1 Running 08smy-job-jc4zz0/1 Completed08smy-job-jc4zz0/1 Completed08smy-job-kts5m0/1 Completed08smy-job-jc4zz0/1 Completed09smy-job-2zl7z0/1 Pending 00smy-job-2zl7z0/1 Pending 00smy-job-jc4zz0/1 Completed09smy-job-2zl7z0/1 ContainerCreating00smy-job-shn8q1/1 Running 09smy-job-kts5m0/1 Completed09smy-job-2zl7z0/1 ContainerCreating00smy-job-kts5m0/1 Completed09smy-job-zb7570/1 Completed010smy-job-52mf90/1 Pending 00smy-job-52mf90/1 Pending 00smy-job-kts5m0/1 Completed010smy-job-52mf90/1 ContainerCreating00smy-job-kts5m0/1 Completed010smy-job-52mf90/1 ContainerCreating00smy-job-b6s8s1/1 Running 011smy-job-zb7570/1 Completed011smy-job-zb7570/1 Completed011smy-job-shn8q0/1 Completed011smy-job-zb7570/1 Completed012smy-job-ftzpl0/1 Pending 00smy-job-ftzpl0/1 Pending 00smy-job-zb7570/1 Completed012smy-job-ftzpl0/1 ContainerCreating00smy-job-ppltd1/1 Running 012smy-job-shn8q0/1 Completed012smy-job-ftzpl0/1 ContainerCreating00smy-job-shn8q0/1 Completed012smy-job-b6s8s0/1 Completed013smy-job-8hf4n0/1 Pending 00smy-job-8hf4n0/1 Pending 00smy-job-shn8q0/1 Completed013smy-job-8hf4n0/1 ContainerCreating00smy-job-shn8q0/1 Completed013smy-job-8hf4n0/1 ContainerCreating00smy-job-2zl7z1/1 Running 05smy-job-b6s8s0/1 Completed014smy-job-b6s8s0/1 Completed014smy-job-ppltd0/1 Completed014smy-job-pxndz0/1 Pending 00smy-job-b6s8s0/1 Completed015smy-job-pxndz0/1 Pending 00smy-job-b6s8s0/1 Completed015smy-job-pxndz0/1 ContainerCreating00smy-job-52mf91/1 Running 05smy-job-ppltd0/1 Completed015smy-job-pxndz0/1 ContainerCreating00smy-job-ppltd0/1 Completed015smy-job-6tdqn0/1 Pending 00smy-job-2zl7z0/1 Completed07smy-job-6tdqn0/1 Pending 00smy-job-ppltd0/1 Completed016smy-job-6tdqn0/1 ContainerCreating00smy-job-ppltd0/1 Completed016smy-job-6tdqn0/1 ContainerCreating00smy-job-ftzpl1/1 Running 05smy-job-2zl7z0/1 Completed08smy-job-2zl7z0/1 Completed08smy-job-52mf90/1 Completed07smy-job-2zl7z0/1 Completed09smy-job-cxfmh0/1 Pending 00smy-job-cxfmh0/1 Pending 00smy-job-2zl7z0/1 Completed09smy-job-cxfmh0/1 ContainerCreating00smy-job-8hf4n1/1 Running 05smy-job-52mf90/1 Completed08smy-job-cxfmh0/1 ContainerCreating00smy-job-52mf90/1 Completed08smy-job-ftzpl0/1 Completed07smy-job-4zg6h0/1 Pending 00smy-job-4zg6h0/1 Pending 00smy-job-52mf90/1 Completed09smy-job-4zg6h0/1 ContainerCreating00smy-job-52mf90/1 Completed09smy-job-4zg6h0/1 ContainerCreating00smy-job-pxndz1/1 Running 05smy-job-ftzpl0/1 Completed08smy-job-ftzpl0/1 Completed08smy-job-8hf4n0/1 Completed07smy-job-ftzpl0/1 Completed09smy-job-5spg70/1 Pending 00smy-job-5spg70/1 Pending 00smy-job-ftzpl0/1 Completed09smy-job-5spg70/1 ContainerCreating00smy-job-6tdqn1/1 Running 05smy-job-8hf4n0/1 Completed08smy-job-5spg70/1 ContainerCreating00smy-job-8hf4n0/1 Completed08smy-job-pxndz0/1 Completed07smy-job-dr2mp0/1 Pending 00smy-job-dr2mp0/1 Pending 00smy-job-8hf4n0/1 Completed09smy-job-dr2mp0/1 ContainerCreating00smy-job-8hf4n0/1 Completed09smy-job-dr2mp0/1 ContainerCreating00smy-job-cxfmh1/1 Running 05smy-job-pxndz0/1 Completed08smy-job-pxndz0/1 Completed08smy-job-6tdqn0/1 Completed07smy-job-pxndz0/1 Completed09smy-job-7wjqq0/1 Pending 00smy-job-7wjqq0/1 Pending 00smy-job-pxndz0/1 Completed09smy-job-7wjqq0/1 ContainerCreating00smy-job-4zg6h1/1 Running 05smy-job-6tdqn0/1 Completed08smy-job-7wjqq0/1 ContainerCreating00smy-job-6tdqn0/1 Completed08smy-job-cxfmh0/1 Completed07smy-job-cz8f40/1 Pending 00smy-job-cz8f40/1 Pending 00smy-job-6tdqn0/1 Completed09smy-job-cz8f40/1 ContainerCreating00smy-job-6tdqn0/1 Completed09smy-job-cz8f40/1 ContainerCreating00smy-job-5spg71/1 Running 05smy-job-cxfmh0/1 Completed08smy-job-cxfmh0/1 Completed08smy-job-4zg6h0/1 Completed07smy-job-cxfmh0/1 Completed09smy-job-cxfmh0/1 Completed09smy-job-4zg6h0/1 Completed08smy-job-4zg6h0/1 Completed08smy-job-5spg70/1 Completed07smy-job-4zg6h0/1 Completed09smy-job-4zg6h0/1 Completed09smy-job-dr2mp1/1 Running 06smy-job-7wjqq1/1 Running 05smy-job-5spg70/1 Completed08smy-job-5spg70/1 Completed08smy-job-5spg70/1 Completed09smy-job-5spg70/1 Completed09smy-job-dr2mp0/1 Completed08smy-job-7wjqq0/1 Completed07smy-job-cz8f41/1 Running 06smy-job-dr2mp0/1 Completed09smy-job-dr2mp0/1 Completed09smy-job-dr2mp0/1 Completed010smy-job-7wjqq0/1 Completed08smy-job-7wjqq0/1 Completed08smy-job-dr2mp0/1 Completed010smy-job-7wjqq0/1 Completed09smy-job-7wjqq0/1 Completed09smy-job-cz8f40/1 Completed08smy-job-cz8f40/1 Completed09smy-job-cz8f40/1 Completed09smy-job-cz8f40/1 Completed010smy-job-cz8f40/1 Completed010s​[root@k8s-master pod]# kubectl get Jobs -n mynsNAME COMPLETIONSDURATIONAGEmy-job18/18 35s 4m15s

4.注意:如上例的话,执行“kubectl delete -f myJob.yaml”就可以将job删掉

二.CronJob(简写为cj)

1.简介

他是在借助Job的情况下,按照指定的时间节点去循环重复执行任务。在 CronJob 对象中定义时间表(schedule),该时间表指定了作业运行的时间间隔或特定的运行时间。CronJob 控制器会定期检查时间表,如果当前时间匹配时间表中定义的时间,则创建一个新的 Job 对象,其中包含要执行的任务(如容器镜像、命令、参数等)。

当 Job 对象创建后,k8s会自动将其调度到可用节点上进行运行,并自动管理作业的生命周期,包括启动、监控、重试和清理等操作。执行成功后pod会被表示为completed(该pod的主进程已经完成并退出)。

2.CronJob较完整解释

截取自edit cj的spec部分

spec:schedule: '*/1 * * * *'#这里是为任务定义时间,分时日月周的顺序书写,控制任务再什么时候执行,如果这里需要指定多个时间时可以用逗号隔开startingDeadlineSeconds: 300#同jobs解释successfulJobsHistoryLimit: 3#同jobs解释suspend: falseconcurrencyPolicy: Allow #并发执行策略,用于定义前一次作业运行尚未完成时是否以及如何运行后一次的作业#默认Allow,允许jobs并发运行#Forbid,禁止并发运行,上一次的运行失败会跳过下一次运行#Replace,用新定时任务区替换旧任务的执行failedJobsHistoryLimit: 1#同jobsjobTemplate:#定义job的控制模板,在pod模板外面嵌套一个对job的模板定义,为cronjob控制器生成jobs metadata:creationTimestamp: null spec:completions: 3#这里就是jobs的定义了parallelism: 1template: metadata:creationTimestamp: null spec:containers:- command: - /bin/sh - -c - for i in [1..10]; do /bin/echo $i;sleep 2; done; image: busybox imagePullPolicy: Always name: my-cronjob-container resources: {} terminationMessagePath: /dev/termination-log terminationMessagePolicy: FilednsPolicy: ClusterFirstrestartPolicy: NeverschedulerName: default-schedulersecurityContext: {}terminationGracePeriodSeconds: 30

格式类似于Linux上的这个定时任务(分时日月周),周表示一周中的第几天,可填值如图示

3.案例演示

这里我们创建一个Cronjob,每隔一分钟(只是为了测试效果)执行指定的遍历任务,记录jobs的成功次数。最后结果可以观察jobs的创建时间(25s-85s-2m25s),确实是按照每分钟执行一次

[root@k8s-master pod]# cat myCronJob.yaml apiVersion: batch/v1kind: CronJobmetadata:name: my-cronjobnamespace: mynsspec:schedule: "*/1 * * * *"startingDeadlineSeconds: 300jobTemplate: metadata: spec:completions: 3parallelism: 1template: spec:restartPolicy: Nevercontainers:- name: my-cronjob-container image: busybox command: ["/bin/sh","-c","for i in [1..10]; do /bin/echo $i;sleep 2; done;"]​[root@k8s-master pod]# kubectl get cj -n mynsNAME SCHEDULESUSPENDACTIVELAST SCHEDULEAGEmy-cronjob*/1 * * * *False 0 53s 3m9s​[root@k8s-master pod]# kubectl get jobs -n mynsNAMECOMPLETIONSDURATIONAGEmy-cronjob-283544713/324s 2m25smy-cronjob-283544723/324s 85smy-cronjob-283544733/323s 25s

4.如上例的话,执行“kubectl delete -f myCronJob.yaml”就可以将cj删掉,jobs一并被删除