玩过Linux系统的童鞋都知道crontab定时任务
实际上cronjob就类似于crontab
[root@vms61 chap8-job]# cat job1.yaml
apiVersion: batch/v1
kind: Job
metadata:
name: job1
spec:
#backoffLimit: 6
#completions: 1
#parallelism: 1
template:
metadata:
name: pi
spec:
containers:
- name: hello
image: busybox
imagePullPolicy: IfNotPresent
command: ["sh","-c","echo hello world! ; sleep 10"]
restartPolicy: Never
[root@vms61 chap8-job]# kubectl apply -f job1.yaml
job.batch/job1 created
[root@vms61 chap8-job]# kubectl get job
NAME COMPLETIONS DURATION AGE
job1 0/1 3s 4s
[root@vms61 chap8-job]# kubectl get pods
NAME READY STATUS RESTARTS AGE
job1-wt6v5 1/1 Running 0 7s
[root@vms61 chap8-job]# kubectl get pods
NAME READY STATUS RESTARTS AGE
job1-wt6v5 1/1 Running 0 12s
[root@vms61 chap8-job]# kubectl get pods
NAME READY STATUS RESTARTS AGE
job1-wt6v5 0/1 Completed 0 22s
[root@vms61 chap8-job]# kubectl get job
NAME COMPLETIONS DURATION AGE
job1 1/1 11s 33s
如果配置文件中的sh改成一个不存在的比如shxxx,且restartPolicy一直未Never的话那么就会一直创建不了,报ContainerCannotRun,也仍然会一直创建;如果restartPolicy改为OnFailure,那么就会一直重启,RESTARTS会一直增加
所以,
job的restart策略只能是:
Nerver:只要任务没有完成,则是新创建pod运行,直到job完成会产生多个pod
OnFailure:只要pod没有完成,则会重启pod,直到job完成
parallelism: N 一次性运行N个pod
completions: M job结束需要成功运行的Pod个数,即状态为Completed的pod数
backoffLimit: N 如果job失败,则重试几次
这里parallelism的值指的是一次性运行几个pod,这个值不会超过completions的值。
[root@vms61 chap8-job]# cat job1.yaml
apiVersion: batch/v1
kind: Job
metadata:
name: job1
spec:
backoffLimit: 6
completions: 4
parallelism: 2
template:
metadata:
name: pi
spec:
containers:
- name: hello
image: busybox
imagePullPolicy: IfNotPresent
command: ["sh","-c","echo hello world! ; sleep 10"]
restartPolicy: OnFailure
[root@vms61 chap8-job]# kubectl apply -f job1.yaml
job.batch/job1 created
[root@vms61 chap8-job]# kubectl get pods
NAME READY STATUS RESTARTS AGE
job1-8fsbc 1/1 Running 0 4s
job1-nlqpl 1/1 Running 0 4s
[root@vms61 chap8-job]# kubectl get pods
NAME READY STATUS RESTARTS AGE
job1-8fsbc 1/1 Running 0 9s
job1-nlqpl 1/1 Running 0 9s
[root@vms61 chap8-job]# kubectl get pods
NAME READY STATUS RESTARTS AGE
job1-8fsbc 1/1 Running 0 11s
job1-nlqpl 1/1 Running 0 11s
[root@vms61 chap8-job]# kubectl get pods
NAME READY STATUS RESTARTS AGE
job1-25g8n 0/1 ContainerCreating 0 0s
job1-8fsbc 0/1 Completed 0 12s
job1-nlqpl 0/1 Completed 0 12s
job1-qmqbr 0/1 ContainerCreating 0 0s
[root@vms61 chap8-job]# kubectl get pods
NAME READY STATUS RESTARTS AGE
job1-25g8n 1/1 Running 0 6s
job1-8fsbc 0/1 Completed 0 18s
job1-nlqpl 0/1 Completed 0 18s
job1-qmqbr 1/1 Running 0 6s
[root@vms61 chap8-job]# kubectl get pods
NAME READY STATUS RESTARTS AGE
job1-25g8n 0/1 Completed 0 12s
job1-8fsbc 0/1 Completed 0 24s
job1-nlqpl 0/1 Completed 0 24s
job1-qmqbr 0/1 Completed 0 12s
例子:计算圆周率后200位
现在vms62和vms63下载perl镜像docker pull perl
然后在master上运行
[root@vms61 chap8-job]# kubectl create job job2 --image=perl -- perl -Mbignum=bpi -wle 'print bpi(200)'
job.batch/job2 created
[root@vms61 chap8-job]# kubectl get jobs
NAME COMPLETIONS DURATION AGE
job2 1/1 4s 4s
[root@vms61 chap8-job]# kubectl get pods
NAME READY STATUS RESTARTS AGE
job2-gggqz 0/1 Completed 0 7s
[root@vms61 chap8-job]# kubectl get pods -w
NAME READY STATUS RESTARTS AGE
job2-gggqz 0/1 Completed 0 13s
^C[root@vms61 chap8-job]# kubectl logs -f job2-gggqz
3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303820
[root@vms61 chap8-job]# kubectl create cronjob mycronjob1 --image=busybox --schedule="*/1 * * * *" --dry-run=client -o yaml -- date > mycj1.yaml
[root@vms61 chap8-job]# cat mycj1.yaml
apiVersion: batch/v1beta1
kind: CronJob
metadata:
creationTimestamp: null
name: mycronjob1
spec:
schedule: '*/1 * * * *'
jobTemplate:
metadata:
creationTimestamp: null
name: mycronjob1
spec:
template:
metadata:
creationTimestamp: null
spec:
containers:
- command: ["sh","-c","date ; sleep 10"]
image: busybox
imagePullPolicy: IfNotPresent
name: mycronjob1
resources: {}
restartPolicy: OnFailure
status: {}
[root@vms61 chap8-job]# kubectl apply -f mycj1.yaml
cronjob.batch/mycronjob1 created
[root@vms61 chap8-job]# kubectl get pods
No resources found in chap8-job namespace.
[root@vms61 chap8-job]# kubectl get jobs
No resources found in chap8-job namespace.
[root@vms61 chap8-job]# kubectl get pods
NAME READY STATUS RESTARTS AGE
mycronjob1-1601805180-4x9qs 1/1 Running 0 2s
[root@vms61 chap8-job]# kubectl get pods
NAME READY STATUS RESTARTS AGE
mycronjob1-1601805180-4x9qs 0/1 Completed 0 37s
[root@vms61 chap8-job]# kubectl get pods
NAME READY STATUS RESTARTS AGE
mycronjob1-1601805180-4x9qs 0/1 Completed 0 62s
mycronjob1-1601805240-w48dq 0/1 ContainerCreating 0 1s
[root@vms61 chap8-job]# kubectl get pods
NAME READY STATUS RESTARTS AGE
mycronjob1-1601805180-4x9qs 0/1 Completed 0 72s
mycronjob1-1601805240-w48dq 0/1 Completed 0 11s
正文完