Kubernetes入门实验:job

作者注:本文仅为笔者学习记录,不具任何参考意义。

k8s job 实验。
注:本文为笔者实验记录,非教程,另会不定时更新。

环境

1
2
3
4
5
# kubectl get node
NAME STATUS ROLES AGE VERSION
edge-node Ready <none> 15m v1.17.0
edge-node2 Ready <none> 16m v1.17.0
ubuntu Ready master 67d v1.17.0

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就认为是成功

技术总结

简单示例

配置示例job.yaml,循环打印数字:

1
2
3
4
5
6
7
8
9
10
11
12
13
apiVersion: batch/v1
kind: Job
metadata:
name: busybox-job
spec:
template:
spec:
containers:
- name: busybox
image: latelee/busybox
command: ["sh", "-c", "for i in {1..5}; do echo $i; sleep 1; done"]
restartPolicy: Never
backoffLimit: 4

创建:

1
kubectl apply -f job.yaml

查看:

1
2
kubectl get pod
kubectl describe pod busybox-job-slx5h

等待片刻,完成:

1
2
3
# kubectl get pod
NAME READY STATUS RESTARTS AGE
busybox-job-slx5h 0/1 Completed 0 39s

查看结果:

1
2
# kubectl logs busybox-job-slx5h
{1..5}

(似乎非预期)

固定次数

配置文件job3.yaml:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
apiVersion: batch/v1
kind: Job
metadata:
name: busybox-job3
spec:
completions: 3
template:
spec:
containers:
- name: busybox
image: latelee/busybox
command: ["sh", "-c", "for i in {1..5}; do echo $i; sleep 1; done"]
restartPolicy: Never
backoffLimit: 4

创建:

1
kubectl apply -f job3.yaml

查看:

1
2
kubectl get pod
kubectl describe pod busybox-job3-n9x8q

等待片刻,完成:

1
2
3
4
5
6
# kubectl get pod
NAME READY STATUS RESTARTS AGE
busybox-job-slx5h 0/1 Completed 0 12m // 上一次的
busybox-job3-n9x8q 0/1 Completed 0 68s // 第一次
busybox-job3-t7spj 0/1 Completed 0 21s // !!第三次
busybox-job3-w7br7 0/1 Completed 0 49s // !!第二次

(pod名称按字符排序,需注意)

删除:

1
kubectl delete -f ../job  // 同一目录有多个yaml,且均需删除,使用之

定时任务

命令行方式:

1
kubectl run hello --schedule="*/1 * * * *" --restart=OnFailure --image=latelee/busybox -- /bin/sh -c "date; echo Hello from the Kubernetes cluster"

注:仅演示,不建议实际使用。

配置文件 cronjob.yaml:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
apiVersion: batch/v1beta1
kind: CronJob
metadata:
name: hello
spec:
schedule: "*/1 * * * *"
jobTemplate:
spec:
template:
spec:
containers:
- name: hello
image: latelee/busybox
args:
- /bin/sh
- -c
- date; echo Hello from the Kubernetes cluster
restartPolicy: OnFailure

创建:

1
kubectl apply -f cronjob.yaml

查看:

1
2
kubectl get cronjob hello
kubectl describe cronjob hello

查看job过程:

1
2
3
4
5
6
7
8
9
# kubectl get cronjob
NAME SCHEDULE SUSPEND ACTIVE LAST SCHEDULE AGE
hello */1 * * * * False 0 <none> 32s

// 等等片刻后

# kubectl get cronjob hello
NAME SCHEDULE SUSPEND ACTIVE LAST SCHEDULE AGE
hello */1 * * * * False 1 22s 2m9s

(发现,ACTIVE在0和1之间变化)

监控job过程:

1
2
3
4
5
6
# kubectl get jobs --watch
NAME COMPLETIONS DURATION AGE
hello-1583853360 1/1 29s 3m12s
hello-1583853420 1/1 17s 2m12s
hello-1583853480 1/1 17s 72s
hello-1583853540 0/1 12s 12s

查看log(必须等待任务被调度到):

1
2
3
4
5
6
7
# kubectl get pods
# kubectl logs hello-1583853900-4jbtv
Tue Mar 10 15:25:23 UTC 2020
Hello from the Kubernetes cluster
# kubectl logs hello-1583853960-h2fdk
Tue Mar 10 15:26:23 UTC 2020
Hello from the Kubernetes cluster

(注意比较日志的时间,恰好1分钟)

删除:

1
kubectl delete cronjob hello