Job主要用于负责批量处理短暂的一次性任务
Job的特点:
Job的资源清单模板
apiVersion: batch/v1
kind: Job
metadata:
name: pod-controller # job名称
namespace: dev # job所属命名空间
labels: # 给job打标签
controller: job
spec:
completions: 4 # 指定Job需要成功运行Pod的总次数,默认为1
parallelism: 2 # 指定Job在任一时刻能够并发运行Pod的数量,默认为1
activeDeadlineSeconds: 60 # 指定Job可以运行的时间期限,超过时间还没结束,系统将会尝试进行终止
backoffLimit: 6 # 指定Job失败后进行重试的次数,默认为6
manualSelector: true # 是否可以使用selector选择器选择Pod,默认为false
selector: # 选择器,通过该控制器管理哪些pod
matchLabels: # Labels匹配规则。和matchExpressions类似
app: busybox-pod
matchExpressions: # Expressions匹配规则。和matchLabels类似
- {key: app, operator: In, values: ["busybox-pod"]}
template: # pod副本创建模板。属性和Pod的属性一样
metadata:
labels:
app: busybox-pod
spec:
restartPolicy: Never # 重启策略只能设置为Never或OnFailure
containers:
- name: busybox
image: busybox:latest
command: ["/bin/sh", "-c", "for i in 1 2 3 4 5 6; do echo $i ; sleep 5; done"]
关于模板中的重启策略的说明:
新建pod-controller.yaml,内容如下。并运行Job
[root@k8s-master ~]# cat pod-controller.yaml
apiVersion: batch/v1
kind: Job
metadata:
name: pod-controller
namespace: dev
labels:
controller: job
spec:
completions: 4
parallelism: 2
manualSelector: true
selector:
matchLabels:
app: busybox-pod
template:
metadata:
labels:
app: busybox-pod
spec:
restartPolicy: Never
containers:
- name: busybox
image: busybox:latest
command: ["/bin/sh", "-c", "for i in 1 2 3 4 5 6; do echo $i ; sleep 5; done"]
[root@k8s-master ~]#
[root@k8s-master ~]# kubectl apply -f pod-controller.yaml
job.batch/pod-controller created
[root@k8s-master ~]#
[root@k8s-master ~]# kubectl get job -n dev -o wide
NAME COMPLETIONS DURATION AGE CONTAINERS IMAGES SELECTOR
pod-controller 0/4 20s 20s busybox busybox:latest app=busybox-pod
[root@k8s-master ~]#
[root@k8s-master ~]# kubectl get pod -n dev -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
pod-controller-jnjr4 1/1 Running 0 25s 10.244.169.136 k8s-node2
pod-controller-qt5s5 1/1 Running 0 25s 10.244.169.135 k8s-node2
[root@k8s-master ~]#
[root@k8s-master ~]# kubectl get job -n dev -o wide
NAME COMPLETIONS DURATION AGE CONTAINERS IMAGES SELECTOR
pod-controller 4/4 115s 119s busybox busybox:latest app=busybox-pod
[root@k8s-master ~]#
[root@k8s-master ~]# kubectl delete job pod-controller -n dev
job.batch "pod-controller" deleted
[root@k8s-master ~]#
CronJob控制器以Job控制器为其管控对象,并借助它管理Pod资源对象。Job控制器定义的作业任务在其控制器资源创建之后便会立即执行,但CronJob可以以类似Linux操作系统的周期性任务作业计划的方式,控制器运行时间点及重复运行的方式
CronJob的资源清单模板
apiVersion: batch/v1beta1
kind: CronJob
metadata:
name: pod-controller # CronJob名称
namespace: dev # CronJob所属的命名空间
labels: # 给CronJob打标签
controller: cronjob
spec:
schedule: "*/3 * * * * " # cron格式的Job调度运行时间点,用于控制Job定时运行时间
concurrencyPolicy: Forbid # Job并发执行策略
failedJobsHistoryLimit: 3 # 为失败的Job保留的历史记录数,默认为1
successfulJobsHistoryLimit: 3 # 为成功的Job保留的历史记录数,默认为3
jobTemplate: # job控制器模板,用于为cronjob控制器生成job对象
metadata: {}
spec:
completions: 4 # 指定Job需要成功运行Pod的总次数,默认为1
parallelism: 2 # 指定Job在任一时刻能够并发运行Pod的数量,默认为1
activeDeadlineSeconds: 60 # 指定Job可以运行的时间期限,超过时间还没结束,系统将会尝试进行终止
backoffLimit: 6 # 指定Job失败后进行重试的次数,默认为6
template: # pod副本创建模板。属性和Pod的属性一样
spec:
restartPolicy: Never # 重启策略只能设置为Never或OnFailure
containers:
- name: busybox
image: busybox:latest
command: [ "/bin/sh", "-c", "for i in 1 2 3 4 5 6; do echo $i; sleep 5; done" ]
schedule属性说明:cron表达式,用于指定Job的执行时间
*/1 * * * *:依次表示分钟 小时 日 月份 星期
分钟的值从0到59
小时的值从0到23
日的值从1到31
月的值从1到12
星期的值从0到6,0表示星期日
多个时间可以用逗号,隔开。范围可以用连字符-给出。* 可以作为通配符。/表示每时间单位
concurrencyPolicy属性说明:Job并发执行策略
新建pod-controller.yaml,内容如下。并运行CronJob
[root@k8s-master ~]# cat pod-controller.yaml
apiVersion: batch/v1
kind: CronJob
metadata:
name: pod-controller
namespace: dev
labels:
controller: cronjob
spec:
schedule: "*/3 * * * * "
concurrencyPolicy: Forbid
jobTemplate:
metadata: {}
spec:
completions: 4
parallelism: 2
template:
spec:
restartPolicy: Never
containers:
- name: busybox
image: busybox:latest
command: [ "/bin/sh", "-c", "for i in 1 2 3 4 5 6; do echo $i; sleep 5; done" ]
[root@k8s-master ~]#
[root@k8s-master ~]# kubectl apply -f pod-controller.yaml
cronjob.batch/pod-controller created
[root@k8s-master ~]#
会根据failedJobsHistoryLimit和successfulJobsHistoryLimit两个参数设置的值,自动对历史Job和Pod进行删除
[root@k8s-master ~]# kubectl get cronjob -n dev -o wide
NAME SCHEDULE SUSPEND ACTIVE LAST SCHEDULE AGE CONTAINERS IMAGES SELECTOR
pod-controller */3 * * * * False 1 18s 2m25s busybox busybox:latest
[root@k8s-master ~]#
[root@k8s-master ~]# kubectl get job -n dev -o wide
NAME COMPLETIONS DURATION AGE CONTAINERS IMAGES SELECTOR
pod-controller-27550290 2/4 23s 23s busybox busybox:latest controller-uid=c5263d73-0614-4326-9ce1-2ad10f09e2fe
[root@k8s-master ~]#
[root@k8s-master ~]# kubectl get pod -n dev -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
pod-controller-27550290-sd9bs 1/1 Running 0 26s 10.244.169.138 k8s-node2
pod-controller-27550290-srsfs 1/1 Running 0 26s 10.244.169.140 k8s-node2
[root@k8s-master ~]#
[root@k8s-master ~]# kubectl delete cronjob pod-controller -n dev
cronjob.batch "pod-controller" deleted
[root@k8s-master ~]#