1、概述
在kubernetes中,有很多类型的pod控制器,每种都有自己的适合的场景,常见的有下面这些:
-
ReplicationController:比较原始的pod控制器,已经被废弃,由ReplicaSet替代
-
ReplicaSet:保证副本数量一直维持在期望值,并支持pod数量扩缩容,镜像版本升级
-
Deployment:通过控制ReplicaSet来控制Pod,并支持滚动升级、回退版本
-
Horizontal Pod Autoscaler:可以根据集群负载自动水平调整Pod的数量,实现削峰填谷
-
DaemonSet:在集群中的指定Node上运行且仅运行一个副本,一般用于守护进程类的任务
-
Job:它创建出来的pod只要完成任务就立即退出,不需要重启或重建,用于执行一次性任务
-
Cronjob:它创建的Pod负责周期性任务控制,不需要持续后台运行
-
StatefulSet:管理有状态应用
在前面文章中我们详解了ReplicaSet、Deployment、Horizontal Pod Autoscaler、DaemonSet控制器,这篇我们详解Job
2、Job详解
Job,主要用于负责批量处理(一次要处理指定数量任务)短暂的一次性(每个任务仅运行一次就结束)任务。Job特点如下:
-
当Job创建的pod执行成功结束时,Job将记录成功结束的pod数量
-
当成功结束的pod达到指定的数量时,Job将完成执行
Job的资源清单文件:
apiVersion: batch/v1 # 版本号
kind: Job # 类型
metadata: # 元数据
name: # rs名称
namespace: # 所属命名空间
labels: #标签
controller: job
spec: # 详情描述
completions: 1 # 指定job需要成功运行Pods的次数。默认值: 1
parallelism: 1 # 指定job在任一时刻应该并发运行Pods的数量。默认值: 1
activeDeadlineSeconds: 30 # 指定job可运行的时间期限,超过时间还未结束,系统将会尝试进行终止。
backoffLimit: 6 # 指定job失败后进行重试的次数。默认是6
manualSelector: true # 是否可以使用selector选择器选择pod,默认是false
selector: # 选择器,通过它指定该控制器管理哪些pod
matchLabels: # Labels匹配规则
app: counter-pod
matchExpressions: # Expressions匹配规则
- {key: app, operator: In, values: [counter-pod]}
template: # 模板,当副本数量不足时,会根据下面的模板创建pod副本
metadata:
labels:
app: counter-pod
spec:
restartPolicy: Never # 重启策略只能设置为Never或者OnFailure
containers:
- name: counter
image: busybox:1.30
command: ["bin/sh","-c","for i in 9 8 7 6 5 4 3 2 1; do echo $i;sleep 2;done"]
关于重启策略设置的说明:
如果指定为OnFailure,则job会在pod出现故障时重启容器,而不是创建pod,failed次数不变
如果指定为Never,则job会在pod出现故障时创建新的pod,并且故障pod不会消失,也不会重启,failed次数加1
如果指定为Always的话,就意味着一直重启,意味着job任务会重复去执行了,当然不对,所以不能设置为Always
3、Job实例
3.1、实例1
创建pc-job.yaml,内容如下:
apiVersion: batch/v1
kind: Job
metadata:
name: pc-job
namespace: dev
spec:
manualSelector: true
selector:
matchLabels:
app: counter-pod
template:
metadata:
labels:
app: counter-pod
spec:
restartPolicy: Never
containers:
- name: counter
image: busybox:1.30
command: ["bin/sh","-c","for i in 9 8 7 6 5 4 3 2 1; do echo $i;sleep 3;done"]
效果:
# 创建job
[root@k8s-master ~]# kubectl apply -f pc-job.yaml
job.batch/pc-job created
# 查看job 可以发现 COMPLETIONS 完成状态 刚才是 0/1 总共1个任务,0个未完成,稍后会打印出1/1代表任务结束
[root@k8s-master ~]# kubectl get job -owide -n dev -w
NAME COMPLETIONS DURATION AGE CONTAINERS IMAGES SELECTOR
pc-job 0/1 28s 28s counter busybox:1.30 app=counter-pod
pc-job 0/1 47s 47s counter busybox:1.30 app=counter-pod
pc-job 0/1 48s 48s counter busybox:1.30 app=counter-pod
pc-job 1/1 48s 48s counter busybox:1.30 app=counter-pod
# 查看pod 状态 STATUS=Completed(完成), READY=0/1 意思是总共1个pod,有0个准备好了,因为任务已经完成所以READY的变成0
[root@k8s-master ~]# kubectl get pods -n dev -w
NAME READY STATUS RESTARTS AGE
pc-job-9rd6l 0/1 Completed 0 2m3s
# 删除job
[root@k8s-master ~]# kubectl delete -f pc-job.yaml
job.batch "pc-job" deleted
3.2、实例2
接下来,调整下pod运行的总数量和并行数量 即:在spec下设置下面两个选项
completions: 6 # 指定job需要成功运行Pods的次数为6
parallelism: 3 # 指定job并发运行Pods的数量为3
调整后yaml
apiVersion: batch/v1
kind: Job
metadata:
name: pc-job
namespace: dev
spec:
completions: 6 # 指定job需要成功运行Pods的次数为6
parallelism: 3 # 指定job并发运行Pods的数量为3
manualSelector: true
selector:
matchLabels:
app: counter-pod
template:
metadata:
labels:
app: counter-pod
spec:
restartPolicy: Never
containers:
- name: counter
image: busybox:1.30
command: ["bin/sh","-c","for i in 9 8 7 6 5 4 3 2 1; do echo $i;sleep 3;done"]
效果:
#然后重新运行job,观察效果,此时会发现,job会每次运行3个pod,总共执行了6个pod
[root@k8s-master ~]# kubectl get pod -n dev -w
NAME READY STATUS RESTARTS AGE
pc-job-g26wm 1/1 Running 0 7s
pc-job-ggccq 1/1 Running 0 7s
pc-job-znz9q 1/1 Running 0 7s
pc-job-znz9q 0/1 Completed 0 30s
pc-job-g26wm 0/1 Completed 0 30s
pc-job-ggccq 0/1 Completed 0 30s
pc-job-g26wm 0/1 Completed 0 31s
pc-job-znz9q 0/1 Completed 0 31s
pc-job-ggccq 0/1 Completed 0 31s
pc-job-g26wm 0/1 Completed 0 32s
pc-job-znz9q 0/1 Completed 0 32s
pc-job-ggccq 0/1 Completed 0 32s
pc-job-dmn7x 0/1 Pending 0 0s
pc-job-wfgtr 0/1 Pending 0 0s
pc-job-dmn7x 0/1 Pending 0 0s
pc-job-cnz45 0/1 Pending 0 0s
pc-job-wfgtr 0/1 Pending 0 0s
pc-job-cnz45 0/1 Pending 0 0s
pc-job-dmn7x 0/1 ContainerCreating 0 0s
pc-job-znz9q 0/1 Completed 0 32s
pc-job-ggccq 0/1 Completed 0 32s
pc-job-g26wm 0/1 Completed 0 32s
pc-job-wfgtr 0/1 ContainerCreating 0 0s
pc-job-cnz45 0/1 ContainerCreating 0 1s
pc-job-cnz45 0/1 ContainerCreating 0 1s
pc-job-wfgtr 0/1 ContainerCreating 0 1s
pc-job-dmn7x 0/1 ContainerCreating 0 2s
pc-job-wfgtr 1/1 Running 0 2s
pc-job-cnz45 1/1 Running 0 2s
pc-job-dmn7x 1/1 Running 0 3s
pc-job-cnz45 0/1 Completed 0 28s
pc-job-dmn7x 0/1 Completed 0 29s
pc-job-wfgtr 0/1 Completed 0 29s
pc-job-cnz45 0/1 Completed 0 29s
pc-job-cnz45 0/1 Completed 0 30s
pc-job-dmn7x 0/1 Completed 0 30s
pc-job-wfgtr 0/1 Completed 0 30s
pc-job-cnz45 0/1 Completed 0 31s
pc-job-dmn7x 0/1 Completed 0 31s
pc-job-wfgtr 0/1 Completed 0 31s
pc-job-dmn7x 0/1 Completed 0 32s
pc-job-wfgtr 0/1 Completed 0 32s
总结:Job只能运行一次性任务,如果要运行周期行任务还得是CronJob(CJ),下篇我们继续实例详解CronJob(CJ)。