在Kubernetes中分贝使用Job和CronJob实现一次性运行的任务和定时运行的的任务,他们分别被Kubernetes的JobController和CronJobController控制器所控制,而这些任务都是通过Pod运行的。
在创建Job和CronJob对象的时候,既可以使用定义对象的YAML文件,还可使用命令直接创建。需要注意的是,从OpenShift 4.5开始,在使用oc命令创建Job和CronJob对象的时候只能使用“oc create job”和“oc create cronjob”命令。而原有“oc run”命令已经不能创建Job和CronJob对象了,它只能运行1个指定pod。
通过YAML创建
创建Job
- 创建项目
$ oc create project myjob
- 在OpenShift控制台的项目中直接运行以下创建Job的YAML脚本。
apiVersion: batch/v1
kind: Job
metadata:
name: pi
spec:
parallelism: 1
completions: 1
activeDeadlineSeconds: 1800
backoffLimit: 6
template:
metadata:
name: pi
spec:
containers:
- name: pi
image: perl
command: ["perl", "-Mbignum=bpi", "-wle", "print bpi(2000)"]
restartPolicy: OnFailure
- 查看job和pod,在pod为“Completed”状态后查看它的日志。注意:从对象名称可以看出, 这种方法是先创建Job对象,然后再通过有Job对象生成的对应Pod对象。
$ oc get job
NAME COMPLETIONS DURATION AGE
pi 1/1 33s 7m37s
$ oc get pod
NAME READY STATUS RESTARTS AGE
pi-kxdcr 0/1 Completed 0 7m40s
$ oc logs pod/pi-kxdcr
3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989380952572010654858632788659361533818279682303019520353018529689957736225994138912497217752834791315155748572424541506959508295331168617278558890750983817546374649393192550604009277016711390098488240128583616035637076601047101819429555961989467678374494482553797747268471040475346462080466842590694912933136770289891521047521620569660240580381501935112533824300355876402474964732639141992726042699227967823547816360093417216412199245863150302861829745557067498385054945885869269956909272107975093029553211653449872027559602364806654991198818347977535663698074265425278625518184175746728909777727938000816470600161452491921732172147723501414419735685481613611573525521334757418494684385233239073941433345477624168625189835694855620992192221842725502542568876717904946016534668049886272327917860857843838279679766814541009538837863609506800642251252051173929848960841284886269456042419652850222106611863067442786220391949450471237137869609563643719172874677646575739624138908658326459958133904780275901
创建CronJob
- 在OpenShift控制台的项目中直接运行以下创建CronJob的YAML脚本。
apiVersion: batch/v1
kind: CronJob
metadata:
name: hello-job
spec:
schedule: "*/1 * * * *"
jobTemplate:
spec:
template:
spec:
containers:
- name: hello
image: busybox
args:
- /bin/sh
- -c
- date; echo Hello from the Kubernetes cluster
restartPolicy: OnFailure
- 通过命令行或控制台查看创建的hello-job。
$ oc get cronjob -n myjob
NAME SCHEDULE SUSPEND ACTIVE LAST SCHEDULE AGE
hello-job */1 * * * * False 1 7s 14m
3. 查看hello-job的详细信息。注意下方有和该对象相关的Events,从时间轴可以看出,当需要创建第4个Pod运行Job的时候,OpenShift删删掉第一个创建Pod。这样就一直保持运行hello-job的Pod数量最多为3个,这和hello-job对象的“Successful Job History Limit”参数定义的是一致的。
$ oc describe cronjob hello-job -n myjob
Name: hello-job
Namespace: myjob
Labels: <none>
Annotations: <none>
Schedule: */1 * * * *
Concurrency Policy: Allow
Suspend: False
Successful Job History Limit: 3
Failed Job History Limit: 1
Starting Deadline Seconds: <unset>
Selector: <unset>
Parallelism: <unset>
Completions: <unset>
Pod Template:
Labels: <none>
Containers:
hello:
Image: busybox
Port: <none>
Host Port: <none>
Args:
/bin/sh
-c
date; echo Hello from the Kubernetes cluster
Environment: <none>
Mounts: <none>
Volumes: <none>
Last Schedule Time: Thu, 03 Sep 2020 03:47:00 +0000
Active Jobs: <none>
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal SuccessfulCreate 15m cronjob-controller Created job hello-job-1599103920
Normal SawCompletedJob 15m cronjob-controller Saw completed job: hello-job-1599103920, status: Complete
Normal SuccessfulCreate 14m cronjob-controller Created job hello-job-1599103980
Normal SawCompletedJob 14m cronjob-controller Saw completed job: hello-job-1599103980, status: Complete
Normal SuccessfulCreate 13m cronjob-controller Created job hello-job-1599104040
Normal SawCompletedJob 13m cronjob-controller Saw completed job: hello-job-1599104040, status: Complete
Normal SuccessfulCreate 12m cronjob-controller Created job hello-job-1599104100
Normal SuccessfulDelete 12m cronjob-controller Deleted job hello-job-1599103920
Normal SawCompletedJob 12m cronjob-controller Saw completed job: hello-job-1599104100, status: Complete
Normal SuccessfulCreate 11m cronjob-controller Created job hello-job-1599104160
Normal SawCompletedJob 11m cronjob-controller Saw completed job: hello-job-1599104160, status: Complete
Normal SuccessfulDelete 11m cronjob-controller Deleted job hello-job-1599103980
Normal SuccessfulCreate 10m cronjob-controller Created job hello-job-1599104220
Normal SuccessfulDelete 10m cronjob-controller Deleted job hello-job-1599104040
Normal SawCompletedJob 10m cronjob-controller Saw completed job: hello-job-1599104220, status: Complete
Normal SuccessfulCreate 9m50s cronjob-controller Created job hello-job-1599104280
Normal SawCompletedJob 9m40s cronjob-controller Saw completed job: hello-job-1599104280, status: Complete
Normal SuccessfulDelete 9m40s cronjob-controller Deleted job hello-job-1599104100
Normal SuccessfulCreate 8m49s cronjob-controller Created job hello-job-1599104340
Normal SawCompletedJob 8m39s cronjob-controller Saw completed job: hello-job-1599104340, status: Complete
Normal SuccessfulDelete 8m39s cronjob-controller Deleted job hello-job-1599104160
Normal SuccessfulCreate 7m49s cronjob-controller Created job hello-job-1599104400
Normal SawCompletedJob 7m39s cronjob-controller Saw completed job: hello-job-1599104400, status: Complete
Normal SuccessfulDelete 7m39s cronjob-controller Deleted job hello-job-1599104220
Normal SawCompletedJob 5m37s (x2 over 6m38s) cronjob-controller (combined from similar events): Saw completed job: hello-job-1599104520, status: Complete
Normal SuccessfulCreate 43s (x7 over 6m48s) cronjob-controller (combined from similar events): Created job hello-job-1599104820
- 在OpenShift控制台的hello-job中查看相关的Event。
- 查看运行的job数量。
$ oc get job -n myjob
NAME COMPLETIONS DURATION AGE
hello-job-1599105120 1/1 7s 3m3s
hello-job-1599105180 1/1 8s 2m2s
hello-job-1599105240 1/1 7s 61s
hello-job-1599105300 0/1 0s
- 查看运行的pod数量,它和job数量一致,同时最多保持3个Pod。
$ oc get pod -n myjob
NAME READY STATUS RESTARTS AGE
hello-job-1599104580-b2gqw 0/1 Completed 0 3m8s
hello-job-1599104640-kv9ml 0/1 Completed 0 2m8s
hello-job-1599104700-g2jmv 0/1 Completed 0 67s
hello-job-1599104760-dtwz6 0/1 ContainerCreating 0 6s
使用命令
创建Job
- 运行以下命令,创建运行Job的Pod。
$ oc create job pi --image=perl -- perl -Mbignum=bpi -wle 'print bpi(2000)'
- 查看Job和Pod对象。注意:从查到的对象可以看出, 这种方法是是直接生成Pod对象并运行,而没有创建Job对象。
$ oc get job
$ oc get pod
NAME READY STATUS RESTARTS AGE
pi-job 0/1 Completed 0 33s
创建CronJob
- 执行命令,创建CronJob。注意:当有“–schedule=’*/1 * * * *’”参数的时候,会自动生成CronJob对象。而Job对象是由CronJob对象生成的。
$ oc create cronjob pi --image=perl --schedule='*/1 * * * *' -- perl -Mbignum=bpi -wle 'print bpi(2000)'
- 查看CronJob