赞
踩
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
- # 创建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-master01 ~]# kubectl create -f pc-job.yaml
- job.batch/pc-job created
- # 查看job
- [root@k8s-master01 ~]# kubectl get job -n dev -o wide -w
- NAME COMPLETIONS DURATION AGE CONTAINERS IMAGES SELECTOR
- pc-job 0/1 21s 21s counter busybox:1.30 app=counter-pod
- pc-job 1/1 31s 79s counter busybox:1.30 app=counter-pod
- # 通过观察pod状态可以看到,pod在运⾏完毕任务后,就会变成Completed状态
- [root@k8s-master01 ~]# kubectl get pods -n dev -w
- NAME READY STATUS RESTARTS AGE
- pc-job-rxg96 1/1 Running 0 29s
- pc-job-rxg96 0/1 Completed 0 33s
-
- # 接下来,调整下pod运⾏的总数量和并⾏数量 即:在spec下设置下⾯两个选项
- # completions: 6 # 指定job需要成功运⾏Pods的次数为6
- # parallelism: 3 # 指定job并发运⾏Pods的数量为3
- # 然后重新运⾏job,观察效果,此时会发现,job会每次运⾏3个pod,总共执⾏了6个pod
- [root@k8s-master01 ~]# kubectl get pods -n dev -w
- NAME READY STATUS RESTARTS AGE
- pc-job-684ft 1/1 Running 0 5s
- pc-job-jhj49 1/1 Running 0 5s
- pc-job-pfcvh 1/1 Running 0 5s
- pc-job-684ft 0/1 Completed 0 11s
- pc-job-v7rhr 0/1 Pending 0 0s
- pc-job-v7rhr 0/1 Pending 0 0s
- pc-job-v7rhr 0/1 ContainerCreating 0 0s
- pc-job-jhj49 0/1 Completed 0 11s
- pc-job-fhwf7 0/1 Pending 0 0s
- pc-job-fhwf7 0/1 Pending 0 0s
- pc-job-pfcvh 0/1 Completed 0 11s
- pc-job-5vg2j 0/1 Pending 0 0s
- pc-job-fhwf7 0/1 ContainerCreating 0 0s
- pc-job-5vg2j 0/1 Pending 0 0s
- pc-job-5vg2j 0/1 ContainerCreating 0 0s
- pc-job-fhwf7 1/1 Running 0 2s
- pc-job-v7rhr 1/1 Running 0 2s
- pc-job-5vg2j 1/1 Running 0 3s
- pc-job-fhwf7 0/1 Completed 0 12s
- pc-job-v7rhr 0/1 Completed 0 12s
- pc-job-5vg2j 0/1 Completed 0 12s
- # 删除job
- [root@k8s-master01 ~]# kubectl delete -f pc-job.yaml
- job.batch "pc-job" deleted
CronJob控制器以Job控制器资源为其管控对象,并借助它管理pod资源对象,Job控制器定义的作业任务在其控制器资源创建之后便会立即执行,但CronJob可以以类似于Linux操作系统的周期性任务作业计划的方式控制其运行时间点及重复运行的方式。也就是说,CronJob可以在特定的时间点(反复的)去运行job任务。
- apiVersion: batch/v1beta1 # 版本号
- kind: CronJob # 类型
- metadata: # 元数据
- name: # rs名称
- namespace: # 所属命名空间
- labels: #标签
- controller: cronjob
- spec: # 详情描述
- schedule: # cron格式的作业调度运⾏时间点,⽤于控制任务在什么时间执⾏
- concurrencyPolicy: # 并发执⾏策略,⽤于定义前⼀次作业运⾏尚未完成时是否以及如何运⾏后⼀次的作业
- failedJobHistoryLimit: # 为失败的任务执⾏保留的历史记录数,默认为1
- successfulJobHistoryLimit: # 为成功的任务执⾏保留的历史记录数,默认为3
- startingDeadlineSeconds: # 启动作业错误的超时时⻓
- jobTemplate: # job控制器模板,⽤于为cronjob控制器⽣成job对象;下⾯其实就是job的定义
- metadata:
- spec:
- completions: 1
- parallelism: 1
- activeDeadlineSeconds: 30
- backoffLimit: 6
- manualSelector: true
- selector:
- matchLabels:
- app: counter-pod
- matchExpressions: 规则
- - {key: app, operator: In, values: [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 20;done"]
-
-
- #需要重点解释的⼏个选项:
- schedule: cron表达式,⽤于指定任务的执⾏时间
- */1 * * * *
- <分钟> <⼩时> <⽇> <⽉份> <星期>
- 分钟 值从 0 到 59.
- ⼩时 值从 0 到 23.
- ⽇ 值从 1 到 31.
- ⽉ 值从 1 到 12.
- 星期 值从 0 到 6, 0 代表星期⽇
- 多个时间可以⽤逗号隔开; 范围可以⽤连字符给出;*可以作为通配符; /表示每...
-
-
- concurrencyPolicy:
- Allow: 允许Jobs并发运⾏(默认)
- Forbid: 禁⽌并发运⾏,如果上⼀次运⾏尚未完成,则跳过下⼀次运⾏
- Replace: 替换,取消当前正在运⾏的作业并⽤新作业替换它
- # 创建pc-cronjob.yaml,内容如下
- apiVersion: batch/v1beta1
- kind: CronJob
- metadata:
- name: pc-cronjob
- namespace: dev
- labels:
- controller: cronjob
- spec:
- schedule: "*/1 * * * *"
- jobTemplate:
- metadata:
- spec:
- template:
- 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"]
-
- # 创建cronjob
- [root@k8s-master01 ~]# kubectl create -f pc-cronjob.yaml
- cronjob.batch/pc-cronjob created
- # 查看cronjob
- [root@k8s-master01 ~]# kubectl get cronjobs -n dev
- NAME SCHEDULE SUSPEND ACTIVE LAST SCHEDULE AGE
- pc-cronjob */1 * * * * False 0 <none> 6s
- # 查看job
- [root@k8s-master01 ~]# kubectl get jobs -n dev
- NAME COMPLETIONS DURATION AGE
- pc-cronjob-1592587800 1/1 28s 3m26s
- pc-cronjob-1592587860 1/1 28s 2m26s
- pc-cronjob-1592587920 1/1 28s 86s
- # 查看pod
- [root@k8s-master01 ~]# kubectl get pods -n dev
- pc-cronjob-1592587800-x4tsm 0/1 Completed 0 2m24s
- pc-cronjob-1592587860-r5gv4 0/1 Completed 0 84s
- pc-cronjob-1592587920-9dxxq 1/1 Running 0 24s
- # 删除cronjob
- [root@k8s-master01 ~]# kubectl delete -f pc-cronjob.yaml
- cronjob.batch "pc-cronjob" deleted
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。