1. 简介

Kubernetes jobs主要是针对短时和批量的工作负载。它是为了结束而运行的,而不是像deployment、replicasets、replication controllers和DaemonSets等其他对象那样持续运行。
Kubernetes Jobs会一直运行到Job中指定的任务完成。也就是说,如果pods给出退出代码0,那么Job就会退出。

2. job

2.1 创建job

1
kubectl create job my-job --image=busybox --dry-run=client -o yaml -- 'touch /tmp/abc.txt;sleep 10' > myjob.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
apiVersion: batch/v1
kind: Job
metadata:
creationTimestamp: null
name: my-job
spec:
backoffLimit: 6
completions: 6
parallelism: 2
template:
metadata:
creationTimestamp: null
spec:
#activeDeadlineSeconds: 6
terminationGracePeriodSeconds: 30
containers:
- command:
- sh
- -c
- touch /tmp/abc.txt;sleep 10
image: busybox
imagePullPolicy: IfNotPresent
name: my-job
resources: {}
restartPolicy: OnFailure
status: {}

参数详解
parallelism: N 一次运行N个pod
completions:M job结束需要成功运行的pod个数,即状态为completed的pod数
backoffLimit:N 如果job失败,则重试几次

job的restart策略只能是:
 Nerver:只要任务没有完成,则是新建pod运行,直到job完成会产生多个pod
 OnFailure:只要pod没有完成,则会重启pod,直到job完成

2.2 举例

计算圆周率

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
apiVersion: batch/v1
kind: Job
metadata:
creationTimestamp: null
name: my-pi
spec:
template:
metadata:
creationTimestamp: null
spec:
#activeDeadlineSeconds: 6
terminationGracePeriodSeconds: 30
containers:
- command: ["perl","-Mbignum=bpi","-wle","print bpi(2000)"]
image: perl
imagePullPolicy: IfNotPresent
name: my-job
resources: {}
restartPolicy: OnFailure
status: {}

查看结果

1
2
[root@node001 chap-8-job]# kubectl logs my-pi-cpcg6 
3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989380952572010654858632788659361533818279682303019520353018529689957736225994138912497217752834791315155748572424541506959508295331168617278558890750983817546374649393192550604009277016711390098488240128583616035637076601047101819429555961989467678374494482553797747268471040475346462080466842590694912933136770289891521047521620569660240580381501935112533824300355876402474964732639141992726042699227967823547816360093417216412199245863150302861829745557067498385054945885869269956909272107975093029553211653449872027559602364806654991198818347977535663698074265425278625518184175746728909777727938000816470600161452491921732172147723501414419735685481613611573525521334757418494684385233239073941433345477624168625189835694855620992192221842725502542568876717904946016534668049886272327917860857843838279679766814541009538837863609506800642251252051173929848960841284886269456042419652850222106611863067442786220391949450471237137869609563643719172874677646575739624138908658326459958133904780275901

3. cronjob

3.1 创建cj

1
kubectl create cronjob mycj --image=busybox --schedule="*/1 * * * *" --dry-run=client -o yaml -- date  > mycj.yaml

3.2 编辑yaml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
apiVersion: batch/v1
kind: CronJob
metadata:
creationTimestamp: null
name: mycj
spec:
successfulJobsHistoryLimit: 5
failedJobsHistoryLimit: 2
jobTemplate:
metadata:
creationTimestamp: null
name: mycj
spec:
template:
metadata:
creationTimestamp: null
spec:
containers:
- command: ["sh","-c","date; sleep 10"]
image: busybox
imagePullPolicy: IfNotPresent
name: mycj
resources: {}
restartPolicy: OnFailure
schedule: '*/1 * * * *'
status: {}


参数详解
successfulJobsHistoryLimit: N 成功的pod保留N个
failedJobsHistoryLimit: N 失败的pod保留N个

3.4 手动执行cronjob

基于现有crontab手动执行job

1
kubectl create job myjobx --from cj/mycj