官方文档:概念 | 工作负载 | 工作负载资源 | ReplicaSet
ReplicaSet 是通过一组字段来定义的,包括一个用来识别可获得的 Pod 的集合的选择算符、一个用来标明应该维护的副本个数的数值、一个用来指定应该创建新 Pod 以满足副本个数条件时要使用的 Pod 模板等等。 每个 ReplicaSet 都通过根据需要创建和删除 Pod 以使得副本个数达到期望值, 进而实现其存在价值。当 ReplicaSet 需要创建新的 Pod 时,会使用所提供的 Pod 模板。
ReplicaSet 通过 Pod 上的 metadata.ownerReferences 字段连接到附属 Pod,该字段给出当前对象的属主资源。 ReplicaSet 所获得的 Pod 都在其 ownerReferences 字段中包含了属主 ReplicaSet 的标识信息。正是通过这一连接,ReplicaSet 知道它所维护的 Pod 集合的状态, 并据此计划其操作行为。
ReplicaSet 使用其选择算符来辨识要获得的 Pod 集合。如果某个 Pod 没有 OwnerReference 或者其 OwnerReference 不是一个 控制器,且其匹配到 某 ReplicaSet 的选择算符,则该 Pod 立即被此 ReplicaSet 获得。
Replication Controller和ReplicaSet
ReplicaSet 是下一代的 Replication Controller,官方推荐使用ReplicaSet。
[root@k8s-1 ~]# vim rs.yaml
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: replicaset-example
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
[root@k8s-1 ~]# kubectl get rs
NAME DESIRED CURRENT READY AGE
replicaset-example 3 3 3 8m52s
[root@k8s-1 ~]# kubectl get pod --show-labels
NAME READY STATUS RESTARTS AGE LABELS
eplicaset-example-gkpll 1/1 Running 0 2m18s app=nginx
eplicaset-example-p85kh 1/1 Running 0 2m18s app=nginx
eplicaset-example-xwt4r 1/1 Running 0 2m18s app=nginx
查看 Pods 的属主引用被设置为前端的 ReplicaSet。 要实现这点,可取回运行中的 Pods 之一的 YAML
输出将类似这样,ReplicaSet 的信息被设置在 metadata 的 ownerReferences 字段中
pod 的名字由 RS- 随机数 组成,从下图的 ownerReference 可以查看到当前 pod 由 RS 创建
[root@k8s-1 ~]# kubectl get pod replicaset-example-gkpll -o yaml | less
[root@k8s-1 ~]# vim rs.yaml
replicas: 6
[root@k8s-1 ~]# kubectl apply -f rs.yaml
replicaset.apps/eplicaset-example configured
[root@k8s-1 ~]# kubectl get pod --show-labels
NAME READY STATUS RESTARTS AGE LABELS
eplicaset-example-d75lr 1/1 Running 0 58s app=nginx
eplicaset-example-gkpll 1/1 Running 0 6m50s app=nginx
eplicaset-example-gnqv9 1/1 Running 0 58s app=nginx
eplicaset-example-p85kh 1/1 Running 0 6m50s app=nginx
eplicaset-example-xtddf 1/1 Running 0 58s app=nginx
eplicaset-example-xwt4r 1/1 Running 0 6m50s app=nginx
[root@k8s-1 ~]# kubectl get rs
NAME DESIRED CURRENT READY AGE
eplicaset-example 6 6 6 8m52s
[root@k8s-1 ~]# kubectl label pod eplicaset-example-d75lr app=demo --overwrite
pod/eplicaset-example-d75lr labeled
[root@k8s-1 ~]# kubectl get pod --show-labels
NAME READY STATUS RESTARTS AGE LABELS
eplicaset-example-d75lr 1/1 Running 0 2m41s app=demo
eplicaset-example-gkpll 1/1 Running 0 8m33s app=nginx
eplicaset-example-gnqv9 1/1 Running 0 2m41s app=nginx
eplicaset-example-j9rn9 1/1 Running 0 20s app=nginx
eplicaset-example-p85kh 1/1 Running 0 8m33s app=nginx
eplicaset-example-xtddf 1/1 Running 0 2m41s app=nginx
eplicaset-example-xwt4r 1/1 Running 0 8m33s app=nginx
[root@k8s-1 ~]# kubectl get rs
NAME DESIRED CURRENT READY AGE
eplicaset-example 6 6 6 8m52s
[root@k8s-1 ~]# kubectl describe rs eplicaset-example
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal SuccessfulCreate 9m55s replicaset-controller Created pod: eplicaset-example-gkpll
Normal SuccessfulCreate 9m55s replicaset-controller Created pod: eplicaset-example-xwt4r
Normal SuccessfulCreate 9m55s replicaset-controller Created pod: eplicaset-example-p85kh
Normal SuccessfulCreate 4m3s replicaset-controller Created pod: eplicaset-example-d75lr
Normal SuccessfulCreate 4m3s replicaset-controller Created pod: eplicaset-example-xtddf
Normal SuccessfulCreate 4m3s replicaset-controller Created pod: eplicaset-example-gnqv9
Normal SuccessfulCreate 102s replicaset-controller Created pod: eplicaset-example-j9rn9
[root@k8s-1 ~]# kubectl label pod eplicaset-example-d75lr app=nginx --overwrite
pod/eplicaset-example-d75lr labeled
[root@k8s-1 ~]# kubectl get pod --show-labels
NAME READY STATUS RESTARTS AGE LABELS
eplicaset-example-d75lr 1/1 Running 0 4m56s app=nginx
eplicaset-example-gkpll 1/1 Running 0 10m app=nginx
eplicaset-example-gnqv9 1/1 Running 0 4m56s app=nginx
eplicaset-example-p85kh 1/1 Running 0 10m app=nginx
eplicaset-example-xtddf 1/1 Running 0 4m56s app=nginx
eplicaset-example-xwt4r 1/1 Running 0 10m app=nginx
官方文档: 概念 | 工作负载 | 工作负载资源 | Deployment
一个 Deployment 为 Pod 和 ReplicaSet 提供声明式的更新能力。
你负责描述 Deployment 中的 目标状态,而 Deployment 控制器(Controller) 以受控速率更改实际状态, 使其变为期望状态。你可以定义 Deployment 以创建新的 ReplicaSet,或删除现有 Deployment, 并通过新的 Deployment 收养其资源。
以下是 Deployments 的典型用例:
创建名为 nginx-deployment(由 .metadata.name 字段标明)的 Deployment。
该 Deployment 创建三个(由 replicas 字段标明)Pod 副本。
selector 字段定义 Deployment 如何查找要管理的 Pods。 在这里,你选择在 Pod 模板中定义的标签(app: nginx)。 不过,更复杂的选择规则是也可能的,只要 Pod 模板本身满足所给规则即可。
template 字段包含以下子字段:
[root@k8s-1 ~]# vim deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: deployment-example
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
[root@k8s-1 ~]# kubectl apply -f deployment.yaml
deployment.apps/deployment-example created
在检查集群中的 Deployment 时,所显示的字段有:
请注意期望副本数是根据 .spec.replicas 字段设置 3。
[root@k8s-1 ~]# kubectl get all
NAME READY STATUS RESTARTS AGE
pod/deployment-example-6799fc88d8-6xgtr 1/1 Running 0 38s
pod/deployment-example-6799fc88d8-7bmbr 1/1 Running 0 38s
pod/deployment-example-6799fc88d8-bcn8x 1/1 Running 0 38s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 126m
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/deployment-example 3/3 3 3 38s
NAME DESIRED CURRENT READY AGE
replicaset.apps/deployment-example-6799fc88d8 3 3 3 38s
[root@k8s-1 ~]# vim deployment.yaml
[root@k8s-1 ~]# kubectl apply -f deployment.yaml
deployment.apps/deployment-example configured
[root@k8s-1 ~]# kubectl describe deployments.apps deployment-example
Pod Template:
Labels: app=nginx
Containers:
nginx:
Image: nginx:1.21.1
Port: <none>
Host Port: <none>
Environment: <none>
Mounts: <none>
Volumes: <none>
Conditions:
Type Status Reason
---- ------ ------
Available True MinimumReplicasAvailable
Progressing True NewReplicaSetAvailable
OldReplicaSets: <none>
NewReplicaSet: deployment-example-54f48578cf (3/3 replicas created)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 10m deployment-controller Scaled up replica set deployment-example-6799fc88d8 to 3
Normal ScalingReplicaSet 4m42s deployment-controller Scaled up replica set deployment-example-54f48578cf to 1
Normal ScalingReplicaSet 2m16s deployment-controller Scaled down replica set deployment-example-6799fc88d8 to 2
Normal ScalingReplicaSet 2m16s deployment-controller Scaled up replica set deployment-example-54f48578cf to 2
Normal ScalingReplicaSet 2m5s deployment-controller Scaled down replica set deployment-example-6799fc88d8 to 1
Normal ScalingReplicaSet 2m5s deployment-controller Scaled up replica set deployment-example-54f48578cf to 3
Normal ScalingReplicaSet 40s deployment-controller Scaled down replica set deployment-example-6799fc88d8 to 0
[root@k8s-1 ~]# kubectl get all
NAME READY STATUS RESTARTS AGE
pod/deployment-example-54f48578cf-hrntl 1/1 Running 0 3m1s
pod/deployment-example-54f48578cf-nx46r 1/1 Running 0 5m27s
pod/deployment-example-54f48578cf-z8d7b 1/1 Running 0 2m50s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 136m
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/deployment-example 3/3 3 3 10m
NAME DESIRED CURRENT READY AGE
replicaset.apps/deployment-example-54f48578cf 3 3 3 5m27s
replicaset.apps/deployment-example-6799fc88d8 0 0 0 10m
[root@k8s-1 ~]# vim deployment.yaml
[root@k8s-1 ~]# kubectl apply -f deployment.yaml
deployment.apps/deployment-example configured
[root@k8s-1 ~]# kubectl get all
NAME READY STATUS RESTARTS AGE
pod/deployment-example-6799fc88d8-2vkw6 1/1 Running 0 42s
pod/deployment-example-6799fc88d8-dbxkc 1/1 Running 0 48s
pod/deployment-example-6799fc88d8-jq6zg 1/1 Running 0 34s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 140m
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/deployment-example 3/3 3 3 14m
NAME DESIRED CURRENT READY AGE
replicaset.apps/deployment-example-54f48578cf 0 0 0 8m59s
replicaset.apps/deployment-example-6799fc88d8 3 3 3 14m
[root@k8s-1 ~]# kubectl get pod deployment-example-6799fc88d8-2vkw6 -o yaml | less
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: "2022-05-09T06:10:39Z"
generateName: deployment-example-6799fc88d8-
labels:
app: nginx
pod-template-hash: 6799fc88d8
name: deployment-example-6799fc88d8-2vkw6
namespace: default
ownerReferences:
- apiVersion: apps/v1
blockOwnerDeletion: true
controller: true
kind: ReplicaSet
name: deployment-example-6799fc88d8
uid: d9f42478-56f1-4f4c-8eb4-8541befa6d40
resourceVersion: "12095"
uid: 747ef794-427b-41c4-88f7-47d307a1400f
spec:
containers:
- image: nginx
imagePullPolicy: Always
name: nginx
[root@k8s-1 ~]# kubectl get rs deployment-example-6799fc88d8 -o yaml | less
apiVersion: apps/v1
kind: ReplicaSet
metadata:
annotations:
deployment.kubernetes.io/desired-replicas: "3"
deployment.kubernetes.io/max-replicas: "4"
deployment.kubernetes.io/revision: "3"
deployment.kubernetes.io/revision-history: "1"
creationTimestamp: "2022-05-09T05:57:04Z"
generation: 7
labels:
app: nginx
pod-template-hash: 6799fc88d8
name: deployment-example-6799fc88d8
namespace: default
ownerReferences:
- apiVersion: apps/v1
blockOwnerDeletion: true
controller: true
kind: Deployment
name: deployment-example
uid: 47d61e3f-3b59-4205-bd2a-08cff99fd529
resourceVersion: "12139"
uid: d9f42478-56f1-4f4c-8eb4-8541befa6d40
spec:
replicas: 3
selector:
matchLabels:
deployment 只负责管理不同版本的 RS, RS 管理 Pod 副本数
每个 RS 对应一个 deployment template 版本,同一个 replicaset 下的 Pod 版本相同
当进行版本更新时,deployment 生效后,当前 RS 会先回收现有的 Pod,再进行更新,创建新的 Pod
[root@k8s-1 ~]# kubectl edit deployments.apps deployment-example
replicas: 3 //
rollingUpdate:
maxSurge: 0
maxUnavailable: 1
写完退出即生效
拉伸副本数为 6
[root@k8s-1 ~]# kubectl scale deployment deployment-example --replicas=6
deployment.apps/deployment-example scaled
[root@k8s-1 ~]# kubectl get all
NAME READY STATUS RESTARTS AGE
pod/deployment-example-6799fc88d8-2vkw6 1/1 Running 0 9m57s
pod/deployment-example-6799fc88d8-79z4m 1/1 Running 0 2m9s
pod/deployment-example-6799fc88d8-bvxfz 1/1 Running 0 2m9s
pod/deployment-example-6799fc88d8-dbxkc 1/1 Running 0 10m
pod/deployment-example-6799fc88d8-jq6zg 1/1 Running 0 9m49s
pod/deployment-example-6799fc88d8-pp4wl 1/1 Running 0 2m9s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 149m
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/deployment-example 6/6 6 6 23m
NAME DESIRED CURRENT READY AGE
replicaset.apps/deployment-example-54f48578cf 0 0 0 18m
replicaset.apps/deployment-example-6799fc88d8 6 6 6 23m
[root@k8s-1 ~]# kubectl edit deployments.apps deployment-example
spec:
minReadySeconds: 5
progressDeadlineSeconds: 600
replicas: 6
[root@k8s-1 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
deployment-example-7cf7d6dbc8-2hqhr 1/1 Running 0 3s
deployment-example-7cf7d6dbc8-grj42 1/1 Running 0 68s
deployment-example-7cf7d6dbc8-mx965 1/1 Running 0 54s
deployment-example-7cf7d6dbc8-nxr8z 1/1 Running 0 43s
deployment-example-7cf7d6dbc8-pp56h 1/1 Running 0 17s
deployment-example-7cf7d6dbc8-sd8qz 1/1 Running 0 32s
[root@k8s-1 ~]# kubectl rollout history deployment
deployment.apps/deployment-example
REVISION CHANGE-CAUSE
2 <none>
3 <none>
4 <none>
[root@k8s-1 ~]# kubectl edit deployments.apps deployment-example
rollingUpdate:
maxSurge: 0
maxUnavailable: 2
[root@k8s-1 ~]# kubectl edit deployments.apps deployment-example
spec:
containers:
- image: nginx:1.21.1
imagePullPolicy: IfNotPresent
name: nginx
[root@k8s-1 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
deployment-example-585c7c497-cbstf 0/1 ContainerCreating 0 1s
deployment-example-585c7c497-cnwzr 1/1 Running 0 15s
deployment-example-585c7c497-gqb4q 0/1 ContainerCreating 0 1s
deployment-example-585c7c497-h8d4r 1/1 Running 0 24s
deployment-example-585c7c497-lj4n5 1/1 Running 0 13s
deployment-example-585c7c497-tx55h 1/1 Running 0 24s
deployment-example-7cf7d6dbc8-2hqhr 1/1 Terminating 0 6m5s
deployment-example-7cf7d6dbc8-grj42 1/1 Terminating 0 7m10s
[root@k8s-1 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
deployment-example-585c7c497-cbstf 1/1 Running 0 11s
deployment-example-585c7c497-cnwzr 1/1 Running 0 25s
deployment-example-585c7c497-gqb4q 1/1 Running 0 11s
deployment-example-585c7c497-h8d4r 1/1 Running 0 34s
deployment-example-585c7c497-lj4n5 1/1 Running 0 23s
deployment-example-585c7c497-tx55h 1/1 Running 0 34s
[root@k8s-1 ~]# kubectl rollout history deployment
deployment.apps/deployment-example
REVISION CHANGE-CAUSE
2 <none>
3 <none>
4 <none>
5 <none>
[root@k8s-1 ~]# kubectl get rs
NAME DESIRED CURRENT READY AGE
deployment-example-54f48578cf 0 0 0 36m
deployment-example-585c7c497 6 6 6 79s
deployment-example-6799fc88d8 0 0 0 42m
deployment-example-7cf7d6dbc8 0 0 0 8m4s
[root@k8s-1 ~]# kubectl rollout pause deployment deployment-example
deployment.apps/deployment-example paused
[root@k8s-1 ~]# kubectl edit deployments.apps deployment-example
[root@k8s-1 ~]# kubectl get rs
NAME DESIRED CURRENT READY AGE
deployment-example-54f48578cf 0 0 0 42m
deployment-example-585c7c497 6 6 6 7m29s
deployment-example-6799fc88d8 0 0 0 48m
deployment-example-7cf7d6dbc8 0 0 0 14m
[root@k8s-1 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
deployment-example-585c7c497-cbstf 1/1 Running 0 7m13s
deployment-example-585c7c497-cnwzr 1/1 Running 0 7m27s
deployment-example-585c7c497-gqb4q 1/1 Running 0 7m13s
deployment-example-585c7c497-h8d4r 1/1 Running 0 7m36s
deployment-example-585c7c497-lj4n5 1/1 Running 0 7m25s
deployment-example-585c7c497-tx55h 1/1 Running 0 7m36s
[root@k8s-1 ~]# kubectl scale deployment deployment-example --replicas=3
deployment.apps/deployment-example scaled
[root@k8s-1 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
deployment-example-585c7c497-gqb4q 1/1 Running 0 7m50s
deployment-example-585c7c497-h8d4r 1/1 Running 0 8m13s
deployment-example-585c7c497-tx55h 1/1 Running 0 8m13s
[root@k8s-1 ~]# kubectl rollout resume deployment deployment-example
deployment.apps/deployment-example resumed
[root@k8s-1 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
deployment-example-585c7c497-gqb4q 1/1 Terminating 0 8m37s
deployment-example-585c7c497-h8d4r 1/1 Running 0 9m
deployment-example-bb957bbb5-498db 0/1 ContainerCreating 0 2s
deployment-example-bb957bbb5-5wfvq 0/1 ContainerCreating 0 2s
[root@k8s-1 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
deployment-example-bb957bbb5-498db 1/1 Running 0 25s
deployment-example-bb957bbb5-5wfvq 1/1 Running 0 25s
deployment-example-bb957bbb5-mszrg 1/1 Running 0 15s
官方文档:概念 | 工作负载 | 工作负载资源 | DaemonSet
最常见的情况:DaemonSet 使得每个node上都有一个flannel网络组件
DaemonSet 确保全部(或者某些)节点上运行一个 Pod 的副本。 当有节点加入集群时, 也会为他们新增一个 Pod 。 当有节点从集群移除时,这些 Pod 也会被回收。删除 DaemonSet 将会删除它创建的所有 Pod。
DaemonSet 的一些典型用法:
在每个节点上运行集群守护进程
在每个节点上运行日志收集守护进程
在每个节点上运行监控守护进程
一种简单的用法是为每种类型的守护进程在所有的节点上都启动一个 DaemonSet。 一个稍微复杂的用法是为同一种守护进程部署多个 DaemonSet;每个具有不同的标志, 并且对不同硬件类型具有不同的内存、CPU 要求。
[root@k8s-1 ~]# vim daemonset.yaml
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: daemonset-example
labels:
k8s-app: zabbix-agent
spec:
selector:
matchLabels:
name: zabbix-agent
template:
metadata:
labels:
name: zabbix-agent
spec:
containers:
- name: zabbix-agent
image: zabbix/zabbix-agent
[root@k8s-1 ~]# kubectl apply -f daemonset.yaml
daemonset.apps/daemonset-example created
[root@k8s-1 ~]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
daemonset-example-7vm9l 1/1 Running 0 2m1s 10.244.2.23 k8s-3 <none> <none>
daemonset-example-hzmft 1/1 Running 0 2m1s 10.244.1.21 k8s-2 <none> <none>
[root@k8s-1 ~]# kubectl describe daemonsets.apps
Name: daemonset-example
Selector: name=zabbix-agent
Node-Selector: <none>
Labels: k8s-app=zabbix-agent
Annotations: deprecated.daemonset.template.generation: 1
Desired Number of Nodes Scheduled: 2
Current Number of Nodes Scheduled: 2
Number of Nodes Scheduled with Up-to-date Pods: 2
Number of Nodes Scheduled with Available Pods: 2
Number of Nodes Misscheduled: 0
Pods Status: 2 Running / 0 Waiting / 0 Succeeded / 0 Failed
Pod Template:
Labels: name=zabbix-agent
Containers:
zabbix-agent:
Image: zabbix/zabbix-agent
Port: <none>
Host Port: <none>
Environment: <none>
Mounts: <none>
Volumes: <none>
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal SuccessfulCreate 3m1s daemonset-controller Created pod: daemonset-example-hzmft
Normal SuccessfulCreate 3m1s daemonset-controller Created pod: daemonset-example-7vm9l
[root@k8s-1 ~]# kubectl edit daemonsets.apps
updateStrategy:
rollingUpdate:
maxSurge: 0
maxUnavailable: 1
type: OnDelete
[root@k8s-1 ~]# kubectl edit daemonsets.apps daemonset-example
spec:
containers:
- image: zabbix/zabbix-agent2
imagePullPolicy: Always
name: zabbix-agent
[root@k8s-1 ~]# kubectl delete pod daemonset-example-7vm9l
[root@k8s-1 ~]# kubectl delete pod daemonset-example-hzmft
[root@k8s-1 ~]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
daemonset-example-4ttgm 1/1 Running 0 80s 10.244.2.24 k8s-3 <none> <none>
daemonset-example-fdm52 1/1 Running 0 31s 10.244.1.22 k8s-2 <none> <none>
官方文档:概念 | 工作负载 | 工作负载资源 | Jobs
Job 会创建一个或者多个 Pods,并将继续重试 Pods 的执行,直到指定数量的 Pods 成功终止。 随着 Pods 成功结束,Job 跟踪记录成功完成的 Pods 个数。 当数量达到指定的成功个数阈值时,任务(即 Job)结束。 删除 Job 的操作会清除所创建的全部 Pods。 挂起 Job 的操作会删除 Job 的所有活跃 Pod,直到 Job 被再次恢复执行。
一种简单的使用场景下,你会创建一个 Job 对象以便以一种可靠的方式运行某 Pod 直到完成。 当第一个 Pod 失败或者被删除(比如因为节点硬件失效或者重启)时,Job 对象会启动一个新的 Pod。
你也可以使用 Job 以并行的方式运行多个 Pod。
[root@k8s-1 ~]# 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@k8s-1 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
pi--1-g58wh 0/1 Completed 0 8m55s
[root@k8s-1 ~]# kubectl logs pi--1-g58wh
[root@k8s-1 ~]# kubectl get pod pi--1-g58wh -o yaml
ownerReferences:
- apiVersion: batch/v1
blockOwnerDeletion: true
controller: true
kind: Job
name: pi
uid: 851f4445-e554-44f5-979e-adfeae39e403
并行性请求(.spec.parallelism)可以设置为任何非负整数。 如果未设置,则默认为 1。 如果设置为 0,则 Job 相当于启动之后便被暂停,直到此值被增加。
实际并行性(在任意时刻运行状态的 Pods 个数)可能比并行性请求略大或略小, 原因如下:
[root@k8s-1 ~]# vim job.yaml
apiVersion: batch/v1
kind: Job
metadata:
name: pi
spec:
completions: 6
parallelism: 2
template:
spec:
containers:
- name: pi
image: perl
command: ["perl", "-Mbignum=bpi", "-wle", "print bpi(2000)"]
restartPolicy: Never
backoffLimit: 4
[root@k8s-1 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
pi--1-4zc4w 0/1 ContainerCreating 0 10s
pi--1-nsjqf 0/1 ContainerCreating 0 10s
[root@k8s-1 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
pi--1-4zc4w 0/1 Completed 0 7m24s
pi--1-57cqr 0/1 Completed 0 6m57s
pi--1-9b5fw 0/1 Completed 0 6m32s
pi--1-nsjqf 0/1 Completed 0 7m24s
pi--1-ntn6l 0/1 Completed 0 7m8s
pi--1-z2d75 0/1 Completed 0 6m46s
[root@k8s-1 ~]# kubectl describe jobs.batch
Name: pi
Namespace: default
Selector: controller-uid=dfb106e0-2f8d-4799-8805-95666c97bfa8
Labels: controller-uid=dfb106e0-2f8d-4799-8805-95666c97bfa8
job-name=pi
Annotations: <none>
Parallelism: 2
Completions: 6
Completion Mode: NonIndexed
Start Time: Mon, 09 May 2022 15:23:29 +0800
Completed At: Mon, 09 May 2022 15:27:49 +0800
Duration: 4m20s
Pods Statuses: 0 Running / 6 Succeeded / 0 Failed
Pod Template:
Labels: controller-uid=dfb106e0-2f8d-4799-8805-95666c97bfa8
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:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal SuccessfulCreate 7m54s job-controller Created pod: pi--1-4zc4w
Normal SuccessfulCreate 7m54s job-controller Created pod: pi--1-nsjqf
Normal SuccessfulCreate 7m38s job-controller Created pod: pi--1-ntn6l
Normal SuccessfulCreate 7m27s job-controller Created pod: pi--1-57cqr
Normal SuccessfulCreate 7m16s job-controller Created pod: pi--1-z2d75
Normal SuccessfulCreate 7m2s job-controller Created pod: pi--1-9b5fw
Normal Completed 3m34s job-controller Job completed
官方文档:概念 | 工作负载 | 工作负载资源 | CronJob
CronJob 创建基于时隔重复调度的 Jobs。
一个 CronJob 对象就像 crontab (cron table) 文件中的一行。 它用 Cron 格式进行编写, 并周期性地在给定的调度时间执行 Job
CronJob 用于执行周期性的动作,例如备份、报告生成等。 这些任务中的每一个都应该配置为周期性重复的(例如:每天/每周/每月一次); 你可以定义任务开始执行的时间间隔。
0 0 13 * 5
[root@k8s-1 ~]# vim cronjob.yaml
apiVersion: batch/v1
kind: CronJob
metadata:
name: cronjob-example
spec:
schedule: "* * * * *"
jobTemplate:
spec:
template:
spec:
containers:
- name: cronjob
image: busybox
args:
- /bin/sh
- -c
- date; echo Hello from k8s cluster
restartPolicy: OnFailure
[root@k8s-1 ~]# kubectl get cronjobs.batch
NAME SCHEDULE SUSPEND ACTIVE LAST SCHEDULE AGE
cronjob-example * * * * * False 0 <none> 18s
[root@k8s-1 ~]# kubectl get jobs.batch
NAME COMPLETIONS DURATION AGE
cronjob-example-27534699 1/1 26s 41s
[root@k8s-1 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
cronjob-example-27534699--1-2h84n 0/1 Completed 0 42s
[root@k8s-1 ~]# kubectl edit cronjobs.batch
Edit cancelled, no changes made.
[root@k8s-1 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
cronjob-example-27534701--1-cgfm9 0/1 Completed 0 2m24s
cronjob-example-27534702--1-f7mtx 0/1 Completed 0 84s
cronjob-example-27534703--1-98jr4 0/1 Completed 0 24s
[root@k8s-1 ~]# kubectl get jobs.batch
NAME COMPLETIONS DURATION AGE
cronjob-example-27534701 1/1 9s 2m45s
cronjob-example-27534702 1/1 26s 105s
cronjob-example-27534703 1/1 7s 45s