Docker&Kubernetes ❀ Kubernetes集群Pod控制器 - Deployment (Deploy)
Posted 无糖可乐没有灵魂
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Docker&Kubernetes ❀ Kubernetes集群Pod控制器 - Deployment (Deploy)相关的知识,希望对你有一定的参考价值。
文章目录
1、资源配置清单
为了更好的解决服务编排的问题,Kubernetes在V 1.2版本开始,引入了Deployment控制器,这种控制器并不会直接管理Pod,而是通过管理ReplicaSet来间接管理Pod,所以Deployment的功能比ReplicaSet更强大;
参数查询方法:
[root@master ~]# kubectl explain deploy
参数汇总梳理:
apiVersion: apps/v1 #版本信息
kind: Deployment #类型
metadata: #元数据
name:
namespace:
labels:
controller: deploy
spec: #详细描述
replicas: 3 #副本数量
revisionHistoryLimit: 30 #保留历史版本,默认为10
paused: false #暂停部署,默认为false
progressDeadlineSeconds: 600 #部署超时时间,默认600s
strategy: #策略
type: RollingUpdate #滚动更新策略
rollingUpdate: #设置滚动更新
maxSurge: 30% #最大额外可以存在的副本数量,可以为百分比或数值
maxUnavailable: 30% #最大不可用状态的副本数量,可以为百分比或数值
selector: #选择器,指定管理那些Pod
matchLabels: #匹配标签规则
app: nginx-pod
matchExpressions:
- key: "app"
operator: In
values: ["nginx-pod"]
template: #模板,当副本数量不足时,根据下面的配置创建Pod
metadata:
labels: #Pod标签
app: nginx-pod
spec:
containers:
- name: nginx
image: nginx:1.17.1
ports:
- containerPort: 80
2、创建控制器
#创建YAML文件
[root@master ~]# cat pc-deployment.yaml
apiVersion: apps/v1 #版本信息
kind: Deployment #类型
metadata: #元数据
name: pc-deployment
namespace: dev
spec: #详细描述
replicas: 3 #副本数量
selector: #选择器,指定管理那些Pod
matchLabels: #匹配标签规则
app: nginx-pod
template: #模板,当副本数量不足时,根据下面的配置创建Pod
metadata:
labels: #Pod标签
app: nginx-pod
spec:
containers:
- name: nginx
image: nginx:1.17.1
ports:
- containerPort: 80
#调用YAML文件
[root@master ~]# kubectl apply -f pc-deployment.yaml
deployment.apps/pc-deployment created
#查看deployment控制器
[root@master ~]# kubectl get deploy pc-deployment -n dev -o wide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
pc-deployment 3/3 3 3 40s nginx nginx:1.17.1 app=nginx-pod
# UP-TO-DATE:最新版本的副本数量;
# AVAILABLE:当前可用的副本数量;
查看RS
[root@master ~]# kubectl get rs -n dev
NAME DESIRED CURRENT READY AGE
pc-deployment-6756f95949 3 3 3 25s
#可以发现RS名称为:deployment_name-xxxxxxxxxx(十位随机码)
#查看deployment控制器创建的Pod
[root@master ~]# kubectl get pod -n dev
NAME READY STATUS RESTARTS AGE
pc-deployment-6756f95949-6vbj4 1/1 Running 0 47s
pc-deployment-6756f95949-l99gj 1/1 Running 0 47s
pc-deployment-6756f95949-xpj4f 1/1 Running 0 47s
# 可以发现Pod名称为:rs_name-xxxxx(五位随机码)
3、副本扩缩容
修改方式比较多,常用的有:Edit、命令修改、Apply(同2.3章节);
#edit
[root@master ~]# kubectl edit deploy pc-deployment -n dev
#命令修改
[root@master ~]# kubectl scale deploy pc-deployment --replicas=5 -n dev
#apply
[root@master ~]# kubectl apply -f pc-deployment.yaml
4、镜像更新
Deployment支持两种镜像更新策略:重建更新、滚动更新(默认)
- 重建更新:在创建出新的Pod之前会关闭所有已经存在的Pod;
- 滚动更新:关闭一部分(可以通过百分比或者数值规定)Pod,启动关闭的同等比例或者数值的Pod数量,在更新过程中,存在两个版本的Pod;
可以通过选项进行配置;
spec:
strategy: #策略
type: RollingUpdate #支持RollingUpdate滚动更新、Recreate重建更新两种
rollingUpdate: #设置滚动更新后需要设置的参数
maxSurge: 30% #最大额外可以存在的副本数量,可以为百分比或数值
maxUnavailable: 30% #最大不可用状态的副本数量,可以为百分比或数值
4.1 重建更新
(此实验建议使用两个会话终端,第一个窗口进行修改,第二个窗口实时监控查看状态信息)
#修改YAML文件,添加如下内容
[root@master ~]# cat pc-deployment.yaml
spec:
~
strategy:
type: Recreate
#使用edit修改镜像版本为1.17.3
[root@master ~]# kubectl edit deploy pc-deployment -n dev
deployment.apps/pc-deployment edited
#开启第二个会话终端,使用参数w监听pod创建过程
[root@master ~]# kubectl get pod -n dev -o wide -w
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
pc-deployment-6756f95949-j8bfv 1/1 Running 0 23s 10.244.112.39 node1.k8s <none> <none>
pc-deployment-6756f95949-pz7w7 1/1 Running 0 23s 10.244.166.155 node2.k8s <none> <none>
pc-deployment-6756f95949-zk96p 1/1 Running 0 23s 10.244.166.154 node2.k8s <none> <none>
#重建更新会关闭所有目前正在使用的Pod;
pc-deployment-6756f95949-pz7w7 1/1 Terminating 0 54s 10.244.166.155 node2.k8s <none> <none>
pc-deployment-6756f95949-zk96p 1/1 Terminating 0 54s 10.244.166.154 node2.k8s <none> <none>
pc-deployment-6756f95949-j8bfv 1/1 Terminating 0 54s 10.244.112.39 node1.k8s <none> <none>
pc-deployment-6756f95949-j8bfv 1/1 Terminating 0 54s 10.244.112.39 node1.k8s <none> <none>
pc-deployment-6756f95949-pz7w7 1/1 Terminating 0 54s 10.244.166.155 node2.k8s <none> <none>
pc-deployment-6756f95949-zk96p 1/1 Terminating 0 54s 10.244.166.154 node2.k8s <none> <none>
pc-deployment-6756f95949-zk96p 0/1 Terminating 0 55s 10.244.166.154 node2.k8s <none> <none>
pc-deployment-6756f95949-zk96p 0/1 Terminating 0 55s 10.244.166.154 node2.k8s <none> <none>
pc-deployment-6756f95949-zk96p 0/1 Terminating 0 55s 10.244.166.154 node2.k8s <none> <none>
pc-deployment-6756f95949-pz7w7 0/1 Terminating 0 55s 10.244.166.155 node2.k8s <none> <none>
pc-deployment-6756f95949-pz7w7 0/1 Terminating 0 55s 10.244.166.155 node2.k8s <none> <none>
pc-deployment-6756f95949-pz7w7 0/1 Terminating 0 55s 10.244.166.155 node2.k8s <none> <none>
pc-deployment-6756f95949-j8bfv 0/1 Terminating 0 55s <none> node1.k8s <none> <none>
pc-deployment-6756f95949-j8bfv 0/1 Terminating 0 55s <none> node1.k8s <none> <none>
pc-deployment-6756f95949-j8bfv 0/1 Terminating 0 55s <none> node1.k8s <none> <none>
#重新创建所有数量的新Pod来代替删除的旧Pod
pc-deployment-78dbc7d98c-56vc2 0/1 Pending 0 0s <none> <none> <none> <none>
pc-deployment-78dbc7d98c-t5b99 0/1 Pending 0 0s <none> <none> <none> <none>
pc-deployment-78dbc7d98c-4cg6f 0/1 Pending 0 0s <none> <none> <none> <none>
pc-deployment-78dbc7d98c-56vc2 0/1 Pending 0 0s <none> node2.k8s <none> <none>
pc-deployment-78dbc7d98c-t5b99 0/1 Pending 0 0s <none> node1.k8s <none> <none>
pc-deployment-78dbc7d98c-4cg6f 0/1 Pending 0 0s <none> node2.k8s <none> <none>
pc-deployment-78dbc7d98c-56vc2 0/1 ContainerCreating 0 0s <none> node2.k8s <none> <none>
pc-deployment-78dbc7d98c-t5b99 0/1 ContainerCreating 0 0s <none> node1.k8s <none> <none>
pc-deployment-78dbc7d98c-4cg6f 0/1 ContainerCreating 0 0s <none> node2.k8s <none> <none>
pc-deployment-78dbc7d98c-t5b99 0/1 ContainerCreating 0 1s <none> node1.k8s <none> <none>
pc-deployment-78dbc7d98c-4cg6f 0/1 ContainerCreating 0 1s <none> node2.k8s <none> <none>
pc-deployment-78dbc7d98c-56vc2 0/1 ContainerCreating 0 1s <none> node2.k8s <none> <none>
pc-deployment-78dbc7d98c-t5b99 1/1 Running 0 93s 10.244.112.40 node1.k8s <none> <none>
pc-deployment-78dbc7d98c-4cg6f 1/1 Running 0 98s 10.244.166.156 node2.k8s <none> <none>
pc-deployment-78dbc7d98c-56vc2 1/1 Running 0 112s 10.244.166.157 node2.k8s <none> <none>
#查看当前镜像版本
[root@master ~]# kubectl get deploy pc-deployment -n dev -o wide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
pc-deployment 3/3 3 3 3m56s nginx nginx:1.17.3 app=nginx-pod
4.2 滚动更新
(此实验建议使用三个会话终端,第一个窗口进行修改,第二个窗口实时监控rs控制器状态信息,第三个窗口实时监控查看Pod状态信息)
#修改YAML文件,添加如下内容
[root@master ~]# cat pc-deployment.yaml
spec:
~
strategy: #策略
type: RollingUpdate #RollingUpdate滚动更新
rollingUpdate:
maxSurge: 25%
maxUnavailable: 25%
#打开标记功能,record为标记参数,会将Deploy版本升级与回退操作进行日志记录,方便后面进行查看与回退操作
[root@master ~]# kubectl apply -f pc-deployment.yaml --record
Flag --record has been deprecated, --record will be removed in the future
deployment.apps/pc-deployment created
#使用edit修改镜像版本为1.17.3
[root@master ~]# kubectl edit deploy pc-deployment -n dev
deployment.apps/pc-deployment edited
#开启第二个会话终端,使用参数w监听rs控制器过程
[root@master ~]# kubectl get rs -n dev -w
NAME DESIRED CURRENT READY AGE
pc-deployment-6499c8d644 3 3 3 13m
#开启滚动更新
pc-deployment-78dbc7d98c 1 0 0 0s
pc-deployment-78dbc7d98c 1 0 0 0s
pc-deployment-78dbc7d98c 1 1 0 0s
pc-deployment-78dbc7d98c 1 1 1 1s
pc-deployment-6499c8d644 2 3 3 14m
#新rs创建第1个pod代替旧rs的第1个pod
pc-deployment-78dbc7d98c 2 1 1 1s
pc-deployment-6499c8d644 2 3 3 14m
pc-deployment-6499c8d644 2 2 2 14m
pc-deployment-78dbc7d98c 2 1 1 1s
pc-deployment-78dbc7d98c 2 2 1 1s
pc-deployment-78dbc7d98c 2 2 2 2s
pc-deployment-6499c8d644 1 2 2 14m
pc-deployment-6499c8d644 1 2 2 14m
#新rs创建第2个pod代替旧rs的第2个pod
pc-deployment-78dbc7d98c 3 2 2 2s
pc-deployment-6499c8d644 1 1 1 14m
pc-deployment-78dbc7d98c 3 2 2 2s
pc-deployment-78dbc7d98c 3 3 2 2s
pc-deployment-78dbc7d98c 3 3 3 3s
pc-deployment-6499c8d644 0 1 1 14m
pc-deployment-6499c8d644 0 1 1 14m
#新rs创建第3个pod代替旧rs的第3个pod
#开启第三个会话终端,使用参数w监听pod创建过程
[root@master ~]# kubectl get pod -n dev -o wide -w
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
pc-deployment-6499c8d644-7hbkc 1/1 Running 0 11m 10.244.166.164 node2.k8s <none> <none>
pc-deployment-6499c8d644-nfwdt 1/1 Running 0 14m 10.244.166.163 node2.k8s <none> <none>
pc-deployment-6499c8d644-sdd89 1/1 Running 0 12m 10.244.112.48 node1.k8s <none> <none>
#开始滚动更新
pc-deployment-78dbc7d98c-d7gv9 0/1 Pending 0 0s <none> <none> <none> <none>
pc-deployment-78dbc7d98c-d7gv9 0/1 Pending 0 0s <none> node1.k8s <none> <none>
pc-deployment-78dbc7d98c-d7gv9 0/1 ContainerCreating 0 0s <none> node1.k8s <none> <none>
pc-deployment-78dbc7d98c-d7gv9 0/1 ContainerCreating 0 1s <none> node1.k8s <none> <none>
pc-deployment-78dbc7d98c-d7gv9 1/1 Running 0 1s 10.244.112.49 node1.k8s <none> <none>
pc-deployment-6499c8d644-sdd89 1/1 Terminating 0 13m 10.244.112.48 node1.k8s <none> <none>
#新rs创建第1个pod代替旧rs的第1个pod
pc-deployment-78dbc7d98c-84jpr 0/1 Pending 0 0s <none> <none> <none> <none>
pc-deployment-78dbc7d98c-84jpr 0/1 Pending 0 0s <none> node2.k8s <none> <none>
pc-deployment-78dbc7d98c-84jpr 0/1 ContainerCreating 0 0s <none> node2.k8s <none> <none>
pc-deployment-6499c8d644-sdd89 1/1 Terminating 0 13m 10.244.112.48 node1.k8s <none> <none>
pc-deployment-78dbc7d98c-84jpr 0/1 ContainerCreating 0 1s <none> node2.k8s <none> <none>
pc-deployment-78dbc7d98c-84jpr 1/1 Running 0 1s 10.244.166.165 node2.k8s <none> <none>
pc-deployment-6499c8d644-nfwdt 1/1 Terminating 0 14m 10.244.166.163 node2.k8s <none> <none>
#新rs创建第2个pod代替旧rs的第2个pod
pc-deployment-78dbc7d98c-nckcs 0/1 Pending 0 0s <none> <none> <none> <none>
pc-deployment-78dbc7d98c-nckcs 0/1 Pending 0 0s <none> node1.k8s <none> <none>
pc-deployment-78dbc7d98c-nckcs 0/1 ContainerCreating 0 0s <none> node1.k8s <none> <none>
pc-deployment-6499c8d644-sdd89 0/1 Terminating 0 13m 10.244.112.48 node1.k8s <none> <none>
pc-deployment-6499c8d644-sdd89 0/1 Terminating 0 13m 10.244.112.48 node1.k8s <none> <none>
pc-deployment-6499c8d644-sdd89 0/1 Terminating 0 13m 10.244.112.48 node1.k8s <none> <none>
pc-deployment-6499c8d644-nfwdt 1/1 Terminating 0 14m 10.244.166.163 node2.k8s <none> <none>
pc-deployment-78dbc7d98c-nckcs 0/1 ContainerCreating 0 1s <none> node1.k8s <none> <none>
pc-deployment-6499c8d644-nfwdt 0/1 Terminating 0 14m 10.244.166.163 node2.k8s <none> <none>
pc-deployment-6499c8d644-nfwdt 0/1 Terminating 0 14m 10.244.166.163 node2.k8s <none> <none>
pc-deployment-6499c8d644-nfwdt 0/1 Terminating 0 14m 10.244.166.163 node2.k8s <none> <none>
pc-deployment-78dbc7d98c-nckcs 1/1 Running 0 1s 10.244.112.50 node1.k8s <none> <none>
pc-deployment-6499c8d644-7hbkc 1/1 Terminating 0 11m 10.244.166.164 node2.k8s <none> <none>
#新rs创建第3个pod代替旧rs的第3个pod
pc-deployment-6499c8d644-7hbkc 1/1 Terminating 0 11m 10.244.166.164 node2.k8s <none> <none>
pc-deployment-6499c8d644-7hbkc 0/1 Terminating 0 11m 10.244.166.164 node2.k8s <none> <none>
pc-deployment-6499c8d644-7hbkc 0/1 Terminating 0 11m 10.244.166.164 node2.k8s <none> <none>
pc-deployment-6499c8d644-7hbkc 0/1 Terminating 0 11m 10.244.166.164 node2.k8s <none> <none>
#查看当前Pod
[root@master ~]# kubectl get pod -n dev -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
pc-deployment-78dbc7d98c-84jpr 1/1 Running 0 4m57s 10.244.166.165 node2.k8s <none> <none>
pc-deployment-78dbc7d98c-d7gv9 1/1 Running 0 4m58s 10.244.112.49 node1.k8s <none> <none>
pc-deployment-78dbc7d98c-nckcs 1/1 Running 0 4m56s 10.244.112.50 node1.k8s <none> <none>
#查看rs控制器
[root@master ~]# kubectl get rs -n dev
NAME DESIRED CURRENT READY AGE
pc-deployment-6499c8d644 0 0 0 19m
pc-deployment-78dbc7d98c 3 3 3 5m14s
#查看当前镜像版本
[root@master ~]# kubectl get deploy pc-deployment -n dev -owide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
pc-deployment 3/3 3 3 23m nginx nginx:1.17.3 app=nginx-pod
5、版本回退
Deployment支持版本升级过程中的暂停、继续功能以及版本回退,具体如下:
[root@master ~]# kubectl rollout --help
~
Available Commands:
history View rollout history #显示历史升级记录
pause Mark the provided resource as paused #暂停版本升级过程
restart Restart a resource #重启版本升级过程
resume Resume a paused resource #继续已经暂停的版本升级过程
status Show the status of the rollout #显示当前升级状态
undo Undo a previous rollout #回退到上一个版本,可以使用--to-revision回退到指定版本
5.1 案例演示
#查看当前升级版本的状态
[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>
#此处日志记录显示为<none>的具体原因是因为在使用apply是未加入--record标志记录参数(实验章节3.4.2)
[root@master ~]# kubectl apply -f pc-deployment.yaml --record
Flag --record has been deprecated, --record will be removed in the future
deployment.apps/pc-deployment created
#正常情况下的日志记录
[root@master ~]# kubectl rollout history deploy pc-deployment -n dev
deployment.apps/pc-deployment
REVISION CHANGE-CAUSE
1 kubectl apply --filename=pc-deployment.yaml --record=true
2 kubectl apply --filename=pc-deployment.yaml --record=true
·
#版本回退
[root@master ~]# kubectl rollout undo deploy pc-deployment --to-revision=1 -n dev
deployment.apps/pc-deployment rolled back
#确认版本回退是否成功
[root@master ~]# kubectl get deploy pc-deployment -n dev -o wide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
pc-deployment 3/3 3 3 30m nginx nginx:1.17.1 app=nginx-pod
#查看新旧rs下所包含Pod信息
[root@master ~]# kubectl get rs -n dev
NAME DESIRED CURRENT READY AGE
pc-deployment-6499c8d644 3 3 3 19m
pc-deployment-78dbc7d98c 0 0 0 5m14s
6、金丝雀发布
Deployment控制器支持自定义控制更新过程中的滚动节奏,如暂停与继续等更新操作,比如等待第一批新的Pod资源创建完成后立即暂停更新过程,此时,仅存在一部分新版本的应用,主体部分还是旧版本应用,然后再筛选出一小部分的用户请求路由到新版本的应用,观察新版本应用是否能够稳定地按照期望的方式运行,确认运行正常后再继续完成剩余旧版本应用的升级,否则立即回滚到更新前版本;
#准备环境
[root@master ~]# kubectl apply -f pc-deployment.yaml
deployment.apps/pc-deployment created
[root@master ~]# kubectl get deploy pc-deployment -n dev -owide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
pc-deployment 3/3 3 3 20s nginx nginx:1.17.1 app=nginx-pod
#更新nginx版本,配置暂停deploy
[root@master ~]# kubectl set image deploy pc-deployment nginx=nginx:1.17.3 -n dev && kubectl rollout pause deployment pc-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: 1 out of 3 new replicas have been updated...
#查看当前暂停状态下的rs,此时新rs已经更新了一个pod,但是旧pod并没有删除原先的pod(暂停效果)
[root@master ~]# kubectl get rs -n dev -owide
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
pc-deployment-6756f95949 3 3 3 2m32s nginx nginx:1.17.1 app=nginx-pod,pod-template-hash=6756f95949
pc-deployment-78dbc7d98c 1 1 1 49s nginx nginx:1.17.3 app=nginx-pod,pod-template-hash=78dbc7d98c
#查看Pod
[root@master ~]# kubectl get pod -n dev
NAME READY STATUS RESTARTS AGE
pc-deployment-6756f95949-96t2g 1/1 Running 0 3m24s
pc-deployment-6756f95949-bqh9b 1/1 Running 0 3m24s
pc-deployment-6756f95949-fnhs7 1/1 Running 0 3m24s
pc-deployment-78dbc7d98c-wz4r5 1/1 Running 0 101s
#确认更新没有问题后,恢复更新
[root@master ~]# kubectl rollout resume deploy pc-deployment -n dev
deployment.apps/pc-deployment resumed
#在第二个终端上查看恢复更新后的状态信息
[root@master ~]# kubectl rollout status deploy pc-deployment -n dev
Waiting for deployment "pc-deployment" rollout to finish: 1 out of 3 new replicas have been updated...
Waiting for deployment spec update to be observed...
Waiting for deployment spec update to be observed...
Waiting for deployment "pc-deployment" rollout to finish: 1 out of 3 new replicas have been updated...
Waiting for deployment "pc-deployment" rollout to finish: 1 out of 3 new replicas have been updated...
Waiting for deployment "pc-deployment" rollout to finish: 2 out of 3 new replicas have been updated...
Waiting for deployment "pc-dep以上是关于Docker&Kubernetes ❀ Kubernetes集群Pod控制器 - Deployment (Deploy)的主要内容,如果未能解决你的问题,请参考以下文章
Docker&Kubernetes ❀ Kubernetes集群安装部署过程与常见的错误解决方法
Docker&Kubernetes ❀ Kubernetes集群安装部署过程与常见的错误解决方法
Docker&Kubernetes ❀ Kubernetes集群实践与部署笔记知识点梳理
Docker&Kubernetes ❀ Docker 容器技术笔记链接梳理