deployment跟别的控制器不一样的是,deployment通过管理replicaset,来管理pod,deloyment比replicaset的功能更加强大,理所应当的给划归重点
deploy的功能有以下几个
- 支持replicaset的所有功能
- 支持发布的停止和继续
- 支持滚动更新和回滚更新
deploy的资源清单文件大概看一下以备不时之需
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
我们创建yaml看看
vi pc-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: pc-deployment
namespace: dev
spec:
replicas: 3
selector:
matchLabels:
run: nginx-pod
template:
metadata:
labels:
run: nginx-pod
spec:
containers:
- name: nginx
image: nginx:1.17.1
上节的pod创建以后是挂起状态我刚刚解决了,重启治百病,我重启了一下系统,又启动了一下minikube现在终于是可以正常创建pod了
[qq@k8snode1 ~]$ kubectl get pod,deploy -n dev
NAME READY STATUS RESTARTS AGE
pod/nginx-64b7c758f4-c4mfv 1/1 Running 0 4m14s
pod/nginx-64b7c758f4-khpp5 1/1 Running 0 4m14s
pod/nginx-64b7c758f4-rdk7w 1/1 Running 0 4m14s
pod/pc-deployment-774fccff58-lf5q2 1/1 Running 0 2m12s
pod/pc-deployment-774fccff58-sm28t 1/1 Running 0 2m12s
pod/pc-deployment-774fccff58-xxh4v 1/1 Running 0 2m12s
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/nginx 3/3 3 3 4m14s
deployment.apps/pc-deployment 3/3 3 3 2m12s
我们就继续了乌拉乌拉
扩缩容
我之前说过了,deploy通过管理rs来管理pod
下面我们就可以看我创建的deploy创建的rs和pod了
[qq@k8snode1 ~]$ kubectl get pod,deploy,rs -n dev
NAME READY STATUS RESTARTS AGE
pod/pc-deployment-774fccff58-lf5q2 1/1 Running 0 10m
pod/pc-deployment-774fccff58-sm28t 1/1 Running 0 10m
pod/pc-deployment-774fccff58-xxh4v 1/1 Running 0 10m
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/pc-deployment 3/3 3 3 10m
NAME DESIRED CURRENT READY AGE
replicaset.apps/pc-deployment-774fccff58 3 3 3 10m
教材上说的用命令手动添加pod
[root@k8s-master01 ~]# kubectl scale deploy pc-deployment --replicas=5 -n dev
deployment.apps/pc-deployment scaled
为啥啊,我直接改文件不香吗,说实话我可能写出来有点困难,看懂还是比较简单的吧
[qq@k8snode1 ~]$ kubectl edit deploy pc-deployment -n dev
deployment.apps/pc-deployment edited
------ replicas: 6 副本数量改了个6,有啥难的
[qq@k8snode1 ~]$ kubectl get pod,deploy,rs -n dev
# 可以看到加了3个pod,其他的基本没变
NAME READY STATUS RESTARTS AGE
pod/pc-deployment-774fccff58-bb7zx 1/1 Running 0 4m34s
pod/pc-deployment-774fccff58-j2m6w 1/1 Running 0 4m34s
pod/pc-deployment-774fccff58-lf5q2 1/1 Running 0 22m
pod/pc-deployment-774fccff58-sm28t 1/1 Running 0 22m
pod/pc-deployment-774fccff58-xxh4v 1/1 Running 0 22m
pod/pc-deployment-774fccff58-zfkkm 1/1 Running 0 4m34s
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/pc-deployment 6/6 6 6 22m
NAME DESIRED CURRENT READY AGE
replicaset.apps/pc-deployment-774fccff58 6 6 6 22m
镜像更新
[qq@k8snode1 ~]$ kubectl get pod -n dev -w
# 首先认识一下持续更新的命令,实乃神器啊,认识一下
NAME READY STATUS RESTARTS AGE
pc-deployment-774fccff58-bb7zx 1/1 Running 0 9m48s
pc-deployment-774fccff58-j2m6w 1/1 Running 0 9m48s
pc-deployment-774fccff58-lf5q2 1/1 Running 0 27m
pc-deployment-774fccff58-sm28t 1/1 Running 0 27m
pc-deployment-774fccff58-xxh4v 1/1 Running 0 27m
pc-deployment-774fccff58-zfkkm 1/1 Running 0 9m48s
deployment支持两种更新策略:重建更新和滚动更新,可以通过strategy指定策略类型,支持两个属性:
strategy:指定新的Pod替换旧的Pod的策略, 支持两个属性:
type:指定策略类型,支持两种策略
Recreate:在创建出新的Pod之前会先杀掉所有已存在的Pod
RollingUpdate:滚动更新,就是杀死一部分,就启动一部分,在更新过程中,存在两个版本Pod
rollingUpdate:当type为RollingUpdate时生效,用于为RollingUpdate设置参数,支持两个属性:
maxUnavailable:用来指定在升级过程中不可用Pod的最大数量,默认为25%。
maxSurge: 用来指定在升级过程中可以超过期望的Pod的最大数量,默认为25%。
先看看重建更新
编辑pc-deployment.yaml,在spec节点下添加更新策略
spec:
strategy: # 策略
type: Recreate # 重建更新
删除掉deploy刚好,我们看一下删除的时候pod的状态,注意,此处开了两个终端
一个终端执行命令,一个终端查看状态
kubectl get pod -n dev -w
[qq@k8snode1 ~]$ kubectl delete deploy pc-deployment -n dev
deployment.apps "pc-deployment" deleted
# 这边一个删除命令
另一边就有反应了
[qq@k8snode1 ~]$ kubectl get pod -n dev -w
NAME READY STATUS RESTARTS AGE
pc-deployment-774fccff58-bb7zx 1/1 Running 0 9m48s
pc-deployment-774fccff58-j2m6w 1/1 Running 0 9m48s
pc-deployment-774fccff58-lf5q2 1/1 Running 0 27m
pc-deployment-774fccff58-sm28t 1/1 Running 0 27m
pc-deployment-774fccff58-xxh4v 1/1 Running 0 27m
pc-deployment-774fccff58-zfkkm 1/1 Running 0 9m48s
pc-deployment-774fccff58-sm28t 1/1 Terminating 0 32m
pc-deployment-774fccff58-xxh4v 1/1 Terminating 0 32m
pc-deployment-774fccff58-j2m6w 1/1 Terminating 0 14m
pc-deployment-774fccff58-zfkkm 1/1 Terminating 0 14m
pc-deployment-774fccff58-bb7zx 1/1 Terminating 0 14m
pc-deployment-774fccff58-lf5q2 1/1 Terminating 0 32m
pc-deployment-774fccff58-lf5q2 0/1 Terminating 0 32m
pc-deployment-774fccff58-lf5q2 0/1 Terminating 0 32m
pc-deployment-774fccff58-lf5q2 0/1 Terminating 0 32m
pc-deployment-774fccff58-j2m6w 0/1 Terminating 0 14m
pc-deployment-774fccff58-j2m6w 0/1 Terminating 0 14m
pc-deployment-774fccff58-j2m6w 0/1 Terminating 0 14m
pc-deployment-774fccff58-bb7zx 0/1 Terminating 0 14m
pc-deployment-774fccff58-bb7zx 0/1 Terminating 0 14m
pc-deployment-774fccff58-bb7zx 0/1 Terminating 0 14m
pc-deployment-774fccff58-xxh4v 0/1 Terminating 0 32m
pc-deployment-774fccff58-xxh4v 0/1 Terminating 0 32m
pc-deployment-774fccff58-xxh4v 0/1 Terminating 0 32m
pc-deployment-774fccff58-zfkkm 0/1 Terminating 0 14m
pc-deployment-774fccff58-zfkkm 0/1 Terminating 0 14m
pc-deployment-774fccff58-zfkkm 0/1 Terminating 0 14m
pc-deployment-774fccff58-sm28t 0/1 Terminating 0 32m
pc-deployment-774fccff58-sm28t 0/1 Terminating 0 32m
pc-deployment-774fccff58-sm28t 0/1 Terminating 0 32m
# 这个自己看一下就好
我们继续改文件
把这一段粘贴到这里来
namespace: dev
spec:
strategy: # 策略
type: Recreate # 重建更新
replicas: 3
selector:
[qq@k8snode1 ~]$ kubectl apply -f pc-deployment.yaml
deployment.apps/pc-deployment created
看看状态pod从挂起,到容器创建,到运行
pc-deployment-774fccff58-npx7s 0/1 Pending 0 0s
pc-deployment-774fccff58-npx7s 0/1 Pending 0 0s
pc-deployment-774fccff58-wgdpd 0/1 Pending 0 1s
pc-deployment-774fccff58-lnfr6 0/1 Pending 0 1s
pc-deployment-774fccff58-wgdpd 0/1 Pending 0 1s
pc-deployment-774fccff58-lnfr6 0/1 Pending 0 1s
pc-deployment-774fccff58-npx7s 0/1 ContainerCreating 0 1s
pc-deployment-774fccff58-wgdpd 0/1 ContainerCreating 0 1s
pc-deployment-774fccff58-lnfr6 0/1 ContainerCreating 0 1s
pc-deployment-774fccff58-npx7s 1/1 Running 0 4s
pc-deployment-774fccff58-wgdpd 1/1 Running 0 4s
pc-deployment-774fccff58-lnfr6 1/1 Running 0 4s
[qq@k8snode1 ~]$ kubectl edit deploy pc-deployment -n dev
deployment.apps/pc-deployment edited
我把版本号改成了1.17.1
看状态
pc-deployment-774fccff58-lnfr6 1/1 Running 0 4s
pc-deployment-774fccff58-lnfr6 1/1 Terminating 0 4m44s
pc-deployment-774fccff58-npx7s 1/1 Terminating 0 4m44s
pc-deployment-774fccff58-wgdpd 1/1 Terminating 0 4m44s
pc-deployment-774fccff58-wgdpd 0/1 Terminating 0 4m45s
pc-deployment-774fccff58-wgdpd 0/1 Terminating 0 4m45s
pc-deployment-774fccff58-wgdpd 0/1 Terminating 0 4m45s
pc-deployment-774fccff58-lnfr6 0/1 Terminating 0 4m45s
pc-deployment-774fccff58-lnfr6 0/1 Terminating 0 4m46s
pc-deployment-774fccff58-lnfr6 0/1 Terminating 0 4m46s
pc-deployment-774fccff58-npx7s 0/1 Terminating 0 4m46s
pc-deployment-774fccff58-npx7s 0/1 Terminating 0 4m46s
pc-deployment-774fccff58-npx7s 0/1 Terminating 0 4m46s
pc-deployment-5d776b56ff-j4cq4 0/1 Pending 0 0s
pc-deployment-5d776b56ff-j4cq4 0/1 Pending 0 0s
pc-deployment-5d776b56ff-47qrc 0/1 Pending 0 0s
pc-deployment-5d776b56ff-mgqdk 0/1 Pending 0 0s
pc-deployment-5d776b56ff-mgqdk 0/1 Pending 0 0s
pc-deployment-5d776b56ff-47qrc 0/1 Pending 0 0s
pc-deployment-5d776b56ff-j4cq4 0/1 ContainerCreating 0 1s
pc-deployment-5d776b56ff-47qrc 0/1 ContainerCreating 0 1s
pc-deployment-5d776b56ff-mgqdk 0/1 ContainerCreating 0 2s
pc-deployment-5d776b56ff-j4cq4 1/1 Running 0 40s
pc-deployment-5d776b56ff-47qrc 1/1 Running 0 42s
pc-deployment-5d776b56ff-mgqdk 1/1 Running 0 45s
再查一下你会发现,之前的rs还在,这是为回滚准备的
[qq@k8snode1 ~]$ kubectl get pod,deploy,rs -n dev
NAME READY STATUS RESTARTS AGE
pod/pc-deployment-5d776b56ff-47qrc 1/1 Running 0 13m
pod/pc-deployment-5d776b56ff-j4cq4 1/1 Running 0 13m
pod/pc-deployment-5d776b56ff-mgqdk 1/1 Running 0 13m
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/pc-deployment 3/3 3 3 18m
NAME DESIRED CURRENT READY AGE
replicaset.apps/pc-deployment-5d776b56ff 3 3 3 13m
replicaset.apps/pc-deployment-774fccff58 0 0 0 18m
滚动更新
改文件
spec:
strategy: # 策略
type: RollingUpdate # 滚动更新策略
rollingUpdate:
maxSurge: 25%
maxUnavailable: 25%
[qq@k8snode1 ~]$ kubectl edit deploy pc-deployment -n dev
deployment.apps/pc-deployment edited
pc-deployment-5d776b56ff-mgqdk 1/1 Running 0 45s
pc-deployment-785c7c59f6-fv769 0/1 Pending 0 0s
pc-deployment-785c7c59f6-fv769 0/1 Pending 0 0s
pc-deployment-785c7c59f6-fv769 0/1 ContainerCreating 0 0s
pc-deployment-785c7c59f6-fv769 1/1 Running 0 32s
pc-deployment-785c7c59f6-j69pm 0/1 Pending 0 0s
pc-deployment-5d776b56ff-47qrc 1/1 Terminating 0 19m
pc-deployment-785c7c59f6-j69pm 0/1 Pending 0 0s
pc-deployment-785c7c59f6-j69pm 0/1 ContainerCreating 0 0s
pc-deployment-5d776b56ff-47qrc 0/1 Terminating 0 19m
pc-deployment-5d776b56ff-47qrc 0/1 Terminating 0 19m
pc-deployment-5d776b56ff-47qrc 0/1 Terminating 0 19m
pc-deployment-785c7c59f6-j69pm 1/1 Running 0 2s
pc-deployment-5d776b56ff-mgqdk 1/1 Terminating 0 19m
pc-deployment-785c7c59f6-85n4f 0/1 Pending 0 1s
pc-deployment-785c7c59f6-85n4f 0/1 Pending 0 1s
pc-deployment-785c7c59f6-85n4f 0/1 ContainerCreating 0 1s
pc-deployment-785c7c59f6-85n4f 1/1 Running 0 1s
pc-deployment-5d776b56ff-mgqdk 0/1 Terminating 0 19m
pc-deployment-5d776b56ff-j4cq4 1/1 Terminating 0 19m
pc-deployment-5d776b56ff-mgqdk 0/1 Terminating 0 19m
pc-deployment-5d776b56ff-mgqdk 0/1 Terminating 0 19m
pc-deployment-5d776b56ff-j4cq4 0/1 Terminating 0 19m
pc-deployment-5d776b56ff-j4cq4 0/1 Terminating 0 19m
pc-deployment-5d776b56ff-j4cq4 0/1 Terminating 0 19m
状态这边的意思看着有点多,也得分方法看
例
pc-deployment-5d776b56ff-mgqdk 1/1 Running 0 45s
| deploy名 | rs编号 | pod随机名
虽乱,但是还能分析出来的,刚开始pod在5d的rs里
deploy创建了78rs,在78rs里创建了fv769pod
然后干掉了一台5drs里的pod,以此模式滚动更新,慢慢所以pod都被更新到了78rs里
版本回退
deployment支持版本升级过程中的暂停、继续功能以及版本回退等诸多功能,下面具体来看.
kubectl rollout: 版本升级相关功能,支持下面的选项:
- status 显示当前升级状态
- history 显示 升级历史记录
- pause 暂停版本升级过程
- resume 继续已经暂停的版本升级过程
- restart 重启版本升级过程
- undo 回滚到上一级版本(可以使用–to-revision回滚到指定版本)
[qq@k8snode1 ~]$ kubectl rollout status deploy pc-deployment -n dev
deployment "pc-deployment" successfully rolled out
查看当前的版本更新状态
[qq@k8snode1 ~]$ kubectl rollout history deploy pc-deployment -n dev
deployment.apps/pc-deployment
REVISION CHANGE-CAUSE
1 <none>
2 <none>
3 <none>
有三次版本更新记录,说明完成过两次升级
版本回退
我们先回滚版本1看看
[qq@k8snode1 ~]$ kubectl rollout undo deploy pc-deployment --to-revision=1 -n dev
deployment.apps/pc-deployment rolled back
[qq@k8snode1 ~]$ kubectl get deploy -n dev -o wide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
pc-deployment 3/3 3 3 3h20m nginx nginx:1.17.1 run=nginx-pod
[qq@k8snode1 ~]$ kubectl get rs -n dev -o wide
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
pc-deployment-5d776b56ff 0 0 0 3h19m nginx nginx:1.17.2 pod-template-hash=5d776b56ff,run=nginx-pod
pc-deployment-774fccff58 3 3 3 3h23m nginx nginx:1.17.1 pod-template-hash=774fccff58,run=nginx-pod
pc-deployment-785c7c59f6 0 0 0 3h nginx nginx:1.17.3 pod-template-hash=785c7c59f6,run=nginx-pod
# 查看rs,发现第一个rs中有3个pod运行,后面两个版本的rs中pod未运行
# 其实deployment之所以可以实现版本的回滚,就是通过记录下历史rs来实现的,
# 一旦想回滚到哪个版本,只需要将当前版本pod数量降为0,然后将回滚版本的pod提升为目标数量就可以了
金丝雀发布
Deployment控制器支持控制更新过程中的控制,如“暂停(pause)”或“继续(resume)”更新操作。
比如有一批新的Pod资源创建完成后立即暂停更新过程,此时,仅存在一部分新版本的应用,主体部分还是旧的版本。然后,再筛选一小部分的用户请求路由到新版本的Pod应用,继续观察能否稳定地按期望的方式运行。确定没问题之后再继续完成余下的Pod资源滚动更新,否则立即回滚更新操作。这就是所谓的金丝雀发布。
更新deployment的版本,并配置暂停deploymen
[qq@k8snode1 ~]$ kubectl set image deploy pc-deployment nginx=nginx:1.17.4 -n dev && kubectl rollout pause deployment pc-deployment -n dev
deployment.apps/pc-deployment image updated
deployment.apps/pc-deployment paused
稍等一下,看一下状态,发现又创建了一个rs里面运行着一个容器,同时原版本没有减少
[qq@k8snode1 ~]$ kubectl get rs -n dev
NAME DESIRED CURRENT READY AGE
pc-deployment-5d776b56ff 0 0 0 3h27m
pc-deployment-6d6bc4bd5 1 1 1 2m7s
pc-deployment-774fccff58 3 3 3 3h32m
pc-deployment-785c7c59f6 0 0 0 3h9m
在看一下更新状态,可以看到更新了1个,更新未完成
[qq@k8snode1 ~]$ 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...
这种情况下在生产环境可以分配一小部分用户去访问新的版本,如果新版本没问题了,就可以完全更新了
[qq@k8snode1 ~]$ kubectl rollout resume deploy pc-deployment -n dev
deployment.apps/pc-deployment resumed
原版本的3台机没了,全部更换为了新版本
[qq@k8snode1 ~]$ kubectl get rs -n dev
NAME DESIRED CURRENT READY AGE
pc-deployment-5d776b56ff 0 0 0 3h40m
pc-deployment-6d6bc4bd5 3 3 3 14m
pc-deployment-774fccff58 0 0 0 3h45m
pc-deployment-785c7c59f6 0 0 0 3h22m
看一下更新状态,也结束了
[qq@k8snode1 ~]$ kubectl rollout status deploy pc-deployment -n dev
deployment "pc-deployment" successfully rolled out
结束实验删除环境
rs,deploy,pod都会被删除
[qq@k8snode1 ~]$ kubectl delete -f pc-deployment.yaml
deployment.apps "pc-deployment" deleted