Docker&Kubernetes ❀ Kubernetes集群Pod控制器 - Job
Posted 无糖可乐没有灵魂
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Docker&Kubernetes ❀ Kubernetes集群Pod控制器 - Job相关的知识,希望对你有一定的参考价值。
文章目录
1、基本概念与控制器特点
Job:主要用于负责批量处理(一次性处理指定数量任务)短暂的一次性(每个任务仅运行一次就结束)任务;
特点如下:
- 当Job创建的Pod执行成功时,Job将记录为成功结束的Pod数量;
- 当成功结束的Pod达到指定的数量时,Job将完成执行;
2、资源配置清单
参数查询方法:
[root@master ~]# kubectl explain job
参数汇总梳理:
apiVersion: batch/v1
kind: Job
metadata:
name: pc-job
namespace: dev
labels:
controller: job
spec:
completions: 6 #指定job需要成功运行Pod的次数,默认值为1
parallelism: 3 #指定job在任一时刻应该并发运行pod的数量,默认值为1
activeDeadlineSeconds: 30 #指定job可运行的时间期限,超过时间未结束系统会尝试终止
backoffLimit: 6 #指定job失败后进行重试的次数,默认为6
manualSelector: true #是否可以使用selector选择器选择pod,默认为false
selector: #pod选择器,指定控制那些pod
matchLabels:
app: count-pod
matchExpressions:
- key: "app"
operator: In
values: ["nginx-pod"]
template: #模板,当副本数量不足时,会根据以下配置创建副本
metadata:
labels:
app: counter-pod
spec:
restartPolicy: Never #重启策略,支持Nerver或OnFailure
containers:
- name: counter
image: busybox:latest
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,failer次数不会改变;
- 如果指定为Never,则Job会在Pod出现故障时创建新的Pod,并且故障Pod不会消失,failed次数加1;
- 如果指定为Always,则一直重启,意味着Job任务会重复执行,因此不能设置为Always;
3、创建控制器
下面主要介绍两个使用,首先使用Job创建单个Pod以观察Pod创建的具体过程,其次创建多个Pod,使其进行并发创建,然后观察创建过程;
3.1 Job控制单个Pod
此实验建议开启三个终端,第一个终端执行调用文件命令,第二个终端监控Job控制器,第三个终端监控Pod;
#创建YAML文件
[root@master ~]# cat 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:latest
command: ["/bin/sh","-c","for i in 9 8 7 6 5 4 3 2 1;do echo $i;sleep 2;done"]
#调用YAML文件
[root@master ~]# kubectl apply -f pc-job.yaml
job.batch/pc-job created
#监控Job控制器
[root@master ~]# kubectl get job -n dev -w
NAME COMPLETIONS DURATION AGE
pc-job 0/1 0s
pc-job 0/1 0s 0s
pc-job 0/1 35s 35s
pc-job 1/1 35s 35s
#监控Pod
NAME READY STATUS RESTARTS AGE
pc-job-qtw84 0/1 Pending 0 0s
pc-job-qtw84 0/1 Pending 0 0s
pc-job-qtw84 0/1 ContainerCreating 0 0s
pc-job-qtw84 0/1 ContainerCreating 0 1s
pc-job-qtw84 1/1 Running 0 17s
pc-job-qtw84 0/1 Completed 0 35s
pc-job-qtw84 0/1 Completed 0 35s
pc-job-qtw84 0/1 Completed 0 35s
3.2 Job控制多个并发Pod
此实验仍然建议开启三个终端,第一个终端执行调用文件命令,第二个终端监控Job控制器,第三个终端监控Pod;
#创建YAML文件
[root@master ~]# vim pc-job.yam
apiVersion: batch/v1
kind: Job
metadata:
name: pc-job
namespace: dev
spec:
manualSelector: true
completions: 6 #指定job需要成功运行Pod的次数,默认值为1
parallelism: 3 #指定job在任一时刻应该并发运行pod的数量,默认值为1
selector:
matchLabels:
app: counter-pod
template:
metadata:
labels:
app: counter-pod
spec:
restartPolicy: Never
containers:
- name: counter
image: busybox:latest
command: ["/bin/sh","-c","for i in 9 8 7 6 5 4 3 2 1;do echo $i;sleep 2;done"]
#调用YAML文件
[root@master ~]# kubectl apply -f pc-job.yaml
job.batch/pc-job created
#监控Job控制器
[root@master ~]# kubectl get job -n dev -w
NAME COMPLETIONS DURATION AGE
pc-job 0/6 0s
pc-job 0/6 0s 0s
pc-job 0/6 35s 35s
pc-job 1/6 35s 35s
pc-job 1/6 36s 36s
pc-job 2/6 36s 36s
pc-job 2/6 52s 52s
pc-job 3/6 52s 52s
pc-job 3/6 71s 71s
pc-job 4/6 71s 71s
pc-job 4/6 71s 71s
pc-job 5/6 71s 71s
pc-job 5/6 87s 87s
pc-job 6/6 87s 87s
#监控Pod
[root@master ~]# kubectl get pod -n dev -w
NAME READY STATUS RESTARTS AGE
pc-job-jllsm 0/1 Pending 0 0s
pc-job-882tp 0/1 Pending 0 0s
pc-job-xblq4 0/1 Pending 0 0s
#与配置参数符合,一次性创建3个Pod执行Job任务,共6个任务,创建2次
pc-job-882tp 0/1 Pending 0 0s
pc-job-jllsm 0/1 Pending 0 0s
pc-job-xblq4 0/1 Pending 0 0s
pc-job-882tp 0/1 ContainerCreating 0 0s
pc-job-xblq4 0/1 ContainerCreating 0 0s
pc-job-jllsm 0/1 ContainerCreating 0 0s
pc-job-882tp 0/1 ContainerCreating 0 1s
pc-job-jllsm 0/1 ContainerCreating 0 2s
pc-job-xblq4 0/1 ContainerCreating 0 2s
pc-job-882tp 1/1 Running 0 17s
pc-job-jllsm 1/1 Running 0 18s
pc-job-xblq4 1/1 Running 0 34s
pc-job-882tp 0/1 Completed 0 35s
pc-job-8scd8 0/1 Pending 0 0s
pc-job-8scd8 0/1 Pending 0 0s
pc-job-882tp 0/1 Completed 0 35s
pc-job-8scd8 0/1 ContainerCreating 0 0s
pc-job-882tp 0/1 Completed 0 35s
pc-job-jllsm 0/1 Completed 0 36s
pc-job-n8hd9 0/1 Pending 0 0s
pc-job-n8hd9 0/1 Pending 0 0s
pc-job-n8hd9 0/1 ContainerCreating 0 0s
pc-job-jllsm 0/1 Completed 0 36s
pc-job-jllsm 0/1 Completed 0 36s
pc-job-8scd8 0/1 ContainerCreating 0 1s
pc-job-n8hd9 0/1 ContainerCreating 0 1s
pc-job-xblq4 0/1 Completed 0 52s
pc-job-zs97t 0/1 Pending 0 0s
pc-job-xblq4 0/1 Completed 0 52s
pc-job-zs97t 0/1 Pending 0 0s
pc-job-zs97t 0/1 ContainerCreating 0 0s
pc-job-xblq4 0/1 Completed 0 52s
pc-job-8scd8 1/1 Running 0 17s
pc-job-zs97t 0/1 ContainerCreating 0 1s
pc-job-n8hd9 1/1 Running 0 17s
pc-job-zs97t 1/1 Running 0 17s
pc-job-8scd8 0/1 Completed 0 36s
pc-job-8scd8 0/1 Completed 0 36s
pc-job-8scd8 0/1 Completed 0 36s
pc-job-n8hd9 0/1 Completed 0 35s
pc-job-n8hd9 0/1 Completed 0 35s
pc-job-n8hd9 0/1 Completed 0 35s
pc-job-zs97t 0/1 Completed 0 35s
pc-job-zs97t 0/1 Completed 0 35s
pc-job-zs97t 0/1 Completed 0 35s
4、删除控制器
[root@master ~]# kubectl delete -f pc-job.yaml
job.batch "pc-job" deleted
以上是关于Docker&Kubernetes ❀ Kubernetes集群Pod控制器 - Job的主要内容,如果未能解决你的问题,请参考以下文章
Docker&Kubernetes ❀ Kubernetes集群安装部署过程与常见的错误解决方法
Docker&Kubernetes ❀ Kubernetes集群安装部署过程与常见的错误解决方法
Docker&Kubernetes ❀ Kubernetes集群实践与部署笔记知识点梳理
Docker&Kubernetes ❀ Docker 容器技术笔记链接梳理