前言
容器分为两类
- 在线业务 – 服务类容器
须要继续不中断的提供服务,容器须要始终运行
- 离线业务 – 工作类容器
个别执行一次性工作,比方统计日志数据等,运行实现后容器即可敞开
更新历史
- 20200618 – 初稿 – 左程立
- 原文地址 – https://blog.zuolinux.com/2020/06/18/controller-job-cronjob.html
Job
cat job.yaml
apiVersion: batch/v1
kind: Job
metadata:
name: job-test
spec:
template:
metadata:
name: job-test
spec:
containers:
- name: test-job
image: busybox
command: ["echo", "test job!"]
restartPolicy: Never
参数
restartPolicy 只能是 Never 或者 onFailure
启动
[root@master01 ~]# kubectl apply -f job.yaml
job.batch/job-test created
运行状态
[root@master01 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
job-test-t2gbw 0/1 ContainerCreating 0 12s
[root@master01 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
job-test-t2gbw 0/1 Completed 0 24s
查看运行后果
[root@master01 ~]# kubectl logs job-test-t2gbw
test job!
Job 执行失败如何解决
将 command 胡乱设置导致 job 无奈胜利启动
command: ["echo123", "test job!"]
restartPolicy: Never
当 restartPolicy: Never 时,能够看到 k8s 在一直的开新的 pod
但不会让它始终开上来,默认参数 spec.backoffLimit: 6 会进行阻止
[root@master01 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
job-test-2zrt4 0/1 ContainerCannotRun 0 87s
job-test-5z884 0/1 ContainerCannotRun 0 77s
job-test-cxgmm 0/1 ContainerCannotRun 0 107s
job-test-nt9gt 0/1 ContainerCannotRun 0 57s
job-test-wxv7l 0/1 ContainerCreating 0 17s
当 restartPolicy: OnFailure 时,k8s 不开新 pod,只会一直重启 pod
[root@master01 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
job-test-bd695 0/1 CrashLoopBackOff 4 3m15s
如果 job 没有报错,然而它始终不完结怎么办?
参数 spec.activeDeadlineSeconds: 100 会在 100s 后将 Job 中所有 Pod 进行敞开
此时 Pod 敞开状态为 reason: DeadlineExceeded
并行 Job
cat job.yaml
apiVersion: batch/v1
kind: Job
metadata:
name: job-test
spec:
parallelism: 2
......
参数
parallelism: 2 仅容许最多两个 Pod 同时运行
查看运行状况
[root@master01 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
job-test-6ttz9 0/1 ContainerCreating 0 8s
job-test-mknjc 0/1 ContainerCreating 0 8s
[root@master01 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
job-test-6ttz9 0/1 Completed 0 68s
job-test-mknjc 0/1 Completed 0 68s
[root@master01 ~]# kubectl get job
NAME COMPLETIONS DURATION AGE
job-test 0/1 of 2 10s 10s
[root@master01 ~]# kubectl get job
NAME COMPLETIONS DURATION AGE
job-test 2/1 of 2 19s 66s
减少参数 completions
apiVersion: batch/v1
kind: Job
metadata:
name: job-test
spec:
parallelism: 2
completions: 8
参数
completions: 8 示意至多胜利运行 8 个 pod
如果不是 8 个胜利,那么会依据 restartPolicy 的策略进行解决
能够认为是一种查看机制
查看运行状况
[root@master01 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
job-test-6xwpt 0/1 ContainerCreating 0 8s
job-test-grk4q 0/1 ContainerCreating 0 8s
[root@master01 ~]# kubectl get job
NAME COMPLETIONS DURATION AGE
job-test 0/8 11s 11s
[root@master01 ~]# kubectl get job
NAME COMPLETIONS DURATION AGE
job-test 6/8 76s 76s
[root@master01 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
job-test-2jxqx 0/1 Completed 0 28s
job-test-6xwpt 0/1 Completed 0 90s
job-test-gndq7 0/1 Completed 0 70s
job-test-grk4q 0/1 Completed 0 90s
job-test-n96k6 0/1 Completed 0 70s
job-test-np4n7 0/1 Completed 0 49s
job-test-scc9c 0/1 Completed 0 28s
job-test-zcnbp 0/1 Completed 0 49s
[root@master01 ~]# kubectl get job
NAME COMPLETIONS DURATION AGE
job-test 8/8 82s 91s
CronJob
CronJob 会定时 create 一个 job 对象
cat cronjob.yaml
apiVersion: batch/v1beta1
kind: CronJob
metadata:
name: hello
spec:
schedule: "*/1 * * * *"
jobTemplate:
spec:
template:
spec:
containers:
- name: hello
image: busybox
command: ["echo","test cron job!"]
restartPolicy: OnFailure
查看运行后果
[root@master01 ~]# kubectl get cronjob
NAME SCHEDULE SUSPEND ACTIVE LAST SCHEDULE AGE
hello */1 * * * * False 0 67s 3m45s
[root@master01 ~]# kubectl get jobs
NAME COMPLETIONS DURATION AGE
hello-1595319480 1/1 27s 3m23s
hello-1595319540 1/1 19s 2m23s
hello-1595319600 1/1 24s 83s
hello-1595319660 0/1 22s 22s
[root@master01 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
hello-1595319480-2kl8h 0/1 Completed 0 3m21s
hello-1595319540-gwthv 0/1 Completed 0 2m21s
hello-1595319600-54548 0/1 Completed 0 81s
hello-1595319660-8xqn6 0/1 ContainerCreating 0 20s
能够看到,就是把后面说过的 Job 对象进行定时的运行。
结束语
Job/CronJob 控制器能够进行离线业务的解决,不须要长期运行一个 Pod。
这类控制器治理的 Pod 不对外提供继续在线服务,工作运行实现后,即会将容器进行敞开,能够用于一些定时数据处理类的工作。
分割我
微信公众号:zuolinux_com