前言
- 本篇来学习Pod控制器-Job
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
Job使用
- 创建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@master ~]# kubectl create -f pc-job.yaml job.batch/pc-job created # 查看job [root@master ~]# kubectl get job -n dev -o wide -w NAME COMPLETIONS DURATION AGE CONTAINERS IMAGES SELECTOR pc-job 0/1 10s 10s counter busybox:1.30 app=counter-pod pc-job 1/1 29s 29s counter busybox:1.30 app=counter-pod # 通过观察pod状态可以看到,pod在运行完毕任务后,就会变成Completed状态 [root@master ~]# kubectl get pods -n dev -w NAME READY STATUS RESTARTS AGE nginx-778cb5fb7b-2q6zf 1/1 Running 0 2d pc-job-5s9pw 0/1 Completed 0 8m4s # 删除job [root@master ~]# kubectl delete -f pc-job.yaml job.batch "pc-job" deleted [root@master ~]# # 接下来,调整下pod运行的总数量和并行数量 即:在spec下设置下面两个选项 # completions: 6 # 指定job需要成功运行Pods的次数为6 # parallelism: 3 # 指定job并发运行Pods的数量为3 # 然后重新运行job,观察效果,此时会发现,job会每次运行3个pod,总共执行了6个pod [root@master ~]# kubectl create -f pc-job.yaml job.batch/pc-job created [root@master ~]# kubectl get pods -n dev -w NAME READY STATUS RESTARTS AGE nginx-778cb5fb7b-2q6zf 1/1 Running 0 2d1h pc-job-mfzjv 1/1 Running 0 7s pc-job-n9qkv 1/1 Running 0 7s pc-job-vhpjs 1/1 Running 0 7s pc-job-n9qkv 0/1 Completed 0 29s pc-job-25m8h 0/1 Pending 0 1s pc-job-25m8h 0/1 Pending 0 1s pc-job-vhpjs 0/1 Completed 0 30s pc-job-4xjwg 0/1 Pending 0 0s pc-job-25m8h 0/1 ContainerCreating 0 1s pc-job-4xjwg 0/1 Pending 0 0s pc-job-mfzjv 0/1 Completed 0 30s pc-job-gm8zc 0/1 Pending 0 0s pc-job-gm8zc 0/1 Pending 0 0s pc-job-4xjwg 0/1 ContainerCreating 0 0s pc-job-gm8zc 0/1 ContainerCreating 0 0s pc-job-25m8h 1/1 Running 0 2s pc-job-4xjwg 1/1 Running 0 2s pc-job-gm8zc 1/1 Running 0 2s pc-job-4xjwg 0/1 Completed 0 28s pc-job-gm8zc 0/1 Completed 0 28s pc-job-25m8h 0/1 Completed 0 29s # 删除job [root@master ~]# kubectl delete -f pc-job.yaml job.batch "pc-job" deleted