一、job概念(一次性任务)
Job负责批量处理短暂的一次性任务 (short lived one-off tasks),即仅执行一次的任务,它保证批处理任务的一个或多个Pod成功结束。
Kubernetes支持以下几种Job:
非并行Job:通常创建一个Pod直至其成功结束
固定结束次数的Job:设置.spec.completions,创建多个Pod,直到.spec.completions个Pod成功结束
带有工作队列的并行Job:设置.spec.Parallelism但不设置.spec.completions,当所有Pod结束并且至少一个成功时,Job就认为是成功
二、CronJob概念(定时任务)
CronJob即定时任务,就类似于Linux系统的crontab,在指定的时间周期运行指定的任务。在Kubernetes 1.5,使用CronJob需要开启batch/v2alpha1 API,即–runtime-config=batch/v2alpha1。
CronJob Spec
.spec.schedule指定任务运行周期,格式同Cron
.spec.jobTemplate指定需要运行的任务,格式同Job
.spec.startingDeadlineSeconds指定任务开始的截止期限
.spec.concurrencyPolicy指定任务的并发策略,支持Allow、Forbid和Replace三个选项
三、job示例
[root@master-191 ~]# kubectl get pod,svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
svc/kubernetes ClusterIP 10.254.0.1 <none> 443/TCP 24d
[root@master-191 ~]# vim job.yaml
apiVersion: batch/v1
kind: Job
metadata:
name: pi
spec:
template:
spec:
containers:
- name: pi
image: perl
command: ["perl", "-Mbignum=bpi", "-wle", "print bpi(2000)"]
restartPolicy: Never
backoffLimit: 4
[root@master-191 ~]# kubectl create -f job.yaml
job "pi" created
[root@master-191 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
pi-c92zx 0/1 ContainerCreating 0 7s
[root@master-191 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
pi-c92zx 1/1 Running 0 1m
#通过日志,可查看计算的圆周率值,想看到这个效果要快点做,执行完pod就删除了,当然删除了以后,往上面翻记录,找到pod名称也能看到日志。
[root@master-191 ~]# kubectl logs -f pi-c92zx
3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196442
#成功执行后,pod资源已经自动删除
[root@master-191 ~]# kubectl get pod
No resources found, use --show-all to see completed objects.
#查看job执行状态
[root@master-191 ~]# kubectl get jobs
NAME DESIRED SUCCESSFUL AGE
pi 1 1 1m
四、cronjob示例
[root@master-191 ~]# vim cronjob.yaml
apiVersion: batch/v1beta1
kind: CronJob
metadata:
name: hello
spec:
schedule: "*/1 * * * *"
jobTemplate:
spec:
template:
spec:
containers:
- name: hello
image: busybox
args:
- /bin/sh
- -c
- date; echo Hello You xiaoqing Happy new year.
restartPolicy: OnFailure
[root@master-191 ~]# kubectl create -f cronjob.yaml
cronjob "hello" created
###看不到pod,我用的是k8s1.9.9版本,高版本的或许能看见pod的状态由running到completed,这个是正常运行一次结束的状态。
[root@master-191 ~]# kubectl get pod
No resources found.
[root@master-191 ~]# kubectl get pod
No resources found, use --show-all to see completed objects.
[root@master-191 ~]# kubectl get cronjob
NAME SCHEDULE SUSPEND ACTIVE LAST SCHEDULE AGE
hello */1 * * * * False 0 34s 1m
[root@master-191 ~]# kubectl delete -f cronjob.yaml
cronjob "hello" deleted
[root@master-191 ~]# kubectl get pod
No resources found.
[root@master-191 ~]# kubectl get cronjob
No resources found.
七分钟左右时,神奇的看见了一次效果。这个第七分钟不是一定的,看运气的哦。一直查看pod状态,连续2分钟,应该能看到一次效果。
[root@master-191 ~]# kubectl get cronjob
NAME SCHEDULE SUSPEND ACTIVE LAST SCHEDULE AGE
hello */1 * * * * False 0 1m 7m
[root@master-191 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
hello-1612683780-xjb2d 0/1 ContainerCreating 0 1s
[root@master-191 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
hello-1612683780-xjb2d 0/1 ContainerCreating 0 3s
[root@master-191 ~]# kubectl get pod
No resources found, use --show-all to see completed objects.
[root@master-191 ~]# kubectl logs -f hello-1612683780-xjb2d
Sun Feb 7 07:43:11 UTC 2021
Hello You xiaoqing Happy new year.
查阅资料发现,这条命令可以看到创建的容器,但看不见echo的输出,因为容器执行完立即就删除了。
[root@master-191 ~]# kubectl get job --show-all
NAME DESIRED SUCCESSFUL AGE
hello-1612685340 1 1 2m
hello-1612685400 1 1 1m
hello-1612685460 1 1 45s
[root@master-191 ~]# kubectl logs -f hello-1612685340
Error from server (NotFound): pods "hello-1612685340" not found
kubenetes官网:https://kubernetes.io/zh/docs
kubenetes社区:https://www.kubernetes.org.cn
kuboard.cn官网:https://kuboard.cn/learning