1 Pod控制器介绍
什么是Pod控制器
Pod控制器是管理pod的中间层,使用Pod控制器之后,只需要告诉Pod控制器,想要多少个什么样的Pod就可以了,它会创建出满足条件的Pod并确保每一个Pod资源处于用户期望的目标状态。如果Pod资源在运行中出现故障,它会基于指定策略重新编排Pod。
Pod是kubernetes的最小管理单元,在kubernetes中,按照pod的创建方式可以将其分为两类:
- 自主式pod:kubernetes直接创建出来的Pod,这种pod删除后就没有了,也不会重建
- 控制器创建的pod:kubernetes通过控制器创建的pod,这种pod删除了之后还会自动重建
在kubernetes中,有很多类型的pod控制器,每种都有自己的适合的场景,常见的有下面这些:
- ReplicationController:比较原始的pod控制器,已经被废弃,由ReplicaSet替代
- ReplicaSet:保证副本数量一直维持在期望值,并支持pod数量扩缩容,镜像版本升级
- Deployment:通过控制ReplicaSet来控制Pod,并支持滚动升级、回退版本
- Horizontal Pod Autoscaler:可以根据集群负载自动水平调整Pod的数量,实现削峰填谷
- DaemonSet:在集群中的指定Node上运行且仅运行一个副本,一般用于守护进程类的任务
- Job:它创建出来的pod只要完成任务就立即退出,不需要重启或重建,用于执行一次性任务
- Cronjob:它创建的Pod负责周期性任务控制,不需要持续后台运行
- StatefulSet:管理有状态应用 【后面有单独一遍介绍,并实战部署高可用redis】
1.2 ReplicaSet(RS)
ReplicaSet的主要作用是保证一定数量的pod正常运行,它会持续监听这些Pod的运行状态,一旦Pod发生故障,就会重启或重建。同时它还支持对pod数量的扩缩容和镜像版本的升降级。
支持的配置项:
apiVersion: apps/v1 # 版本号
kind: ReplicaSet # 类型
metadata: # 元数据
name: # rs名称
namespace: # 所属命名空间
labels: #标签
controller: rs
spec: # 详情描述
replicas: 3 # 副本数量
selector: # 选择器,通过它指定该控制器管理哪些pod
matchLabels: # Labels匹配规则
app: nginx-pod
matchExpressions: # Expressions匹配规则
- {key: app, operator: In, values: [nginx-pod]}
template: # 模板,当副本数量不足时,会根据下面的模板创建pod副本
metadata:
labels:
app: nginx-pod
spec:
containers:
- name: nginx
image: nginx:1.17.1
ports:
- containerPort: 80
在这里面,需要新了解的配置项就是spec下面几个选项:
-
replicas:指定副本数量,其实就是当前rs创建出来的pod的数量,默认为1
-
selector:选择器,它的作用是建立pod控制器和pod之间的关联关系,采用的Label Selector机制在pod模板上定义label,在控制器上定义选择器,就可以表明当前控制器能管理哪些pod了
-
template:模板,就是当前控制器创建pod所使用的模板板,里面其实就是前一章学过的pod的定义
创建ReplicaSet
创建pc-replicaset.yaml文件,内容如下:
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: pc-replicaset
namespace: dev
spec:
replicas: 3
selector:
matchLabels:
app: nginx-pod
template:
metadata:
labels:
app: nginx-pod
spec:
containers:
- name: nginx
image: nginx:1.17.1
#创建replicaset
[root@master ~]# kubectl create -f pc-replicaset.yaml
replicaset.apps/pc-replicaset created
# 去掉上一章中设置的污点
[root@master ~]# kubectl taint nodes node1 tag:NoExecute-
node/node1 untainted
[root@master ~]# kubectl taint nodes node2 tag:NoExecute-
node/node2 untainted
#查看namespace:dev下资源情况情况
[root@master ~]# kubectl -n dev get all
NAME READY STATUS RESTARTS AGE
pod/pc-replicaset-hvw4q 1/1 Running 0 5m54s
pod/pc-replicaset-rh8rv 1/1 Running 0 5m54s
pod/pc-replicaset-xctmp 1/1 Running 0 5m54s
pod/pod-toleration 1/1 Running 0 18m
pod/taint1-766c47bf55-bjzbl 1/1 Running 0 42m
pod/taint2-84946958cf-wn748 1/1 Running 0 42m
pod/taint3-57d45f9d4c-t8xjn 1/1 Running 0 42m
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/taint1 1/1 1 1 49m
deployment.apps/taint2 1/1 1 1 44m
deployment.apps/taint3 1/1 1 1 42m
NAME DESIRED CURRENT READY AGE
replicaset.apps/pc-replicaset 3 3 3 5m54s
replicaset.apps/taint1-766c47bf55 1 1 1 49m
replicaset.apps/taint2-84946958cf 1 1 1 44m
replicaset.apps/taint3-57d45f9d4c 1 1 1 42m
#删除上一章遗留的资源
[root@master ~]# kubectl -n dev delete deployment taint1
deployment.apps "taint1" deleted
[root@master ~]# kubectl -n dev delete deployment taint2
deployment.apps "taint2" deleted
[root@master ~]# kubectl -n dev delete deployment taint3
deployment.apps "taint3" deleted
[root@master ~]# kubectl -n dev get all
NAME READY STATUS RESTARTS AGE
pod/pc-replicaset-hvw4q 1/1 Running 0 6m45s
pod/pc-replicaset-rh8rv 1/1 Running 0 6m45s
pod/pc-replicaset-xctmp 1/1 Running 0 6m45s
pod/pod-toleration 1/1 Running 0 19m
pod/taint3-57d45f9d4c-t8xjn 0/1 Terminating 0 43m
NAME DESIRED CURRENT READY AGE
replicaset.apps/pc-replicaset 3 3 3 6m45s
[root@master ~]# kubectl -n dev delete pod pod-toleration
pod "pod-toleration" deleted
# 查看当前控制器创建出来的pod
# 这里发现控制器创建出来的pod的名称是在控制器名称后面拼接了-xxxxx随机码
[root@master ~]# kubectl -n dev get all
NAME READY STATUS RESTARTS AGE
pod/pc-replicaset-hvw4q 1/1 Running 0 7m21s
pod/pc-replicaset-rh8rv 1/1 Running 0 7m21s
pod/pc-replicaset-xctmp 1/1 Running 0 7m21s
# 查看rs
# DESIRED:期望副本数量
# CURRENT:当前副本数量
# READY:已经准备好提供服务的副本数量
NAME DESIRED CURRENT READY AGE
replicaset.apps/pc-replicaset 3 3 3 7m21s
扩缩容
# 编辑rs的副本数量,修改spec:replicas: 5即可
[root@master ~]# kubectl edit rs pc-replicaset -n dev
replicaset.apps/pc-replicaset edited
[root@master ~]# kubectl -n dev get all
NAME READY STATUS RESTARTS AGE
pod/pc-replicaset-5qzcq 1/1 Running 0 7s
pod/pc-replicaset-9bwtm 1/1 Running 0 7s
pod/pc-replicaset-hvw4q 1/1 Running 0 14m
pod/pc-replicaset-rh8rv 1/1 Running 0 14m
pod/pc-replicaset-xctmp 1/1 Running 0 14m
NAME DESIRED CURRENT READY AGE
replicaset.apps/pc-replicaset 5 5 5 14m
# 当然也可以直接使用命令实现
# 使用scale命令实现扩缩容, 后面--replicas=n直接指定目标数量即可
[root@master ~]# kubectl -n dev scale rs pc-replicaset --replicas=2
replicaset.apps/pc-replicaset scaled
# 这样就只剩下两个pod了
[root@master ~]# kubectl -n dev get all
NAME READY STATUS RESTARTS AGE
pod/pc-replicaset-hvw4q 1/1 Running 0 14m
pod/pc-replicaset-rh8rv 1/1 Running 0 14m
NAME DESIRED CURRENT READY AGE
replicaset.apps/pc-replicaset 2 2 2 14m
镜像升级
# 编辑rs的容器镜像 - image: nginx:1.17.2
[root@master ~]# kubectl edit rs pc-replicaset -n dev
replicaset.apps/pc-replicaset edited
[root@master ~]# kubectl -n dev get rs
NAME DESIRED CURRENT READY AGE
pc-replicaset 2 2 2 20m
# 再次查看,发现镜像版本已经变更了
[root@master ~]# kubectl -n dev get rs -o wide
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
pc-replicaset 2 2 2 21m nginx nginx:1.17.2 app=nginx-pod
# 同样的道理,也可以使用命令完成这个工作
# kubectl set image rs rs名称 容器=镜像版本 -n namespace
[root@master ~]# kubectl set image rs pc-replicaset nginx=nginx:1.17.1 -n dev
replicaset.apps/pc-replicaset image updated
# 再次查看,发现镜像版本已经变更了
[root@master ~]# kubectl -n dev get rs -o wide
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
pc-replicaset 2 2 2 23m nginx nginx:1.17.1 app=nginx-pod
删除ReplicaSet
# 使用kubectl delete命令会删除此RS以及它管理的Pod
# 在kubernetes删除RS前,会将RS的replicasclear调整为0,等待所有的Pod被删除后,在执行RS对象的删除
[root@master ~]# kubectl delete rs pc-replicaset -n dev
replicaset.apps "pc-replicaset" deleted
[root@master ~]# kubectl get pod -n dev
No resources found in dev namespace.
# 再创建一遍
[root@master ~]# kubectl -n dev get all
NAME READY STATUS RESTARTS AGE
pod/pc-replicaset-8wszx 1/1 Running 0 32s
pod/pc-replicaset-hkmnl 1/1 Running 0 32s
pod/pc-replicaset-t7mn2 1/1 Running 0 32s
NAME DESIRED CURRENT READY AGE
replicaset.apps/pc-replicaset 3 3 3 32s
# 如果希望仅仅删除RS对象(保留Pod),可以使用kubectl delete命令时添加--cascade=false选项(不推荐)。
[root@master ~]# kubectl delete rs pc-replicaset -n dev --cascade=false
replicaset.apps "pc-replicaset" deleted
[root@master ~]# kubectl -n dev get all
NAME READY STATUS RESTARTS AGE
pod/pc-replicaset-8wszx 1/1 Running 0 89s
pod/pc-replicaset-hkmnl 1/1 Running 0 89s
pod/pc-replicaset-t7mn2 1/1 Running 0 89s
[root@master ~]# kubectl create -f pc-replicaset.yaml
replicaset.apps/pc-replicaset created
# 也可以使用yaml直接删除(推荐)
[root@master ~]# kubectl delete -f pc-replicaset.yaml
replicaset.apps "pc-replicaset" deleted
1.3 Deployment(Deploy)
为了更好的解决服务编排的问题,kubernetes在V1.2版本开始,引入了Deployment控制器。值得一提的是,这种控制器并不直接管理pod,而是通过管理ReplicaSet来简介管理Pod,即:Deployment管理ReplicaSet,ReplicaSet管理Pod。所以Deployment比ReplicaSet功能更加强大。
Deployment主要功能有下面几个:
- 支持ReplicaSet的所有功能
- 支持发布的停止、继续
- 支持滚动升级和回滚版本
Deployment的资源清单文件:
apiVersion: apps/v1 # 版本号
kind: Deployment # 类型
metadata: # 元数据
name: # rs名称
namespace: # 所属命名空间
labels: #标签
controller: deploy
spec: # 详情描述
replicas: 3 # 副本数量
revisionHistoryLimit: 3 # 保留历史版本
paused: false # 暂停部署,默认是false
progressDeadlineSeconds: 600 # 部署超时时间(s),默认是600
strategy: # 策略
type: RollingUpdate # 滚动更新策略
rollingUpdate: # 滚动更新
maxSurge: 30% # 最大额外可以存在的副本数,可以为百分比,也可以为整数
maxUnavailable: 30% # 最大不可用状态的 Pod 的最大值,可以为百分比,也可以为整数
selector: # 选择器,通过它指定该控制器管理哪些pod
matchLabels: # Labels匹配规则
app: nginx-pod
matchExpressions: # Expressions匹配规则
- {key: app, operator: In, values: [nginx-pod]}
template: # 模板,当副本数量不足时,会根据下面的模板创建pod副本
metadata:
labels:
app: nginx-pod
spec:
containers:
- name: nginx
image: nginx:1.17.1
ports:
- containerPort: 80
1.3.1 创建deployment
创建pc-deployment.yaml,内容如下:
apiVersion: apps/v1
kind: Deployment
metadata:
name: pc-deployment
namespace: dev
spec:
replicas: 3
selector:
matchLabels:
app: nginx-pod
template:
metadata:
labels:
app: nginx-pod
spec:
containers:
- name: nginx
image: nginx:1.17.1
[root@master ~]# kubectl create -f pc-deployment.yaml
deployment.apps/pc-deployment created
# 查看deployment
# UP-TO-DATE 最新版本的pod的数量
# AVAILABLE 当前可用的pod的数量
[root@master ~]# kubectl get all -n dev
NAME READY STATUS RESTARTS AGE
pod/pc-deployment-5d89bdfbf9-8bfz2 1/1 Running 0 12s
pod/pc-deployment-5d89bdfbf9-94jjx 1/1 Running 0 12s
pod/pc-deployment-5d89bdfbf9-tqpd9 1/1 Running 0 12s
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/pc-deployment 3/3 3 3 12s
NAME DESIRED CURRENT READY AGE
replicaset.apps/pc-deployment-5d89bdfbf9 3 3 3 12s
[root@master ~]# kubectl get deploy pc-deployment -n dev
NAME READY UP-TO-DATE AVAILABLE AGE
pc-deployment 3/3 3 3 100s
1.3.2 deployment扩缩容
# 命令变更副本数为5
[root@master ~]# kubectl scale deploy pc-deployment --replicas=5 -n dev
deployment.apps/pc-deployment scaled
#查看副本数
[root@master ~]# kubectl get all -n dev
NAME READY STATUS RESTARTS AGE
pod/pc-deployment-5d89bdfbf9-8bfz2 1/1 Running 0 4m31s
pod/pc-deployment-5d89bdfbf9-94jjx 1/1 Running 0 4m31s
pod/pc-deployment-5d89bdfbf9-cf92c 1/1 Running 0 8s
pod/pc-deployment-5d89bdfbf9-tqpd9 1/1 Running 0 4m31s
pod/pc-deployment-5d89bdfbf9-z764k 1/1 Running 0 8s
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/pc-deployment 5/5 5 5 4m31s
NAME DESIRED CURRENT READY AGE
replicaset.apps/pc-deployment-5d89bdfbf9 5 5 5 4m31s
# 编辑deployment的副本数量,修改spec:replicas: 2即可
[root@master ~]# kubectl edit deploy pc-deployment -n dev
deployment.apps/pc-deployment edited
#查看副本数, 两个处于running 其它副本处理销毁状态
[root@master ~]# kubectl get all -n dev
NAME READY STATUS RESTARTS AGE
pod/pc-deployment-5d89bdfbf9-8bfz2 1/1 Running 0 5m14s
pod/pc-deployment-5d89bdfbf9-94jjx 0/1 Terminating 0 5m14s
pod/pc-deployment-5d89bdfbf9-tqpd9 0/1 Terminating 0 5m14s
pod/pc-deployment-5d89bdfbf9-z764k 1/1 Running 0 51s
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/pc-deployment 2/2 2 2 5m14s
NAME DESIRED CURRENT READY AGE
replicaset.apps/pc-deployment-5d89bdfbf9 2 2 2 5m14s
#过会再看 只剩下两个副本了
[root@master ~]# kubectl get all -n dev
NAME READY STATUS RESTARTS AGE
pod/pc-deployment-5d89bdfbf9-8bfz2 1/1 Running 0 7m44s
pod/pc-deployment-5d89bdfbf9-z764k 1/1 Running 0 3m21s
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/pc-deployment 2/2 2 2 7m44s
NAME DESIRED CURRENT READY AGE
replicaset.apps/pc-deployment-5d89bdfbf9 2 2 2 7m44s
镜像更新
deployment支持两种更新策略:重建更新
和滚动更新
,可以通过strategy指定策略类型,支持两个属性:
- strategy:指定新的Pod替换旧的Pod的策略, 支持两个属性:
- type:指定策略类型,支持两种策略
- Recreate:在创建出新的Pod之前会先杀掉所有已存在的Pod
- RollingUpdate:滚动更新,就是杀死一部分,就启动一部分,在更新过程中,
存在两个版本
Pod 。当type为RollingUpdate时生效,用于为RollingUpdate设置参数,支持两个属性:
- maxUnavailable:用来指定在升级过程中不可用Pod的最大数量,默认为25%。
- maxSurge: 用来指定在升级过程中可以超过期望的Pod的最大数量,默认为25%。
重建更新
- 编辑pc-deployment.yaml,在spec节点下添加更新策略
spec:
strategy: # 策略
type: Recreate # 重建更新
- 创建deploy进行验证
# 变更镜像
[root@master ~]# kubectl set image deployment pc-deployment nginx=nginx:1.17.2 -n dev
deployment.apps/pc-deployment image updated
#观察更新过程
[root@master ~]# kubectl get pod -n dev -w
NAME READY STATUS RESTARTS AGE
#首先原有存活的两个pod 版本号为1.17.1
pc-deployment-5d89bdfbf9-8bfz2 1/1 Running 0 17m
pc-deployment-5d89bdfbf9-z764k 1/1 Running 0 12m
#创建一个新的pod scr6h
pc-deployment-675d469f8b-scr6h 0/1 ContainerCreating 0 8s
pc-deployment-675d469f8b-scr6h 1/1 Running 0 26s
#新pod scr6h 创建完成 开始销毁 第一个pod z764k
pc-deployment-5d89bdfbf9-z764k 1/1 Terminating 0 13m
# 创建第二个pod z8m6c 并开始销毁第二个pod z764k
pc-deployment-675d469f8b-z8m6c 0/1 Pending 0 0s
pc-deployment-675d469f8b-z8m6c 0/1 Pending 0 0s
pc-deployment-675d469f8b-z8m6c 0/1 ContainerCreating 0 0s
pc-deployment-5d89bdfbf9-z764k 0/1 Terminating 0 13m
pc-deployment-5d89bdfbf9-z764k 0/1 Terminating 0 13m
pc-deployment-5d89bdfbf9-z764k 0/1 Terminating 0 13m
#第二个pod新建完成
pc-deployment-675d469f8b-z8m6c 1/1 Running 0 27s
pc-deployment-5d89bdfbf9-8bfz2 1/1 Terminating 0 17m
pc-deployment-5d89bdfbf9-8bfz2 0/1 Terminating 0 17m
pc-deployment-5d89bdfbf9-8bfz2 0/1 Terminating 0 18m
pc-deployment-5d89bdfbf9-8bfz2 0/1 Terminating 0 18m
#再查看一次 pod创建情况
[root@master ~]# kubectget pod -n dev -w -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
pc-deployment-675d469f8b-scr6h 1/1 Running 0 8m58s 10.244.1.55 node1 <none> <none>
pc-deployment-675d469f8b-z8m6c 1/1 Running 0 8m32s 10.244.2.25 node2 <none> <none>
滚动更新
- 编辑pc-deployment.yaml,在spec节点下添加更新策略
spec:
strategy: # 策略
type: RollingUpdate # 滚动更新策略
rollingUpdate:
maxSurge: 25%
maxUnavailable: 25%
^C[root@master ~]# kubectl set image deployment pc-deployment nginx=nginx:1.17.3 -n dev
deployment.apps/pc-deployment image updated
[root@master ~]# kubectl -n dev get pod -w
NAME READY STATUS RESTARTS AGE
pc-deployment-675d469f8b-scr6h 1/1 Running 0 14m
pc-deployment-675d469f8b-z8m6c 1/1 Running 0 14m
pc-deployment-7865c58bdf-8ghwm 0/1 ContainerCreating 0 3s
pc-deployment-7865c58bdf-8ghwm 1/1 Running 0 26s
pc-deployment-675d469f8b-z8m6c 1/1 Terminating 0 14m
pc-deployment-7865c58bdf-zpq8j 0/1 Pending 0 0s
pc-deployment-7865c58bdf-zpq8j 0/1 Pending 0 0s
pc-deployment-7865c58bdf-zpq8j 0/1 ContainerCreating 0 0s
pc-deployment-675d469f8b-z8m6c 0/1 Terminating 0 14m
pc-deployment-675d469f8b-z8m6c 0/1 Terminating 0 14m
pc-deployment-675d469f8b-z8m6c 0/1 Terminating 0 14m
pc-deployment-7865c58bdf-zpq8j 1/1 Running 0 52s
pc-deployment-675d469f8b-scr6h 1/1 Terminating 0 15m
pc-deployment-675d469f8b-scr6h 0/1 Terminating 0 15m
pc-deployment-675d469f8b-scr6h 0/1 Terminating 0 15m
pc-deployment-675d469f8b-scr6h 0/1 Terminating 0 15m
^C[root@master ~]# kubectl -n dev get pod
NAME READY STATUS RESTARTS AGE
pc-deployment-7865c58bdf-8ghwm 1/1 Running 0 2m6s
pc-deployment-7865c58bdf-zpq8j 1/1 Running 0 100s
# 中间过程是滚动进行的,也就是边销毁边创建
镜像更新中rs的变化:
# 查看rs,发现原来的rs的依旧存在,只是pod数量变为了0,而后又新产生了一个rs,pod数量为4
# 其实这就是deployment能够进行版本回退的奥妙所在,后面会详细解释
[root@master ~]# kubectl get rs -n dev
NAME DESIRED CURRENT READY AGE
pc-deployment-5d89bdfbf9 0 0 0 36m
pc-deployment-675d469f8b 0 0 0 19m
pc-deployment-7865c58bdf 2 2 2 4m47s
1.3.3 版本回退
deployment支持版本升级过程中的暂停、继续功能以及版本回退等诸多功能,下面具体来看.
kubectl rollout: 版本升级相关功能,支持下面的选项:
- status: 显示当前升级状态
- history: 显示 升级历史记录
- pause :暂停版本升级过程
- resume: 继续已经暂停的版本升级过程
- restart: 重启版本升级过程
- undo: 回滚到上一级版本(可以使用–to-revision回滚到指定版本)
# 查看当前升级版本的状态
[root@master ~]# kubectl rollout status deploy pc-deployment -n dev
deployment "pc-deployment" successfully rolled out
# 查看升级历史记录
# 可以发现有三次版本记录,说明完成过两次升级
[root@master ~]# kubectl rollout history deploy pc-deployment -n dev
deployment.apps/pc-deployment
REVISION CHANGE-CAUSE
1 <none>
2 <none>
3 <none>
# 版本回滚
# 这里直接使用--to-revision=1回滚到了1版本, 如果省略这个选项,就是回退到上个版本,就是2版本
[root@master ~]# kubectl rollout undo deployment pc-deployment --to-revision=1 -n dev
deployment.apps/pc-deployment rolled back
# 查看发现,通过nginx镜像版本可以发现到了第一版
[root@master ~]# kubectl get deploy -n dev -o wide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
pc-deployment 2/2 2 2 49m nginx nginx:1.17.1 app=nginx-pod
# 查看rs,发现第一个rs中有2个pod运行,后面两个版本的rs中pod为运行
# 其实deployment之所以可是实现版本的回滚,就是通过记录下历史rs来实现的,
# 一旦想回滚到哪个版本,只需要将当前版本pod数量降为0,然后将回滚版本的pod提升为目标数量就可以了
[root@master ~]# kubectl get rs -n dev
NAME DESIRED CURRENT READY AGE
pc-deployment-5d89bdfbf9 2 2 2 50m
pc-deployment-675d469f8b 0 0 0 33m
pc-deployment-7865c58bdf 0 0 0 19m
1.3.4 金丝雀发布
Deployment控制器支持控制更新过程中的控制,如“暂停(pause)”或“继续(resume)”更新操作。
比如有一批新的Pod资源创建完成后立即暂停更新过程,此时,仅存在一部分新版本的应用,主体部分还是旧的版本。然后,再筛选一小部分的用户请求路由到新版本的Pod应用,继续观察能否稳定地按期望的方式运行。确定没问题之后再继续完成余下的Pod资源滚动更新,否则立即回滚更新操作。这就是所谓的金丝雀发布
。
#为了效果更加明显 增加副本数为4
[root@master ~]# kubectl scale deploy pc-deployment --replicas=4 -n dev
deployment.apps/pc-deployment scaled
#查看扩容结果
[root@master ~]# kubectl -n dev get rs
NAME DESIRED CURRENT READY AGE
pc-deployment-5d89bdfbf9 4 4 4 59m
pc-deployment-675d469f8b 0 0 0 42m
pc-deployment-7865c58bdf 0 0 0 27m
[root@master ~]# kubectl -n dev get pod
NAME READY STATUS RESTARTS AGE
pc-deployment-5d89bdfbf9-4qzl8 1/1 Running 0 9m41s
pc-deployment-5d89bdfbf9-7nm4r 1/1 Running 0 15s
pc-deployment-5d89bdfbf9-fmqtk 1/1 Running 0 9m43s
pc-deployment-5d89bdfbf9-hlvld 1/1 Running 0 15s
#更新deployment的版本号为:nginx:1.17.4 并配置暂停deployment
[root@master ~]# kubectl -n dev set image deploy pc-deployment nginx=nginx:1.17.4 && kubectl rollout pause deployment -n dev
deployment.apps/pc-deployment image updated
deployment.apps/pc-deployment paused
#观察更新状态
[root@master ~]# kubectl rollout status deploy pc-deployment -n dev
Waiting for deployment "pc-deployment" rollout to finish: 2 out of 4 new replicas have been updated...
# 监控更新的过程,可以看到已经新增了一个资源,但是并未按照预期的状态去删除一个旧的资源,就是因为使用了pause暂停命令
[root@master ~]# kubectl get rs -n dev -o wide
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
pc-deployment-5d89bdfbf9 3 3 3 63m nginx nginx:1.17.1 app=nginx-pod,pod-template-hash=5d89bdfbf9
pc-deployment-675d469f8b 0 0 0 46m nginx nginx:1.17.2 app=nginx-pod,pod-template-hash=675d469f8b
pc-deployment-6c9f56fcfb 2 2 0 58s nginx nginx:1.17.4 app=nginx-pod,pod-template-hash=6c9f56fcfb
pc-deployment-7865c58bdf 0 0 0 31m nginx nginx:1.17.3 app=nginx-pod,pod-template-hash=7865c58bdf
[root@master ~]# kubectl get pod -n dev -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
pc-deployment-5d89bdfbf9-4qzl8 1/1 Running 0 14m 10.244.1.57 node1 <none> <none>
pc-deployment-5d89bdfbf9-fmqtk 1/1 Running 0 14m 10.244.2.27 node2 <none> <none>
pc-deployment-5d89bdfbf9-hlvld 1/1 Running 0 4m49s 10.244.1.58 node1 <none> <none>
pc-deployment-6c9f56fcfb-cjfk7 1/1 Running 0 100s 10.244.2.29 node2 <none> <none>
pc-deployment-6c9f56fcfb-n68ks 1/1 Running 0 100s 10.244.1.59 node1 <none> <none>
# 确保更新的pod没问题了,继续更新
[root@master ~]# kubectl rollout resume deploy pc-deployment -n dev
deployment.apps/pc-deployment resumed
# 查看最后的更新情况
[root@master ~]# kubectl get rs -n dev -o wide
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
pc-deployment-5d89bdfbf9 0 0 0 64m nginx nginx:1.17.1 app=nginx-pod,pod-template-hash=5d89bdfbf9
pc-deployment-675d469f8b 0 0 0 47m nginx nginx:1.17.2 app=nginx-pod,pod-template-hash=675d469f8b
pc-deployment-6c9f56fcfb 4 4 4 2m29s nginx nginx:1.17.4 app=nginx-pod,pod-template-hash=6c9f56fcfb
pc-deployment-7865c58bdf 0 0 0 33m nginx nginx:1.17.3 app=nginx-pod,pod-template-hash=7865c58bdf
[root@master ~]# kubectl get pods -n dev
NAME READY STATUS RESTARTS AGE
pc-deployment-6c9f56fcfb-4b9d5 1/1 Running 0 21s
pc-deployment-6c9f56fcfb-cjfk7 1/1 Running 0 2m41s
pc-deployment-6c9f56fcfb-flqr7 1/1 Running 0 21s
pc-deployment-6c9f56fcfb-n68ks 1/1 Running 0 2m41s
删除deployment
# 删除deployment,其下的rs和pod也将被删除
[root@master ~]# kubectl delete deploy pc-deployment -n dev
deployment.apps "pc-deployment" deleted
[root@master ~]# kubectl -n dev get all
No resources found in dev namespace.
1.4 Horizontal Pod Autoscaler(HPA)
我们已经可以实现通过手工执行kubectl scale命令实现Pod扩容或缩容,但是这显然不符合Kubernetes的定位目标–自动化、智能化。 Kubernetes期望可以实现通过监测Pod的使用情况,实现pod数量的自动调整,于是就产生了Horizontal Pod Autoscaler(HPA)
这种控制器。
HPA可以获取每个Pod利用率,然后和HPA中定义的指标进行对比,同时计算出需要伸缩的具体值,最后实现Pod的数量的调整。其实HPA与之前的Deployment一样,也属于一种Kubernetes资源对象,它通过追踪分析RC控制的所有目标Pod的负载变化情况,来确定是否需要针对性地调整目标Pod的副本数,这是HPA的实现原理。
接下来,我们来做一个实验
1.4.1 安装metrics-server
metrics-server可以用来收集集群中的资源使用情况
为了不再集群中安装git 我手动去github下载metrics-server
# 修改deployment, 注意修改的是镜像和初始化参数
[root@master ~]# cd metrics-server-0.3.6/deploy/1.8+/
[root@master 1.8+]# vi metrics-server-deployment.yaml
按图中添加下面选项
hostNetwork: true
image: registry.cn-hangzhou.aliyuncs.com/google_containers/metrics-server-amd64:v0.3.6
args:
- --kubelet-insecure-tls
- --kubelet-preferred-address-types=InternalIP,Hostname,InternalDNS,ExternalDNS,ExternalIP
# 安装metrics-server
[root@master 1.8+]# kubectl apply -f ./
clusterrole.rbac.authorization.k8s.io/system:aggregated-metrics-reader created
clusterrolebinding.rbac.authorization.k8s.io/metrics-server:system:auth-delegator created
rolebinding.rbac.authorization.k8s.io/metrics-server-auth-reader created
apiservice.apiregistration.k8s.io/v1beta1.metrics.k8s.io created
serviceaccount/metrics-server created
deployment.apps/metrics-server created
service/metrics-server created
clusterrole.rbac.authorization.k8s.io/system:metrics-server created
clusterrolebinding.rbac.authorization.k8s.io/system:metrics-server created
# 查看pod运行情况
[root@master 1.8+]# kubectl get pod -n kube-system
NAME READY STATUS RESTARTS AGE
metrics-server-6b976979db-hq4z6 1/1 Running 0 17s
# 使用kubectl top node 查看资源使用情况
[root@master 1.8+]# kubectl top node
NAME CPU(cores) CPU% MEMORY(bytes) MEMORY%
master 128m 6% 1679Mi 60%
node1 25m 1% 704Mi 25%
node2 26m 1% 658Mi 23%
1.4.2 准备deployment和servie
创建pc-hpa-pod.yaml文件,内容如下:
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
namespace: dev
spec:
strategy: # 策略
type: RollingUpdate # 滚动更新策略
replicas: 1
selector:
matchLabels:
app: nginx-pod
template:
metadata:
labels:
app: nginx-pod
spec:
containers:
- name: nginx
image: nginx:1.17.1
resources: # 资源配额
limits: # 限制资源(上限)
cpu: "1" # CPU限制,单位是core数
requests: # 请求资源(下限)
cpu: "100m" # CPU限制,单位是core数
#创建deployment
[root@master config]# kubectl run nginx --image=nginx:1.17.1 --requests=cpu=100m -n dev
kubectl run --generator=deployment/apps.v1 is DEPRECATED and will be removed in a future version. Use kubectl run --generator=run-pod/v1 or kubectl create instead.
deployment.apps/nginx created
#创建service
[root@master config]# kubectl expose deployment nginx --type=NodePort --port=80 -n dev
service/nginx exposed
#查看创建情况
[root@master config]# kubectl get all -n dev
NAME READY STATUS RESTARTS AGE
pod/nginx-778cb5fb7b-5v8b2 1/1 Running 0 51s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/nginx NodePort 10.103.168.127 <none> 80:31339/TCP 7s
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/nginx 1/1 1 1 51s
NAME DESIRED CURRENT READY AGE
replicaset.apps/nginx-778cb5fb7b 1 1 1 51s
1.4.3 部署HPA
创建pc-hpa.yaml文件,内容如下:
apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
name: pc-hpa
namespace: dev
spec:
minReplicas: 1 #最小pod数量
maxReplicas: 10 #最大pod数量
targetCPUUtilizationPercentage: 3 # CPU使用率指标
scaleTargetRef: # 指定要控制的nginx信息
apiVersion: /v1
kind: Deployment
name: nginx
[root@master config]# kubectl create -f pc-hpa.yaml
horizontalpodautoscaler.autoscaling/pc-hpa created
[root@master config]# kubectl get hpa -n dev
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
pc-hpa Deployment/nginx <unknown>/3% 1 10 0 10s
1.4.4 测试
使用压测工具对service地址192.168.5.4:31830进行压测,然后通过控制台查看hpa和pod的变化
hpa变化
[root@master config]# kubectl get hpa -n dev -w
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
pc-hpa Deployment/nginx 0%/3% 1 10 1 4m11s
pc-hpa Deployment/nginx 0%/3% 1 10 1 5m19s
pc-hpa Deployment/nginx 22%/3% 1 10 1 6m50s
pc-hpa Deployment/nginx 22%/3% 1 10 4 7m5s
pc-hpa Deployment/nginx 22%/3% 1 10 8 7m21s
pc-hpa Deployment/nginx 6%/3% 1 10 8 7m51s
pc-hpa Deployment/nginx 0%/3% 1 10 8 9m6s
pc-hpa Deployment/nginx 0%/3% 1 10 8 13m
pc-hpa Deployment/nginx 0%/3% 1 10 1 14m
deployment变化
[root@master config]# kubectl get deployment -n dev -w
NAME READY UP-TO-DATE AVAILABLE AGE
nginx 1/1 1 1 11m
nginx 1/4 1 1 13m
nginx 1/4 1 1 13m
nginx 1/4 1 1 13m
nginx 1/4 4 1 13m
nginx 1/8 4 1 14m
nginx 1/8 4 1 14m
nginx 1/8 4 1 14m
nginx 1/8 8 1 14m
nginx 2/8 8 2 14m
nginx 3/8 8 3 14m
nginx 4/8 8 4 14m
nginx 5/8 8 5 14m
nginx 6/8 8 6 14m
nginx 7/8 8 7 14m
nginx 8/8 8 8 15m
nginx 8/1 8 8 20m
nginx 8/1 8 8 20m
nginx 1/1 1 1 20m
pod变化
[root@master config]# kubectl get pods -n dev -w
NAME READY STATUS RESTARTS AGE
nginx-7df9756ccc-bh8dr 1/1 Running 0 11m
nginx-7df9756ccc-cpgrv 0/1 Pending 0 0s
nginx-7df9756ccc-8zhwk 0/1 Pending 0 0s
nginx-7df9756ccc-rr9bn 0/1 Pending 0 0s
nginx-7df9756ccc-cpgrv 0/1 ContainerCreating 0 0s
nginx-7df9756ccc-8zhwk 0/1 ContainerCreating 0 0s
nginx-7df9756ccc-rr9bn 0/1 ContainerCreating 0 0s
nginx-7df9756ccc-m9gsj 0/1 Pending 0 0s
nginx-7df9756ccc-g56qb 0/1 Pending 0 0s
nginx-7df9756ccc-sl9c6 0/1 Pending 0 0s
nginx-7df9756ccc-fgst7 0/1 Pending 0 0s
nginx-7df9756ccc-g56qb 0/1 ContainerCreating 0 0s
nginx-7df9756ccc-m9gsj 0/1 ContainerCreating 0 0s
nginx-7df9756ccc-sl9c6 0/1 ContainerCreating 0 0s
nginx-7df9756ccc-fgst7 0/1 ContainerCreating 0 0s
nginx-7df9756ccc-8zhwk 1/1 Running 0 19s
nginx-7df9756ccc-rr9bn 1/1 Running 0 30s
nginx-7df9756ccc-m9gsj 1/1 Running 0 21s
nginx-7df9756ccc-cpgrv 1/1 Running 0 47s
nginx-7df9756ccc-sl9c6 1/1 Running 0 33s
nginx-7df9756ccc-g56qb 1/1 Running 0 48s
nginx-7df9756ccc-fgst7 1/1 Running 0 66s
nginx-7df9756ccc-fgst7 1/1 Terminating 0 6m50s
nginx-7df9756ccc-8zhwk 1/1 Terminating 0 7m5s
nginx-7df9756ccc-cpgrv 1/1 Terminating 0 7m5s
nginx-7df9756ccc-g56qb 1/1 Terminating 0 6m50s
nginx-7df9756ccc-rr9bn 1/1 Terminating 0 7m5s
nginx-7df9756ccc-m9gsj 1/1 Terminating 0 6m50s
nginx-7df9756ccc-sl9c6 1/1 Terminating 0 6m50s
1.5 DaemonSet(DS)
DaemonSet类型的控制器可以保证在集群中的每一台(或指定)节点上都运行一个副本。一般适用于日志收集、节点监控等场景。也就是说,如果一个Pod提供的功能是节点级别的(每个节点都需要且只需要一个),那么这类Pod就适合使用DaemonSet类型的控制器创建。
DaemonSet控制器的特点:
- 每当向集群中添加一个节点时,指定的 Pod 副本也将添加到该节点上
- 当节点从集群中移除时,Pod 也就被垃圾回收了
先来看下DaemonSet的资源清单文件:
apiVersion: apps/v1 # 版本号
kind: DaemonSet # 类型
metadata: # 元数据
name: # rs名称
namespace: # 所属命名空间
labels: #标签
controller: daemonset
spec: # 详情描述
revisionHistoryLimit: 3 # 保留历史版本
updateStrategy: # 更新策略
type: RollingUpdate # 滚动更新策略
rollingUpdate: # 滚动更新
maxUnavailable: 1 # 最大不可用状态的 Pod 的最大值,可以为百分比,也可以为整数
selector: # 选择器,通过它指定该控制器管理哪些pod
matchLabels: # Labels匹配规则
app: nginx-pod
matchExpressions: # Expressions匹配规则
- {key: app, operator: In, values: [nginx-pod]}
template: # 模板,当副本数量不足时,会根据下面的模板创建pod副本
metadata:
labels:
app: nginx-pod
spec:
containers:
- name: nginx
image: nginx:1.17.1
ports:
- containerPort: 80
创建pc-daemonset.yaml,内容如下:
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: pc-daemonset
namespace: dev
spec:
selector:
matchLabels:
app: nginx-pod
template:
metadata:
labels:
app: nginx-pod
spec:
containers:
- name: nginx
image: nginx:1.17.1
[root@master config]# kubectl create -f pc-daemonset.yaml
daemonset.apps/pc-daemonset created
[root@master config]# kubectl -n dev get ds -n dev -o wide
NAME DESIRED CURRENT READY UP-TO-DATE AVAILABLE NODE SELECTOR AGE CONTAINERS IMAGES SELECTOR
pc-daemonset 2 2 2 2 2 <none> 18s nginx nginx:1.17.1 app=nginx-pod
[root@master config]# kubectl -n dev get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
pc-daemonset-cd9l7 1/1 Running 0 31s 10.244.2.33 node2 <none> <none>
pc-daemonset-spm5h 1/1 Running 0 31s 10.244.1.61 node1 <none> <none>
[root@master config]# kubectl delete -f pc-daemonset.yaml
daemonset.apps "pc-daemonset" deleted
1.6 Job
Job,主要用于负责 批量处理(一次要处理指定数量任务)短暂的一次性(每个任务仅运行一次就结束)任务。
Job特点如下:
- 当Job创建的pod执行成功结束时,Job将记录成功结束的pod数量
- 当成功结束的pod达到指定的数量时,Job将完成执行
job资源清单:
apiVersion: batch/v1 # 版本号
kind: Job # 类型
metadata: # 元数据
name: # rs名称
namespace: # 所属命名空间
labels: #标签
controller: job
spec: # 详情描述
completions: 1 # 指定job需要成功运行Pods的次数。默认值: 1
parallelism: 1 # 指定job在任一时刻应该并发运行Pods的数量。默认值: 1
activeDeadlineSeconds: 30 # 指定job可运行的时间期限,超过时间还未结束,系统将会尝试进行终止。
backoffLimit: 6 # 指定job失败后进行重试的次数。默认是6
manualSelector: true # 是否可以使用selector选择器选择pod,默认是false
selector: # 选择器,通过它指定该控制器管理哪些pod
matchLabels: # Labels匹配规则
app: counter-pod
matchExpressions: # Expressions匹配规则
- {key: app, operator: In, values: [counter-pod]}
template: # 模板,当副本数量不足时,会根据下面的模板创建pod副本
metadata:
labels:
app: counter-pod
spec:
restartPolicy: Never # 重启策略只能设置为Never或者OnFailure
containers:
- name: counter
image: busybox:1.30
command: ["bin/sh","-c","for i in 9 8 7 6 5 4 3 2 1; do echo $i;sleep 2;done"]
关于重启策略设置的说明:
如果指定为OnFailure,则job会在pod出现故障时重启容器,而不是创建pod,failed次数不变
如果指定为Never,则job会在pod出现故障时创建新的pod,并且故障pod不会消失,也不会重启,failed次数加1
如果指定为Always的话,就意味着一直重启,意味着job任务会重复去执行了,当然不对,所以不能设置为Always
创建pc-job.yaml:
apiVersion: batch/v1
kind: Job
metadata:
name: pc-job
namespace: dev
spec:
manualSelector: true
selector:
matchLabels:
app: counter-pod
template:
metadata:
labels:
app: counter-pod
spec:
restartPolicy: Never
containers:
- name: counter
image: busybox:1.30
command: ["bin/sh","-c","for i in 9 8 7 6 5 4 3 2 1; do echo $i;sleep 3;done"]
[root@master config]# kubectl -n dev get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-778cb5fb7b-5v8b2 1/1 Running 0 34h 10.244.2.32 node2 <none> <none>
# 通过观察pod状态可以看到,pod在运行完毕任务后,就会变成Completed状态
[root@master config]# kubectl get pods -n dev -w
NAME READY STATUS RESTARTS AGE
pc-job-rxg96 1/1 Running 0 29s
pc-job-rxg96 0/1 Completed 0 33s
[root@master config]# kubectl delete -f pc-job.yaml
# 接下来,调整下pod运行的总数量和并行数量 即:在spec下设置下面两个选项
# completions: 6 # 指定job需要成功运行Pods的次数为6
# parallelism: 3 # 指定job并发运行Pods的数量为3
# 然后重新运行job,观察效果,此时会发现,job会每次运行3个pod,总共执行了6个pod
[root@master config]# vi pc-job.yaml
[root@master config]# kubectl apply -f pc-job.yaml
job.batch/pc-job created
[root@master config]# kubectl get pods -n dev -w
NAME READY STATUS RESTARTS AGE
nginx-778cb5fb7b-5v8b2 1/1 Running 0 34h
pc-job-g5w9g 1/1 Running 0 8s
pc-job-rk2pg 1/1 Running 0 8s
pc-job-z9xmt 1/1 Running 0 8s
pc-job-z9xmt 0/1 Completed 0 29s
pc-job-rgn4d 0/1 Pending 0 0s
pc-job-rgn4d 0/1 Pending 0 0s
pc-job-rk2pg 0/1 Completed 0 29s
pc-job-k26sm 0/1 Pending 0 0s
pc-job-k26sm 0/1 Pending 0 0s
pc-job-rgn4d 0/1 ContainerCreating 0 0s
pc-job-k26sm 0/1 ContainerCreating 0 0s
pc-job-g5w9g 0/1 Completed 0 29s
pc-job-trkwv 0/1 Pending 0 0s
pc-job-trkwv 0/1 Pending 0 0s
pc-job-trkwv 0/1 ContainerCreating 0 0s
pc-job-trkwv 1/1 Running 0 1s
pc-job-rgn4d 1/1 Running 0 1s
pc-job-k26sm 1/1 Running 0 1s
pc-job-trkwv 0/1 Completed 0 28s
pc-job-rgn4d 0/1 Completed 0 28s
pc-job-k26sm 0/1 Completed 0 29s
[root@master config]# kubectl delete -f pc-job.yaml
job.batch "pc-job" deleted
1.7 CronJob(CJ)
CronJob控制器以 Job控制器资源为其管控对象,并借助它管理pod资源对象,Job控制器定义的作业任务在其控制器资源创建之后便会立即执行,但CronJob可以以类似于Linux操作系统的周期性任务作业计划的方式控制其运行时间点及重复运行的方式。也就是说,CronJob可以在特定的时间点(反复的)去运行job任务。
CronJob的资源清单:
apiVersion: batch/v1beta1 # 版本号
kind: CronJob # 类型
metadata: # 元数据
name: # rs名称
namespace: # 所属命名空间
labels: #标签
controller: cronjob
spec: # 详情描述
schedule: # cron格式的作业调度运行时间点,用于控制任务在什么时间执行
concurrencyPolicy: # 并发执行策略,用于定义前一次作业运行尚未完成时是否以及如何运行后一次的作业
failedJobHistoryLimit: # 为失败的任务执行保留的历史记录数,默认为1
successfulJobHistoryLimit: # 为成功的任务执行保留的历史记录数,默认为3
startingDeadlineSeconds: # 启动作业错误的超时时长
jobTemplate: # job控制器模板,用于为cronjob控制器生成job对象;下面其实就是job的定义
metadata:
spec:
completions: 1
parallelism: 1
activeDeadlineSeconds: 30
backoffLimit: 6
manualSelector: true
selector:
matchLabels:
app: counter-pod
matchExpressions: 规则
- {key: app, operator: In, values: [counter-pod]}
template:
metadata:
labels:
app: counter-pod
spec:
restartPolicy: Never
containers:
- name: counter
image: busybox:1.30
command: ["bin/sh","-c","for i in 9 8 7 6 5 4 3 2 1; do echo $i;sleep 20;done"]
需要重点解释的几个选项:
schedule: cron表达式,用于指定任务的执行时间
*/1 * * * *
<分钟> <小时> <日> <月份> <星期>
分钟 值从 0 到 59.
小时 值从 0 到 23.
日 值从 1 到 31.
月 值从 1 到 12.
星期 值从 0 到 6, 0 代表星期日
多个时间可以用逗号隔开; 范围可以用连字符给出;*可以作为通配符; /表示每…
concurrencyPolicy:
Allow: 允许Jobs并发运行(默认)
Forbid: 禁止并发运行,如果上一次运行尚未完成,则跳过下一次运行
Replace: 替换,取消当前正在运行的作业并用新作业替换它
创建pc-cronjob.yaml,内容如下:
apiVersion: batch/v1beta1
kind: CronJob
metadata:
name: pc-cronjob
namespace: dev
labels:
controller: cronjob
spec:
schedule: "*/1 * * * *"
jobTemplate:
metadata:
spec:
template:
spec:
restartPolicy: Never
containers:
- name: counter
image: busybox:1.30
command: ["bin/sh","-c","for i in 9 8 7 6 5 4 3 2 1; do echo $i;sleep 3;done"]
[root@master config]# kubectl create -f pc-cronjob.yaml
cronjob.batch/pc-cronjob created
[root@master config]# kubectl get cronjobs -n dev
NAME SCHEDULE SUSPEND ACTIVE LAST SCHEDULE AGE
pc-cronjob */1 * * * * False 0 <none> 11s
[root@master config]# kubectl get job -n dev -w
NAME COMPLETIONS DURATION AGE
pc-cronjob-1656750060 0/1 11s 11s
pc-cronjob-1656750060 1/1 29s 29s
^C[root@master config]# kubectl get job -n dev -w -o wide
NAME COMPLETIONS DURATION AGE CONTAINERS IMAGES SELECTOR
pc-cronjob-1656750060 1/1 29s 54s counter busybox:1.30 controller-uid=019816dc-d96f-4b8f-aa5d-3a6ec00d0268
pc-cronjob-1656750120 0/1 0s counter busybox:1.30 controller-uid=bd44c6ca-3787-42df-9b55-1b4b5e0b84fb
pc-cronjob-1656750120 0/1 0s 0s counter busybox:1.30 controller-uid=bd44c6ca-3787-42df-9b55-1b4b5e0b84fb
pc-cronjob-1656750120 1/1 28s 28s counter busybox:1.30 controller-uid=bd44c6ca-3787-42df-9b55-1b4b5e0b84fb
pc-cronjob-1656750180 0/1 0s counter busybox:1.30 controller-uid=4756b4a6-a7ac-4ec3-851e-87699b7f444c
pc-cronjob-1656750180 0/1 0s 0s counter busybox:1.30 controller-uid=4756b4a6-a7ac-4ec3-851e-87699b7f444c
[root@master config]# kubectl get pod -n dev -w -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-778cb5fb7b-5v8b2 1/1 Running 0 34h 10.244.2.32 node2 <none> <none>
pc-cronjob-1656750060-qqqqw 0/1 Completed 0 2m25s 10.244.1.64 node1 <none> <none>
pc-cronjob-1656750120-rvbj2 0/1 Completed 0 84s 10.244.2.39 node2 <none> <none>
pc-cronjob-1656750180-slkh9 1/1 Running 0 24s 10.244.1.65 node1 <none> <none>
pc-cronjob-1656750180-slkh9 0/1 Completed 0 29s 10.244.1.65 node1 <none> <none>
[root@master config]#
[root@master config]# kubectl delete -f pc-cronjob.yaml
cronjob.batch "pc-cronjob" deleted