目录
一、job控制器
二、cronjob控制器
一、job控制器
job控制器就是一次性任务的pod控制器,pod完成作业后不会重启,其重启策略是:Never;
简单案例
启动一个pod,执行完成一个事件,然后pod关闭;
例如:计算π的值,取前500位;
# 编辑资源清单
[root@k8s1 job]# cat job.yaml
apiVersion: batch/v1
kind: Job
metadata:
name: job-pi
spec:
#定义pod模板
template:
spec:
containers:
- name: c1
image: perl:5.34
#执行任务命令,计算π值
command: ["perl", "-Mbignum=bpi", "-wle", "print bpi(500)"]
restartPolicy: Never
#指定标记此次作业失败之前的重复次数,不写默认6次
backoffLimit: 4
# 创建job资源
[root@k8s1 job]# kubectl apply -f job.yaml
job.batch/job-pi created
# 查看pod
[root@k8s1 job]# kubectl get pod
NAME READY STATUS RESTARTS AGE
job-pi-j846r 0/1 Completed 0 32s
# 查看日志
[root@k8s1 job]# kubectl logs -f job-pi-j846r
3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491
二、cronjob控制器
cronjob控制器,是基于job控制器,定期频率性执行任务;与linux系统中的crontab一样;
# 编辑crontab资源清单
[root@k8s1 job]# cat cronjob.yaml
apiVersion: batch/v1
kind: CronJob
metadata:
name: oslee
spec:
schedule: "* * * * *"
#定义job的模板
jobTemplate:
spec:
template:
spec:
containers:
- name: c1
image: alpine:latest
#执行命令,也是任务
command:
- /bin/sh
- -c
- date; echo Thus, it has become art;
restartPolicy: OnFailure
# 创建crontab资源
[root@k8s1 job]# kubectl apply -f cronjob.yaml
cronjob.batch/oslee created