96 lines
2.9 KiB
Markdown
96 lines
2.9 KiB
Markdown
|
<h1><center>kubernetes工作负载资源Job</center></h1>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
------
|
|||
|
|
|||
|
## 一:Job
|
|||
|
|
|||
|
Job 会创建一个或者多个 Pods,并将继续重试 Pods 的执行,直到指定数量的 Pods 成功终止。 随着 Pods 成功结束,Job 跟踪记录成功完成的 Pods 个数。 当数量达到指定的成功个数阈值时,任务(即 Job)结束。 删除 Job 的操作会清除所创建的全部 Pods。 挂起 Job 的操作会删除 Job 的所有活跃 Pod,直到 Job 被再次恢复执行。一种简单的使用场景下,你会创建一个 Job 对象以便以一种可靠的方式运行某 Pod 直到完成。 当第一个 Pod 失败或者被删除(比如因为节点硬件失效或者重启)时,Job 对象会启动一个新的 Pod。
|
|||
|
|
|||
|
#### 1.创建Job
|
|||
|
|
|||
|
```shell
|
|||
|
[root@master xingdian]# cat Jobs.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
|
|||
|
```
|
|||
|
|
|||
|
#### 2.运行
|
|||
|
|
|||
|
```shell
|
|||
|
[root@master xingdian]# kubectl create -f Jobs.yaml
|
|||
|
```
|
|||
|
|
|||
|
#### 3.查看运行结果
|
|||
|
|
|||
|
```shell
|
|||
|
[root@master xingdian]# kubectl get job
|
|||
|
NAME COMPLETIONS DURATION AGE
|
|||
|
pi 1/1 7m37s 145m
|
|||
|
```
|
|||
|
|
|||
|
#### 4.查看详细信息
|
|||
|
|
|||
|
```shell
|
|||
|
[root@master xingdian]# kubectl describe jobs/pi
|
|||
|
Name: pi
|
|||
|
Namespace: default
|
|||
|
Selector: controller-uid=98846cab-bb0c-430d-b577-519602c5636d
|
|||
|
Labels: controller-uid=98846cab-bb0c-430d-b577-519602c5636d
|
|||
|
job-name=pi
|
|||
|
Annotations: batch.kubernetes.io/job-tracking:
|
|||
|
Parallelism: 1
|
|||
|
Completions: 1
|
|||
|
Completion Mode: NonIndexed
|
|||
|
Start Time: Mon, 02 May 2022 20:47:48 +0800
|
|||
|
Completed At: Mon, 02 May 2022 20:55:25 +0800
|
|||
|
Duration: 7m37s
|
|||
|
Pods Statuses: 0 Active / 1 Succeeded / 0 Failed
|
|||
|
Pod Template:
|
|||
|
Labels: controller-uid=98846cab-bb0c-430d-b577-519602c5636d
|
|||
|
job-name=pi
|
|||
|
Containers:
|
|||
|
pi:
|
|||
|
Image: perl
|
|||
|
Port: <none>
|
|||
|
Host Port: <none>
|
|||
|
Command:
|
|||
|
perl
|
|||
|
-Mbignum=bpi
|
|||
|
-wle
|
|||
|
print bpi(2000)
|
|||
|
Environment: <none>
|
|||
|
Mounts: <none>
|
|||
|
Volumes: <none>
|
|||
|
Events: <none>
|
|||
|
```
|
|||
|
|
|||
|
#### 5.编写 Job 规约
|
|||
|
|
|||
|
与 Kubernetes 中其他资源的配置类似,Job 也需要 `apiVersion`、`kind` 和 `metadata` 字段
|
|||
|
|
|||
|
Job 的名字必须是合法的DNS 子域名
|
|||
|
|
|||
|
Job 配置还需要一个`.spec` 节
|
|||
|
|
|||
|
#### 6.Pod 模版
|
|||
|
|
|||
|
Job 的 `.spec` 中只有 `.spec.template` 是必需的字段
|
|||
|
|
|||
|
字段 `.spec.template` 的值是一个Pod 模版。 其规范与Pod完全相同,只是其中不再需要 `apiVersion` 或 `kind` 字段
|
|||
|
|
|||
|
Job 中的 Pod 模版必需设置合适的标签和合适的重启策略
|
|||
|
|
|||
|
Job 中 Pod 的`RestartPolicy`只能设置为 `Never` 或 `OnFailure` 之一
|