4 Kubernetes资源-Pod控制器(1)
Kubernetes中Pod分为自主式和控制器管理式:
- YAML指定类型为Pod的均为自主式,此类Pod退出后不会被创建;
- YMAL指定类型为ReplicaSet、Deployment、DaemonSet、Job、CronJob、StatefulSet、HPA的均为控制器管理式,在控制器的生命周期内始终保持Pod的副本数据。
控制器相当于状态机,用来控制Pod的具体状态和行为。
根据应用场景可以分为:
- 无状态后台服务:ReplicaSet和Deployment
- 节点为单位:DaemonSet
- 批处理任务:Job和CronJob
- 有状态后台服务:StatefulSet
根据运行方式可以分为:
- 声明式:Deployment、CronJob,使用apply启动
- 命令式:Pod、ReplicaSet、DaemonSet、Job,使用create启动。
4.1 ReplicaSet
ReplicaSet 资源能够确保Pod的副本数保持在用户期望的值,当容器异常退出时会自动创建新的Pod,反之则删除。
ReplicaSet控制器通过匹配Pod的标签获取自己所管理的Pod,维持所管理的标签的Pod副本数目。ReplicaSet拥有集合式的标签选择器,它被设计成能控制多个不同标签的Pod副本,例如应用APP发布了V1和V2两个版本,用户希望APP的Pod副本数保持固定,并且允许同时包含V1和V2版本的Pod,就可以用ReplicaSet来实现控制。
- 编写YAML文件,通过app: tomcat标签将ReplicaSet与Pod资源关联:
[root@k8smaster43-11 CSDN]# cat replicaset.yaml
apiVersion: apps/v1 # API版本
kind: ReplicaSet # 资源类型
metadata:
name: tomcat-replicaset # RS资源名称
spec:
replicas: 3 # Pod副本数目
selector:
matchLabels:
app: tomcat # 关联Pod标签
template:
# 以下是配置Pod资源,可以看做ReplicaSet嵌套了关联的Pod资源
metadata:
name: tomcat-pod # Pod资源名称
labels:
app: tomcat # Pod资源标签
spec:
containers:
- name: demo-tomcat # Pod容器名称
image: tomcat:8.0 # Pod容器镜像
imagePullPolicy: Never # 镜像下载模式
ports:
- containerPort: 8080 # Pod内容器服务端口
protocol: TCP # 端口服务类型
[root@k8smaster43-11 CSDN]#
- 创建RS资源:
[root@k8smaster43-11 CSDN]# kubectl apply -f replicaset.yaml
replicaset.apps/tomcat-replicaset created
[root@k8smaster43-11 CSDN]#
- 查看ReplicaSet和对应Pod的资源:
[root@k8smaster43-11 CSDN]# kubectl get rs,pod -o wide --show-labels
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR LABELS
replicaset.apps/tomcat-replicaset 3 3 3 2m7s demo-tomcat tomcat:8.0 app=tomcat <none>
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES LABELS
pod/tomcat-replicaset-dxddd 1/1 Running 0 2m7s 10.244.2.44 k8sworker43-22 <none> <none> app=tomcat
pod/tomcat-replicaset-mf9c2 1/1 Running 0 2m7s 10.244.2.43 k8sworker43-22 <none> <none> app=tomcat
pod/tomcat-replicaset-z5qpp 1/1 Running 0 2m7s 10.244.1.54 k8sworker43-21 <none> <none> app=tomcat
[root@k8smaster43-11 CSDN]#
- 删除ReplicaSet资源并且保留Pod资源:
[root@k8smaster43-11 CSDN]# kubectl delete -f replicaset.yaml --cascade=false
replicaset.apps "tomcat-replicaset" deleted
[root@k8smaster43-11 CSDN]#
[root@k8smaster43-11 CSDN]# kubectl get rs,pod -o wide --show-labels
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES LABELS
pod/tomcat-replicaset-dxddd 1/1 Running 0 12m 10.244.2.44 k8sworker43-22 <none> <none> app=tomcat
pod/tomcat-replicaset-mf9c2 1/1 Running 0 12m 10.244.2.43 k8sworker43-22 <none> <none> app=tomcat
pod/tomcat-replicaset-z5qpp 1/1 Running 0 12m 10.244.1.54 k8sworker43-21 <none> <none> app=tomcat
[root@k8smaster43-11 CSDN]#
- 删除ReplicaSet和对应Pod的资源:
[root@k8smaster43-11 CSDN]# kubectl delete -f replicaset.yaml
replicaset.apps "tomcat-replicaset" deleted
[root@k8smaster43-11 CSDN]#
4.2 Deployment
确保Pod的副本数保持在用户期望的值,当容器异常退出时会自动创建新的Pod,反之则删除。Deployment控制器除了包含ReplicaSet的功能,还具有滚动升级、回滚应用、扩缩容、暂停和继续功能。
Deployment控制器可以设置升级策略:Recreate重建、RollingUpdate滚动更新,其中滚动更新包含两个参数maxUnavailabe指定更新过程中可用状态的Pod数量上限、maxSurge指定更新过程中Pod总数超过副本期望部分的最大值。
- 编写YAML文件,通过app: tomcat标签将Deployment、ReplicaSet与Pod资源关联:
[root@k8smaster43-11 CSDN]# cat deployment.yaml
apiVersion: apps/v1 # API版本
kind: Deployment # 资源类型
metadata:
name: tomcat-deployment # Deployment资源名称
spec:
strategy: # Pod更新策略
type: RollingUpdate # RollingUpdate滚动更新/Recreate重建
rollingUpdate: # RollingUpdate滚动更新时副本数目策略
maxUnavailable: 2
maxSurge: 0
# 以下配置与ReplicaSet.spec资源配置相同
replicas: 6 # Pod副本数目
selector:
matchLabels:
app: tomcat # 关联Pod标签
template:
metadata:
name: tomcat-pod # Pod资源名称
labels:
app: tomcat # Pod资源标签
spec:
containers:
- name: tomcat # Pod容器名称
image: tomcat:8.0 # Pod容器镜像
imagePullPolicy: Never # 容器下载模式
ports:
- containerPort: 8080 # Pod内容器服务端口
protocol: TCP # 端口服务类型
[root@k8smaster43-11 CSDN]#
- 创建Deployment资源:
[root@k8smaster43-11 CSDN]# kubectl apply -f deployment.yaml
deployment.apps/tomcat-deployment created
[root@k8smaster43-11 CSDN]#
- 查看Deployment、ReplicaSet和对应Pod的资源:
[root@k8smaster43-11 CSDN]# kubectl get deploy,rs,pod -o wide --show-labels
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR LABELS
deployment.apps/tomcat-deployment 6/6 6 6 15s tomcat tomcat:8.0 app=tomcat <none>
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR LABELS
replicaset.apps/tomcat-deployment-7fbcf7c944 6 6 6 15s tomcat tomcat:8.0 app=tomcat,pod-template-hash=7fbcf7c944 app=tomcat,pod-template-hash=7fbcf7c944
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES LABELS
pod/tomcat-deployment-7fbcf7c944-68wv5 1/1 Running 0 15s 10.244.2.46 k8sworker43-22 <none> <none> app=tomcat,pod-template-hash=7fbcf7c944
pod/tomcat-deployment-7fbcf7c944-dzf92 1/1 Running 0 15s 10.244.2.47 k8sworker43-22 <none> <none> app=tomcat,pod-template-hash=7fbcf7c944
pod/tomcat-deployment-7fbcf7c944-m6j2v 1/1 Running 0 15s 10.244.2.45 k8sworker43-22 <none> <none> app=tomcat,pod-template-hash=7fbcf7c944
pod/tomcat-deployment-7fbcf7c944-mw8hh 1/1 Running 0 15s 10.244.1.57 k8sworker43-21 <none> <none> app=tomcat,pod-template-hash=7fbcf7c944
pod/tomcat-deployment-7fbcf7c944-tqldt 1/1 Running 0 15s 10.244.1.56 k8sworker43-21 <none> <none> app=tomcat,pod-template-hash=7fbcf7c944
pod/tomcat-deployment-7fbcf7c944-vw4jj 1/1 Running 0 15s 10.244.1.55 k8sworker43-21 <none> <none> app=tomcat,pod-template-hash=7fbcf7c944
[root@k8smaster43-11 CSDN]#
- 对副本进行扩容/缩容
[root@k8smaster43-11 CSDN]# kubectl scale deployment.apps/tomcat-deployment --replicas 10
deployment.apps/tomcat-deployment scaled
[root@k8smaster43-11 CSDN]#
[root@k8smaster43-11 CSDN]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
tomcat-deployment-7fbcf7c944-4sjfj 1/1 Running 0 8s 10.244.1.59 k8sworker43-21 <none> <none>
tomcat-deployment-7fbcf7c944-68wv5 1/1 Running 0 10m 10.244.2.46 k8sworker43-22 <none> <none>
tomcat-deployment-7fbcf7c944-8s54r 1/1 Running 0 8s 10.244.2.48 k8sworker43-22 <none> <none>
tomcat-deployment-7fbcf7c944-dzf92 1/1 Running 0 10m 10.244.2.47 k8sworker43-22 <none> <none>
tomcat-deployment-7fbcf7c944-hl824 1/1 Running 0 8s 10.244.2.49 k8sworker43-22 <none> <none>
tomcat-deployment-7fbcf7c944-lzg9c 1/1 Running 0 8s 10.244.1.58 k8sworker43-21 <none> <none>
tomcat-deployment-7fbcf7c944-m6j2v 1/1 Running 0 10m 10.244.2.45 k8sworker43-22 <none> <none>
tomcat-deployment-7fbcf7c944-mw8hh 1/1 Running 0 10m 10.244.1.57 k8sworker43-21 <none> <none>
tomcat-deployment-7fbcf7c944-tqldt 1/1 Running 0 10m 10.244.1.56 k8sworker43-21 <none> <none>
tomcat-deployment-7fbcf7c944-vw4jj 1/1 Running 0 10m 10.244.1.55 k8sworker43-21 <none> <none>
[root@k8smaster43-11 CSDN]#
- 对服务版本进行滚动更新,将镜像从tomcat:8.0更新到tomcat:6.0:
[root@k8smaster43-11 CSDN]# kubectl set image deployment.apps/tomcat-deployment tomcat=tomcat:6.0
deployment.apps/tomcat-deployment image updated
[root@k8smaster43-11 CSDN]#
[root@k8smaster43-11 CSDN]# kubectl get deploy,rs,pod -o wide --show-labels
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR LABELS
deployment.apps/tomcat-deployment 10/10 10 10 61s tomcat tomcat:6.0 app=tomcat <none>
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR LABELS
replicaset.apps/tomcat-deployment-5dddc7f6b6 10 10 10 25s tomcat tomcat:6.0 app=tomcat,pod-template-hash=5dddc7f6b6 app=tomcat,pod-template-hash=5dddc7f6b6
replicaset.apps/tomcat-deployment-7fbcf7c944 0 0 0 61s tomcat tomcat:8.0 app=tomcat,pod-template-hash=7fbcf7c944 app=tomcat,pod-template-hash=7fbcf7c944
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES LABELS
pod/tomcat-deployment-5dddc7f6b6-4xm6c 1/1 Running 0 17s 10.244.2.75 k8sworker43-22 <none> <none> app=tomcat,pod-template-hash=5dddc7f6b6
pod/tomcat-deployment-5dddc7f6b6-6mb4c 1/1 Running 0 25s 10.244.1.87 k8sworker43-21 <none> <none> app=tomcat,pod-template-hash=5dddc7f6b6
pod/tomcat-deployment-5dddc7f6b6-g7nb5 1/1 Running 0 21s 10.244.1.89 k8sworker43-21 <none> <none> app=tomcat,pod-template-hash=5dddc7f6b6
pod/tomcat-deployment-5dddc7f6b6-km7bf 1/1 Running 0 23s 10.244.1.88 k8sworker43-21 <none> <none> app=tomcat,pod-template-hash=5dddc7f6b6
pod/tomcat-deployment-5dddc7f6b6-kt54w 1/1 Running 0 24s 10.244.2.73 k8sworker43-22 <none> <none> app=tomcat,pod-template-hash=5dddc7f6b6
pod/tomcat-deployment-5dddc7f6b6-kzx56 1/1 Running 0 15s 10.244.1.92 k8sworker43-21 <none> <none> app=tomcat,pod-template-hash=5dddc7f6b6
pod/tomcat-deployment-5dddc7f6b6-mgg2d 1/1 Running 0 15s 10.244.1.91 k8sworker43-21 <none> <none> app=tomcat,pod-template-hash=5dddc7f6b6
pod/tomcat-deployment-5dddc7f6b6-n2d9k 1/1 Running 0 17s 10.244.2.74 k8sworker43-22 <none> <none> app=tomcat,pod-template-hash=5dddc7f6b6
pod/tomcat-deployment-5dddc7f6b6-nplcm 1/1 Running 0 20s 10.244.1.90 k8sworker43-21 <none> <none> app=tomcat,pod-template-hash=5dddc7f6b6
pod/tomcat-deployment-5dddc7f6b6-nrl9w 1/1 Running 0 25s 10.244.2.72 k8sworker43-22 <none> <none> app=tomcat,pod-template-hash=5dddc7f6b6
[root@k8smaster43-11 CSDN]#
- 对服务版本进行回滚,将镜像从tomcat:6.0还原回tomcat:8.0:
[root@k8smaster43-11 CSDN]# kubectl rollout undo deployment.apps/tomcat-deployment
deployment.apps/tomcat-deployment rolled back
[root@k8smaster43-11 CSDN]#
[root@k8smaster43-11 CSDN]# kubectl get deploy,rs,pod -o wide --show-labels
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR LABELS
deployment.apps/tomcat-deployment 10/10 10 10 6m47s tomcat tomcat:8.0 app=tomcat <none>
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR LABELS
replicaset.apps/tomcat-deployment-5dddc7f6b6 0 0 0 6m11s tomcat tomcat:6.0 app=tomcat,pod-template-hash=5dddc7f6b6 app=tomcat,pod-template-hash=5dddc7f6b6
replicaset.apps/tomcat-deployment-7fbcf7c944 10 10 10 6m47s tomcat tomcat:8.0 app=tomcat,pod-template-hash=7fbcf7c944 app=tomcat,pod-template-hash=7fbcf7c944
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES LABELS
pod/tomcat-deployment-7fbcf7c944-6hh5w 1/1 Running 0 2m13s 10.244.1.95 k8sworker43-21 <none> <none> app=tomcat,pod-template-hash=7fbcf7c944
pod/tomcat-deployment-7fbcf7c944-6hjxj 1/1 Running 0 2m12s 10.244.2.79 k8sworker43-22 <none> <none> app=tomcat,pod-template-hash=7fbcf7c944
pod/tomcat-deployment-7fbcf7c944-9hp95 1/1 Running 0 2m8s 10.244.1.96 k8sworker43-21 <none> <none> app=tomcat,pod-template-hash=7fbcf7c944
pod/tomcat-deployment-7fbcf7c944-dk2jt 1/1 Running 0 2m16s 10.244.2.78 k8sworker43-22 <none> <none> app=tomcat,pod-template-hash=7fbcf7c944
pod/tomcat-deployment-7fbcf7c944-fkhrb 1/1 Running 0 2m19s 10.244.1.93 k8sworker43-21 <none> <none> app=tomcat,pod-template-hash=7fbcf7c944
pod/tomcat-deployment-7fbcf7c944-jc8vw 1/1 Running 0 2m18s 10.244.2.77 k8sworker43-22 <none> <none> app=tomcat,pod-template-hash=7fbcf7c944
pod/tomcat-deployment-7fbcf7c944-pfqxk 1/1 Running 0 2m16s 10.244.1.94 k8sworker43-21 <none> <none> app=tomcat,pod-template-hash=7fbcf7c944
pod/tomcat-deployment-7fbcf7c944-pm6rg 1/1 Running 0 2m8s 10.244.2.80 k8sworker43-22 <none> <none> app=tomcat,pod-template-hash=7fbcf7c944
pod/tomcat-deployment-7fbcf7c944-pxf5b 1/1 Running 0 2m6s 10.244.1.97 k8sworker43-21 <none> <none> app=tomcat,pod-template-hash=7fbcf7c944
pod/tomcat-deployment-7fbcf7c944-vnvjl 1/1 Running 0 2m19s 10.244.2.76 k8sworker43-22 <none> <none> app=tomcat,pod-template-hash=7fbcf7c944
- 删除Deployment、ReplicaSet和对应Pod的资源:
[root@k8smaster43-11 CSDN]# kubectl delete -f deployment.yaml
deployment.apps "tomcat-deployment" deleted
[root@k8smaster43-11 CSDN]#
对于滚动更新,不同的参数设置可以实现不同的更新效果:
# 先启动一个新Pod,再停止一个旧Pod,直到所以Pod都更新完
strategy:
rollingUpdate:
maxSurge: 1
maxUnavailable: 0
type: RollingUpdate
# 先停止一个旧Pod,再启动一个新Pod,直到所以Pod都更新完
strategy:
rollingUpdate:
maxSurge: 0
maxUnavailable: 1
type: RollingUpdate
# 停25%旧Pod、启动25%新Pod,直到所以Pod都更新完
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 25%
maxUnavailable: 25%