一、Job控制器简介
1.1 Job控制器简介
Job,主要用于负责批量处理短暂的一次性任务,Job特点如下:
- 当Job创建的Pod执行成功时,job将几乎成功结束的pod数量
- 当成功结束的pod达到指定的数量时,job将完成执行
1.2 Job控制器资源文件
apiVersion: batch/v1 # 版本号
kind: Job # 类型
metadata: # 元数据
name: # rs名称
namespae: # 所属命名空间
labels: # 标签
controller: job
spec: # 详情描述
completions: 1 # 指定Job需要成功运行Pod的次数,默认值为1
parallelism: 1 # 指定Job在同一时刻应该并发运行Pod的数量,默认值为1
activeDeadlineSeconds: 30 # 指定Job可运行的时间期限,超过时间还为结束,系统将会尝试进行终止
backoffLimit: 6 # 指定Job失败后进行重试的次数,默认值为6
manualSelector: true # 是否可以使用selector选择器选择pod,默认是false
selector: # 选择器,通过它指定该控制器管理哪些Pod
matchLabels: # Label匹配规则
app: controller-pod
matchExpressions: # Expressions匹配项
- {key: app, operator: In, values: [controller-pod]}
template: # 模板,当副本数量不足时,会根据下面的模板创建pod副本
metadata:
labels:
app: controller-pod
spec:
restartPolicy: Never # 重启策略只能设置为Never或者OnFailure
containers:
- name: counter
- image: nginx:1.17.1
二、Job控制器实例
2.1 Job控制器实例演示
编辑pc_job.yaml文件,内容如下
apiVersion: v1
kind: Namespace
metadata:
name: dev
---
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和pod的状态变化
# 监视job的窗口
kubectl get job -n dev -o wide -w
# 监视pod的窗口
kubectl get pod -n dev -o wide -w
然后使用如下命令创建资源
[root@master pod_controller]# kubectl apply -f pc_job.yaml
namespace/dev created
job.batch/pc-job created
[root@master pod_controller]#
然后可以看到job的变化如下:
[root@master ~]# kubectl get job -n dev -o wide -w
NAME COMPLETIONS DURATION AGE CONTAINERS IMAGES SELECTOR
pc-job 0/1 0s counter busybox:1.30 app=counter-pod
pc-job 0/1 0s 0s counter busybox:1.30 app=counter-pod
pc-job 1/1 28s 28s counter busybox:1.30 app=counter-pod
而pod的变化如下:
[root@master ~]# kubectl get pod -n dev -o wide -w
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
pc-job-hn5gj 0/1 Pending 0 0s <none> <none> <none> <none>
pc-job-hn5gj 0/1 Pending 0 0s <none> node2 <none> <none>
pc-job-hn5gj 0/1 ContainerCreating 0 0s <none> node2 <none> <none>
pc-job-hn5gj 1/1 Running 0 1s 10.244.2.105 node2 <none> <none>
pc-job-hn5gj 0/1 Completed 0 28s 10.244.2.105 node2 <none> <none>
2.2 设置总数和并行参数的Job控制器实例
编辑pc_job.yaml文件,内容如下,需要创建6个pod,允许并行执行3个
apiVersion: v1
kind: Namespace
metadata:
name: dev
---
apiVersion: batch/v1
kind: Job
metadata:
name: pc-job
namespace: dev
spec:
manualSelector: true
completions: 6
parallelism: 3
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和pod的状态变化
# 监视job的窗口
kubectl get job -n dev -o wide -w
# 监视pod的窗口
kubectl get pod -n dev -o wide -w
然后使用如下命令创建资源
[root@master pod_controller]# kubectl apply -f pc_job.yaml
namespace/dev created
job.batch/pc-job created
[root@master pod_controller]#
job的变化过程如下:
[root@master ~]# kubectl get job -n dev -o wide -w
NAME COMPLETIONS DURATION AGE CONTAINERS IMAGES SELECTOR
pc-job 0/6 0s counter busybox:1.30 app=counter-pod
pc-job 0/6 0s 0s counter busybox:1.30 app=counter-pod
pc-job 1/6 28s 28s counter busybox:1.30 app=counter-pod
pc-job 2/6 28s 28s counter busybox:1.30 app=counter-pod
pc-job 3/6 29s 29s counter busybox:1.30 app=counter-pod
pc-job 4/6 57s 57s counter busybox:1.30 app=counter-pod
pc-job 5/6 57s 57s counter busybox:1.30 app=counter-pod
pc-job 6/6 57s 57s counter busybox:1.30 app=counter-pod
pod的变化过程如下:
[root@master ~]# kubectl get pod -n dev -o wide -w
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
pc-job-m7q4d 0/1 Pending 0 0s <none> <none> <none> <none>
pc-job-jwtsg 0/1 Pending 0 0s <none> <none> <none> <none>
pc-job-7hg9g 0/1 Pending 0 0s <none> <none> <none> <none>
pc-job-m7q4d 0/1 Pending 0 0s <none> node2 <none> <none>
pc-job-jwtsg 0/1 Pending 0 0s <none> node1 <none> <none>
pc-job-7hg9g 0/1 Pending 0 0s <none> node1 <none> <none>
pc-job-m7q4d 0/1 ContainerCreating 0 0s <none> node2 <none> <none>
pc-job-jwtsg 0/1 ContainerCreating 0 0s <none> node1 <none> <none>
pc-job-7hg9g 0/1 ContainerCreating 0 0s <none> node1 <none> <none>
pc-job-jwtsg 1/1 Running 0 1s 10.244.1.89 node1 <none> <none>
pc-job-7hg9g 1/1 Running 0 1s 10.244.1.88 node1 <none> <none>
pc-job-m7q4d 1/1 Running 0 2s 10.244.2.106 node2 <none> <none>
pc-job-7hg9g 0/1 Completed 0 28s 10.244.1.88 node1 <none> <none>
pc-job-vdsgl 0/1 Pending 0 0s <none> <none> <none> <none>
pc-job-jwtsg 0/1 Completed 0 28s 10.244.1.89 node1 <none> <none>
pc-job-vdsgl 0/1 Pending 0 0s <none> node1 <none> <none>
pc-job-vtln5 0/1 Pending 0 0s <none> <none> <none> <none>
pc-job-vdsgl 0/1 ContainerCreating 0 0s <none> node1 <none> <none>
pc-job-vtln5 0/1 Pending 0 0s <none> node1 <none> <none>
pc-job-vtln5 0/1 ContainerCreating 0 1s <none> node1 <none> <none>
pc-job-m7q4d 0/1 Completed 0 29s 10.244.2.106 node2 <none> <none>
pc-job-d77m9 0/1 Pending 0 0s <none> <none> <none> <none>
pc-job-d77m9 0/1 Pending 0 0s <none> node2 <none> <none>
pc-job-d77m9 0/1 ContainerCreating 0 0s <none> node2 <none> <none>
pc-job-vtln5 1/1 Running 0 1s 10.244.1.90 node1 <none> <none>
pc-job-vdsgl 1/1 Running 0 2s 10.244.1.91 node1 <none> <none>
pc-job-d77m9 1/1 Running 0 1s 10.244.2.107 node2 <none> <none>
pc-job-vtln5 0/1 Completed 0 29s 10.244.1.90 node1 <none> <none>
pc-job-vdsgl 0/1 Completed 0 29s 10.244.1.91 node1 <none> <none>
pc-job-d77m9 0/1 Completed 0 28s 10.244.2.107 node2 <none> <none>
可以看出,确实可以实现创建6个任务,同时并发3个执行,总体执行时间大概两个周期即58秒左右即可完成