Kubernetes K8S之资源控制器Job和CronJob详解
Kubernetes的资源控制器Job和CronJob详解与示例
主机配置规划
服务器名称(hostname) | 系统版本 | 配置 | 内网IP | 外网IP(模拟) |
---|---|---|---|---|
k8s-master | CentOS7.7 | 2C/4G/20G | 172.16.1.110 | 10.0.0.110 |
k8s-node01 | CentOS7.7 | 2C/4G/20G | 172.16.1.111 | 10.0.0.111 |
k8s-node02 | CentOS7.7 | 2C/4G/20G | 172.16.1.112 | 10.0.0.112 |
什么是控制器
kubernetes中内建了很多controller(控制器),这些相当于一个状态机,用来控制pod的具体状态和行为。
- ReplicationController 和 ReplicaSet
- Deployment
- DaemonSet
- StatefulSet
- Job/CronJob
- HorizontalPodAutoscaler
Job
负责批处理任务
Job创建一个或多个Pod,并确保指定数量的Pod成功终止。Pod成功完成后,Job将跟踪成功完成的情况。当达到指定的成功完成次数时,任务(即Job)就完成了。删除Job将清除其创建的Pod。
一个简单的情况是创建一个Job对象,以便可靠地运行一个Pod来完成。如果第一个Pod发生故障或被删除(例如,由于节点硬件故障或节点重启),则Job对象将启动一个新的Pod。
当然还可以使用Job并行运行多个Pod。
Job终止和清理
Job完成后,不会再创建其他Pod,但是Pod也不会被删除。这样使我们仍然可以查看已完成容器的日志,以检查是否有错误、警告或其他诊断输出。Job对象在完成后也将保留下来,以便您查看其状态。
当我们删除Job对象时,对应的pod也会被删除。
特殊说明
- 单个Pod时,默认Pod成功运行后Job即结束
- restartPolicy 仅支持Never和OnFailure
- .spec.completions 标识Job结束所需要成功运行的Pod个数,默认为1
- .spec.parallelism 标识并行运行的Pod个数,默认为1
- .spec.activeDeadlineSeconds 为Job的持续时间,不管有多少Pod创建。一旦工作到指定时间,所有的运行pod都会终止且工作状态将成为type: Failed与reason: DeadlineExceeded。
CronJob
Cron Job 创建是基于时间调度的 Jobs
一个 CronJob 对象就像 crontab (cron table) 文件中的一行。它用 Cron 格式进行编写,并周期性地在给定的调度时间执行 Job。
CronJob 限制
CronJob 创建 Job 对象,每个 Job 的执行次数大约为一次。 之所以说 “大约” ,是因为在某些情况下,可能会创建两个 Job,或者不会创建任何 Job。虽然试图使这些情况尽量少发生,但不能完全杜绝。因此,Job 应该是幂等的。
CronJob 仅负责创建与其调度时间相匹配的 Job,而 Job 又负责管理其代表的 Pod。
使用案例:
1、在给定时间点调度Job
2、创建周期性运行的Job。如:数据备份、数仓导数、执行任务、邮件发送、数据拉取、数据推送
特殊说明
.spec.schedule 必选,任务被创建和执行的调度时间。同Cron格式串,例如 0 * * * *。
- .spec.jobTemplate 必选,任务模版。它和 Job的语法完全一样
- .spec.startingDeadlineSeconds 可选的。默认未设置。它表示任务如果由于某种原因错过了调度时间,开始该任务的截止时间的秒数。过了截止时间,CronJob 就不会开始任务。不满足这种最后期限的任务会被统计为失败任务。如果没有该声明,那任务就没有最后期限。
- .spec.concurrencyPolicy 可选的。它声明了 CronJob 创建的任务执行时发生重叠如何处理。spec 仅能声明下列规则中的一种:
Allow (默认):CronJob 允许并发任务执行。
Forbid:CronJob 不允许并发任务执行;如果新任务的执行时间到了而老任务没有执行完,CronJob 会忽略新任务的执行。
Replace:如果新任务的执行时间到了而老任务没有执行完,CronJob 会用新任务替换当前正在运行的任务。
请注意,并发性规则仅适用于相同 CronJob 创建的任务。如果有多个 CronJob,它们相应的任务总是允许并发执行的。
- .spec.suspend 可选的。如果设置为 true ,后续发生的执行都会挂起。这个设置对已经开始执行的Job不起作用。默认是关闭的false。
备注:在调度时间内挂起的执行都会被统计为错过的任务。当 .spec.suspend 从 true 改为 false 时,且没有开始的最后期限,错过的任务会被立即调度。 - .spec.successfulJobsHistoryLimit 和 .spec.failedJobsHistoryLimit 可选的。 这两个声明了有多少执行完成和失败的任务会被保留。默认设置为3和1。限制设置为0代表相应类型的任务完成后不会保留。
说明:如果 startingDeadlineSeconds 设置为很大的数值或未设置(默认),并且 concurrencyPolicy 设置为 Allow,则作业将始终至少运行一次。
Job示例
yaml文件
1 [root@k8s-master controller]# pwd 2 /root/k8s_practice/controller 3 [root@k8s-master controller]# cat job.yaml 4 apiVersion: batch/v1 5 kind: Job 6 metadata: 7 name: pi 8 spec: 9 #completions: 3 # 标识Job结束所需要成功运行的Pod个数,默认为1 10 template: 11 spec: 12 containers: 13 - name: pi 14 image: registry.cn-beijing.aliyuncs.com/google_registry/perl:5.26 15 command: ["perl", "-Mbignum=bpi", "-wle", "print bpi(2000)"] 16 restartPolicy: Never 17 backoffLimit: 4
创建job,与状态查看
1 [root@k8s-master controller]# kubectl apply -f job.yaml 2 job.batch/pi created 3 [root@k8s-master controller]# kubectl get job -o wide 4 NAME COMPLETIONS DURATION AGE CONTAINERS IMAGES SELECTOR 5 pi 0/1 16s 16s pi registry.cn-beijing.aliyuncs.com/google_registry/perl:5.26 controller-uid=77004357-fd5e-4395-9bbb-cd0698e19cb9 6 [root@k8s-master controller]# kubectl get pod -o wide 7 NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES 8 pi-6zvm5 0/1 ContainerCreating 0 85s <none> k8s-node01 <none> <none>
之后再次查看
1 [root@k8s-master controller]# kubectl get job -o wide 2 NAME COMPLETIONS DURATION AGE CONTAINERS IMAGES SELECTOR 3 pi 1/1 14m 44m pi registry.cn-beijing.aliyuncs.com/google_registry/perl:5.26 controller-uid=77004357-fd5e-4395-9bbb-cd0698e19cb9 4 [root@k8s-master controller]# kubectl get pod -o wide 5 NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES 6 pi-6zvm5 0/1 Completed 0 44m 10.244.4.63 k8s-node01 <none> <none> 7 [root@k8s-master controller]# 8 [root@k8s-master controller]# kubectl describe job pi 9 Name: pi 10 Namespace: default 11 Selector: controller-uid=76680f6f-442c-4a09-91dc-c3d4c18465b0 12 Labels: controller-uid=76680f6f-442c-4a09-91dc-c3d4c18465b0 13 job-name=pi 14 Annotations: kubectl.kubernetes.io/last-applied-configuration: 15 {"apiVersion":"batch/v1","kind":"Job","metadata":{"annotations":{},"name":"pi","namespace":"default"},"spec":{"backoffLimit":4," 16 Parallelism: 1 17 Completions: 1 18 Start Time: Tue, 11 Aug 2020 23:34:44 +0800 19 Completed At: Tue, 11 Aug 2020 23:35:02 +0800 20 Duration: 18s 21 Pods Statuses: 0 Running / 1 Succeeded / 0 Failed 22 Pod Template: 23 Labels: controller-uid=76680f6f-442c-4a09-91dc-c3d4c18465b0 24 job-name=pi 25 Containers: 26 pi: 27 Image: registry.cn-beijing.aliyuncs.com/google_registry/perl:5.26 28 Port: <none> 29 Host Port: <none> 30 Command: 31 perl 32 -Mbignum=bpi 33 -wle 34 print bpi(2000) 35 Environment: <none> 36 Mounts: <none> 37 Volumes: <none> 38 Events: 39 Type Reason Age From Message 40 ---- ------ ---- ---- ------- 41 Normal SuccessfulCreate 2m33s job-controller Created pod: pi-6zvm5
并查看 Pod 的标准输出
1 [root@k8s-master controller]# kubectl logs --tail 500 pi-6zvm5 2 3.141592653589793238462643383279502884197169399375105820974944592307816406………………
CronJob示例
yaml文件
1 [root@k8s-master controller]# pwd 2 /root/k8s_practice/controller 3 [root@k8s-master controller]# cat cronjob.yaml 4 apiVersion: batch/v1beta1 5 kind: CronJob 6 metadata: 7 name: hello 8 spec: 9 schedule: "*/1 * * * *" 10 jobTemplate: 11 spec: 12 template: 13 spec: 14 containers: 15 - name: hello 16 image: registry.cn-beijing.aliyuncs.com/google_registry/busybox:1.24 17 args: 18 - /bin/sh 19 - -c 20 - date; echo Hello from the Kubernetes cluster 21 restartPolicy: OnFailure
启动cronjob并查看状态
1 [root@k8s-master controller]# kubectl apply -f cronjob.yaml 2 cronjob.batch/hello created 3 [root@k8s-master controller]# kubectl get cronjob -o wide 4 NAME SCHEDULE SUSPEND ACTIVE LAST SCHEDULE AGE CONTAINERS IMAGES SELECTOR 5 hello */1 * * * * False 1 8s 27s hello registry.cn-beijing.aliyuncs.com/google_registry/busybox:1.24 <none> 6 [root@k8s-master controller]# 7 [root@k8s-master controller]# kubectl get job -o wide 8 NAME COMPLETIONS DURATION AGE CONTAINERS IMAGES SELECTOR 9 hello-1590721020 1/1 2s 21s hello registry.cn-beijing.aliyuncs.com/google_registry/busybox:1.24 controller-uid=9e0180e8-8362-4a58-8b93-089b92774b5e 10 [root@k8s-master controller]# 11 [root@k8s-master controller]# kubectl get pod -o wide 12 NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES 13 hello-1590721020-m4fr8 0/1 Completed 0 36s 10.244.4.66 k8s-node01 <none> <none>
几分钟之后的状态信息
1 [root@k8s-master controller]# kubectl get cronjob -o wide 2 NAME SCHEDULE SUSPEND ACTIVE LAST SCHEDULE AGE CONTAINERS IMAGES SELECTOR 3 hello */1 * * * * False 0 55s 7m14s hello registry.cn-beijing.aliyuncs.com/google_registry/busybox:1.24 <none> 4 [root@k8s-master controller]# 5 [root@k8s-master controller]# 6 [root@k8s-master controller]# kubectl get job -o wide 7 NAME COMPLETIONS DURATION AGE CONTAINERS IMAGES SELECTOR 8 hello-1590721260 1/1 1s 3m1s hello registry.cn-beijing.aliyuncs.com/google_registry/busybox:1.24 controller-uid=0676bd6d-861b-440b-945b-4b2704872728 9 hello-1590721320 1/1 2s 2m1s hello registry.cn-beijing.aliyuncs.com/google_registry/busybox:1.24 controller-uid=09c1902e-76ef-4731-b3b4-3188961c13e9 10 hello-1590721380 1/1 2s 61s hello registry.cn-beijing.aliyuncs.com/google_registry/busybox:1.24 controller-uid=f30dc159-8905-4cfc-b06b-f950c8dcfc28 11 [root@k8s-master controller]# 12 [root@k8s-master controller]# kubectl get pod -o wide 13 NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES 14 hello-1590721320-m4pxf 0/1 Completed 0 2m6s 10.244.4.70 k8s-node01 <none> <none> 15 hello-1590721380-wk7jh 0/1 Completed 0 66s 10.244.2.77 k8s-node02 <none> <none> 16 hello-1590721440-rcx7v 0/1 Completed 0 6s 10.244.4.72 k8s-node01 <none> <none> 17 [root@k8s-master controller]# 18 [root@k8s-master controller]# kubectl describe cronjob hello 19 Name: hello 20 Namespace: default 21 Labels: <none> 22 Annotations: kubectl.kubernetes.io/last-applied-configuration: 23 {"apiVersion":"batch/v1beta1","kind":"CronJob","metadata":{"annotations":{},"name":"hello","namespace":"default"},"spec":{"jobTemplate":{"... 24 Schedule: */1 * * * * 25 Concurrency Policy: Allow 26 Suspend: False 27 Successful Job History Limit: 3 28 Failed Job History Limit: 1 29 Starting Deadline Seconds: <unset> 30 Selector: <unset> 31 Parallelism: <unset> 32 Completions: <unset> 33 Pod Template: 34 Labels: <none> 35 Containers: 36 hello: 37 Image: registry.cn-beijing.aliyuncs.com/google_registry/busybox:1.24 38 Port: <none> 39 Host Port: <none> 40 Args: 41 /bin/sh 42 -c 43 date; echo Hello from the Kubernetes cluster 44 Environment: <none> 45 Mounts: <none> 46 Volumes: <none> 47 Last Schedule Time: Wed, 12 Aug 2020 00:01:00 +0800 48 Active Jobs: <none> 49 Events: 50 Type Reason Age From Message 51 ---- ------ ---- ---- ------- 52 Normal SuccessfulCreate 19m cronjob-controller Created job hello-1597160520 53 Normal SawCompletedJob 19m cronjob-controller Saw completed job: hello-1597160520, status: Complete 54 Normal SuccessfulCreate 18m cronjob-controller Created job hello-1597160580 55 Normal SawCompletedJob 18m cronjob-controller Saw completed job: hello-1597160580, status: Complete 56 Normal SuccessfulCreate 17m cronjob-controller Created job hello-1597160640 57 Normal SawCompletedJob 17m cronjob-controller Saw completed job: hello-1597160640, status: Complete 58 Normal SuccessfulCreate 16m cronjob-controller Created job hello-1597160700 59 Normal SuccessfulDelete 16m cronjob-controller Deleted job hello-1597160520 60 Normal SawCompletedJob 16m cronjob-controller Saw completed job: hello-1597160700, status: Complete 61 Normal SuccessfulCreate 15m cronjob-controller Created job hello-1597160760 62 Normal SawCompletedJob 15m cronjob-controller Saw completed job: hello-1597160760, status: Complete 63 Normal SuccessfulDelete 15m cronjob-controller Deleted job hello-1597160580 64 Normal SuccessfulCreate 14m cronjob-controller Created job hello-1597160820 65 Normal SuccessfulDelete 14m cronjob-controller Deleted job hello-1597160640 66 Normal SawCompletedJob 14m cronjob-controller Saw completed job: hello-1597160820, status: Complete 67 Normal SuccessfulCreate 13m cronjob-controller Created job hello-1597160880 68 Normal SawCompletedJob 13m cronjob-controller Saw completed job: hello-1597160880, status: Complete 69 ……………… 70 Normal SawCompletedJob 11m cronjob-controller Saw completed job: hello-1597161000, status: Complete 71 Normal SuccessfulDelete 11m cronjob-controller Deleted job hello-1597160820 72 Normal SawCompletedJob 10m cronjob-controller (combined from similar events): Saw completed job: hello-1597161060, status: Complete 73 Normal SuccessfulCreate 4m13s (x7 over 10m) cronjob-controller (combined from similar events): Created job hello-1597161420
找到最后一次调度任务创建的 Pod, 并查看 Pod 的标准输出。请注意任务名称和 Pod 名称是不同的。
1 [root@k8s-master controller]# kubectl logs pod/hello-1590721740-rcx7v # 或者 kubectl logs hello-1590721740-rcx7v 2 Fri May 29 03:09:04 UTC 2020 3 Hello from the Kubernetes cluster
删除 CronJob
1 [root@k8s-master controller]# kubectl get cronjob 2 NAME SCHEDULE SUSPEND ACTIVE LAST SCHEDULE AGE 3 hello */1 * * * * False 0 32s 19m 4 [root@k8s-master controller]# 5 [root@k8s-master controller]# kubectl delete cronjob hello # 或者 kubectl delete -f cronjob.yaml 6 cronjob.batch "hello" deleted 7 [root@k8s-master controller]# kubectl get cronjob # 可见已删除 8 No resources found in default namespace.
相关阅读
1、Kubernetes K8S之资源控制器RC、RS、Deployment详解
2、Kubernetes K8S之资源控制器StatefulSets详解
3、Kubernetes K8S之资源控制器Daemonset详解
4、官网:Jobs
完毕!
———END———
如果觉得不错就关注下呗 (-^O^-) !