目录
1、Deployment控制器支持自定义控制更新过程中的滚动节奏,如暂停(pause)或者继续(resume)更新操作。
3、更新deployment的版本,并配置暂停deployment
监控更新的过程,可以看到已经新增了一个资源,但是并未按照预期的状态去删除一个旧的资源,就是因为使用了pause的暂停命令
当出现deployment "nginx-01" successfully rolled out时说明已经更新完毕
默认情况下,访问server流量将会负载均衡到4个实例上,新增server实现新的实例与旧实例访问分流:
查看pod实例标签名;编辑查看位于命名空间rmh的名为new-nginx的service资源对象,复制文本内容,并创建对应的yaml文件,修改标签选择器内容:
删除kubernetes svc资源,并根据配置文件创建或更新资源
同样,编辑位于命名空间rmh中名为nginx-service的service资源对象,复制文本内容,并创建对应的yaml文件,修改标签选择器内容:
可以通过不同的server标签,来完成金丝雀的发布,将流量分流到新旧版本的过程
一、金丝雀发布(灰度发布)
1、Deployment控制器支持自定义控制更新过程中的滚动节奏,如暂停(pause)或者继续(resume)更新操作。
①比如等待第一批新的Pod资源创建完成后立即暂停更新过程,此时,仅存在一部分新版本的应用,主版本还是旧的版本:
②然后,在筛选一小部分的用户请求路由到新版本的Pod应用,继续观察能否稳定的按期望的方式运行
③确定没问题之后再继续完成余下的Pod资源滚动更新,否则立即回滚更新操作。这就是所谓的金丝雀发布
2、准备工作
[root@master01 ~]]#kubectl get ns
NAME STATUS AGE
default Active 6d1h
kube-flannel Active 6d
kube-node-lease Active 6d1h
kube-public Active 6d1h
kube-system Active 6d1h
kubernetes-dashboard Active 6d
[root@master01 ~]]#kubectl create ns rmh
namespace/rmh created
[root@master01 ~]]#kubectl create deployment nginx-01 --image=nginx:1.14 --port=80 --replicas=3 -n rmh
deployment.apps/nginx-01 created
[root@master01 ~]]#kubectl get pod -owide -n rmh
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-01-799fb6fb65-hhxmb 0/1 ContainerCreating 0 15s <none> node01 <none> <none>
nginx-01-799fb6fb65-jctbh 0/1 ContainerCreating 0 15s <none> node02 <none> <none>
nginx-01-799fb6fb65-n2772 0/1 ContainerCreating 0 15s <none> node01 <none> <none>
[root@master01 ~]]#kubectl expose deployment nginx-01 --port=80 --target-port=80 --name=service -n rmh --type=NodePort
service/service exposed
[root@master01 ~]]#kubectl get svc -n rmh -o wide
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
service NodePort 10.96.172.149 <none> 80:31573/TCP 12s app=nginx-01
[root@master01 ~]]#curl -I 10.96.172.149
HTTP/1.1 200 OK
Server: nginx/1.14.2
Date: Wed, 22 May 2024 09:29:57 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 04 Dec 2018 14:44:49 GMT
Connection: keep-alive
ETag: "5c0692e1-264"
Accept-Ranges: bytes
3、更新deployment的版本,并配置暂停deployment
[root@master01 ~]# kubectl set image deployment nginx-01 nginx=nginx:1.15 -n rmh && kubectl rollout pause deployment nginx-01 -n rmh
deployment.apps/nginx-01 image updated
deployment.apps/nginx-01 paused
[root@master01 ~]# kubectl rollout status deployment nginx-01 -n rmh
Waiting for deployment "nginx-01" rollout to finish: 1 out of 3 new replicas have been updated...
# 更新名为"nginx-01"的部署(Deployment)中的 "nginx" 容器的镜像版本为"nginx:1.15
# 暂停名为"nginx-01"的部署的滚动更新,这意味着在执行这个命令后,将不会继续推进新的副本集,并且当前的副本集将保持不变
4、开启另外一个窗口查看pod信息
监控更新的过程,可以看到已经新增了一个资源,但是并未按照预期的状态去删除一个旧的资源,就是因为使用了pause的暂停命令
[root@master01 ~]]#kubectl get pods -w -n rmh
NAME READY STATUS RESTARTS AGE
nginx-01-78cb4c6b78-7x46z 1/1 Running 0 2m40s
nginx-01-799fb6fb65-hhxmb 1/1 Running 0 7m52s
nginx-01-799fb6fb65-jctbh 1/1 Running 0 7m52s
nginx-01-799fb6fb65-n2772 1/1 Running 0 7m52s
查看nginx的版本信息:
[root@master01 ~]]#kubectl get pod -n rmh -owide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-01-78cb4c6b78-7x46z 1/1 Running 0 3m36s 10.244.1.14 node02 <none> <none>
nginx-01-799fb6fb65-hhxmb 1/1 Running 0 8m48s 10.244.2.15 node01 <none> <none>
nginx-01-799fb6fb65-jctbh 1/1 Running 0 8m48s 10.244.1.13 node02 <none> <none>
nginx-01-799fb6fb65-n2772 1/1 Running 0 8m48s 10.244.2.14 node01 <none> <none>
[root@master01 ~]]#kubectl get svc -n rmh
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service NodePort 10.96.172.149 <none> 80:31573/TCP 5m43s
[root@master01 ~]]#curl -I 10.244.1.14
HTTP/1.1 200 OK
Server: nginx/1.15.12
Date: Wed, 22 May 2024 09:35:25 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 16 Apr 2019 13:08:19 GMT
Connection: keep-alive
ETag: "5cb5d3c3-264"
Accept-Ranges: bytes
[root@master01 ~]]#curl -I 10.244.2.15
HTTP/1.1 200 OK
Server: nginx/1.14.2
Date: Wed, 22 May 2024 09:35:48 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 04 Dec 2018 14:44:49 GMT
Connection: keep-alive
ETag: "5c0692e1-264"
Accept-Ranges: bytes
[root@master01 ~]]#curl -I 10.244.2.14
HTTP/1.1 200 OK
Server: nginx/1.14.2
Date: Wed, 22 May 2024 09:35:55 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 04 Dec 2018 14:44:49 GMT
Connection: keep-alive
ETag: "5c0692e1-264"
Accept-Ranges: bytes
[root@master01 ~]]#curl -I 10.244.1.13
HTTP/1.1 200 OK
Server: nginx/1.14.2
Date: Wed, 22 May 2024 09:36:02 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 04 Dec 2018 14:44:49 GMT
Connection: keep-alive
ETag: "5c0692e1-264"
Accept-Ranges: bytes
5、确保更新的pod没问题,继续更新
[root@master01 ~]]#kubectl rollout resume deployment nginx-01 -n rmh
deployment.apps/nginx-01 resumed
6、查看最后的更新状况
[root@master01 ~]# kubectl get pod -w -n rmh
[root@master01 ~]]#kubectl get pod -n rmh -owide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-01-78cb4c6b78-24x72 1/1 Running 0 54s 10.244.1.15 node02 <none> <none>
nginx-01-78cb4c6b78-626xv 0/1 ContainerCreating 0 53s <none> node01 <none> <none>
nginx-01-78cb4c6b78-7x46z 1/1 Running 0 7m11s 10.244.1.14 node02 <none> <none>
nginx-01-799fb6fb65-n2772 1/1 Running 0 12m 10.244.2.14 node01 <none> <none>
当出现deployment "nginx-01" successfully rolled out时说明已经更新完毕
7、分阶段访问
在金丝雀发布中,将流量分流到新旧版本的这个过程被称为分阶段访问(Staged Access),也可以称为阶段性流量调度(Staged Traffic Shifting),就是将流量逐步引导到新版本的过程,以确保新版本的稳定性和可靠性。
[root@master01 ~]]#kubectl set image deployment nginx-01 nginx=nginx:1.16 -n rmh && kubectl rollout pause deployment nginx-01 -n rmh
deployment.apps/nginx-01 image updated
deployment.apps/nginx-01 paused
[root@master01 ~]]#kubectl get pod -n rmh
NAME READY STATUS RESTARTS AGE
nginx-01-78cb4c6b78-24x72 1/1 Running 0 6m5s
nginx-01-78cb4c6b78-626xv 1/1 Running 0 6m4s
nginx-01-78cb4c6b78-7x46z 1/1 Running 0 12m
nginx-01-85c54f54dc-c6q8b 0/1 ContainerCreating 0 14s
默认情况下,访问server流量将会负载均衡到4个实例上,新增server实现新的实例与旧实例访问分流:
[root@master01 ~]]#kubectl expose deployment nginx-01 -n rmh --port=80 --target-port=80 --name=new-nginx --type=NodePort
service/new-nginx exposed
[root@master01 ~]]#kubectl get svc -n rmh
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
new-nginx NodePort 10.96.154.134 <none> 80:30244/TCP 16s
service NodePort 10.96.172.149 <none> 80:31573/TCP 16m
查看pod实例标签名;编辑查看位于命名空间rmh的名为new-nginx的service资源对象,复制文本内容,并创建对应的yaml文件,修改标签选择器内容:
[root@master01 ~]]#kubectl get pod --show-labels -n rmh
NAME READY STATUS RESTARTS AGE LABELS
nginx-01-78cb4c6b78-24x72 1/1 Running 0 9m57s app=nginx-01,pod-template-hash=78cb4c6b78
nginx-01-78cb4c6b78-626xv 1/1 Running 0 9m56s app=nginx-01,pod-template-hash=78cb4c6b78
nginx-01-78cb4c6b78-7x46z 1/1 Running 0 16m app=nginx-01,pod-template-hash=78cb4c6b78
nginx-01-85c54f54dc-c6q8b 1/1 Running 0 4m6s app=nginx-01,pod-template-hash=85c54f54dc
[root@master01 ~]]#kubectl edit svc new-nginx -n rmh
# Please edit the object below. Lines beginning with a '#' will be ignored,
# and an empty file will abort the edit. If an error occurs while saving this file will be
# reopened with the relevant failures.
#
apiVersion: v1
kind: Service
metadata:
creationTimestamp: "2024-05-22T09:45:38Z"
labels:
app: nginx-01
name: new-nginx
namespace: rmh
resourceVersion: "49767"
uid: 824f80c5-7c70-41c9-84f7-ffafee88cffd
spec:
clusterIP: 10.96.154.134
clusterIPs:
- 10.96.154.134
externalTrafficPolicy: Cluster
ports:
- nodePort: 30244
port: 80
protocol: TCP
targetPort: 80
selector:
app: nginx-01
sessionAffinity: None
type: NodePort
status:
"/tmp/kubectl-edit-bqgbt.yaml" 30L, 700C
[root@master01 ~]# kubectl edit svc new-nginx -n rmh #编辑复制文本
[root@master01 ~]# mkdir yaml;cd yaml
[root@master01 yaml]# vim new-nginx.yaml
[root@master01 yaml]]#vim new-nginx.yaml
apiVersion: v1
kind: Service
metadata:
labels:
app: nginx-01
name: new-nginx
namespace: rmh
spec:
clusterIP: 10.96.154.134
clusterIPs:
- 10.96.154.134
externalTrafficPolicy: Cluster
ports:
- nodePort: 30244
port: 80
protocol: TCP
targetPort: 80
selector:
pod-template-hash:85c54f54dc
sessionAffinity: None
type: NodePort
删除kubernetes svc资源,并根据配置文件创建或更新资源
[root@master01 yaml]]#kubectl delete svc new-nginx -n rmh
service "new-nginx" deleted
[root@master01 yaml]]#kubectl get svc -n rmh
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service NodePort 10.96.172.149 <none> 80:31573/TCP 26m
[root@master01 yaml]]#kubectl apply -f new-nginx.yaml -n rmh
service/new-nginx created
[root@master01 yaml]]#kubectl get svc -n rmh
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
new-nginx NodePort 10.96.154.134 <none> 80:30244/TCP 19s
service NodePort 10.96.172.149 <none> 80:31573/TCP 31m
[root@master01 yaml]]#kubectl get endpoints new-nginx -n rmh
NAME ENDPOINTS AGE
new-nginx 10.244.2.17:80 33s
访问升级版本的pod,查看流量调度是否正确:
[root@master01 yaml]]#curl -I 10.244.2.17
HTTP/1.1 200 OK
Server: nginx/1.16.1
Date: Wed, 22 May 2024 10:02:28 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 13 Aug 2019 10:05:00 GMT
Connection: keep-alive
ETag: "5d528b4c-264"
Accept-Ranges: bytes
同样,编辑位于命名空间rmh中名为nginx-service的service资源对象,复制文本内容,并创建对应的yaml文件,修改标签选择器内容:
[root@master01 yaml]]#vim nginx-server.yaml
apiVersion: v1
kind: Service
metadata:
labels:
app: nginx-01
name: service
namespace: rmh
spec:
clusterIP: 10.96.127.149
clusterIPs:
- 10.96.127.149
externalTrafficPolicy: Cluster
ports:
- nodePort: 31573
port: 80
protocol: TCP
targetPort: 80
selector:
pod-template-hash:78cb4c6b78
sessionAffinity: None
type: NodePort
[root@master01 yaml]]#vim nginx-server.yaml
[root@master01 yaml]]#kubectl get svc -n rmh
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
new-nginx NodePort 10.96.154.134 <none> 80:30244/TCP 11m
service NodePort 10.96.172.149 <none> 80:31573/TCP 42m
[root@master01 yaml]]#kubectl delete svc service -n rmh
service "service" deleted
[root@master01 yaml]]#ls
new-nginx.yaml nginx-server.yaml
[root@master01 yaml]]#vim nginx-server.yaml
[root@master01 yaml]]#kubectl apply -f nginx-server.yaml -n rmh
service/service created
[root@master01 yaml]]#kubectl get svc -n rmh
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
new-nginx NodePort 10.96.154.134 <none> 80:30244/TCP 13m
service NodePort 10.96.127.149 <none> 80:31573/TCP 11s
[root@master01 yaml]]#kubectl get endpoints service -n rmh
NAME ENDPOINTS AGE
service 10.244.1.14:80,10.244.1.15:80,10.244.2.16:80 37s
访问旧版本pod,查看流量调度是否正确
[root@master01 yaml]]#curl -I 10.244.1.14
HTTP/1.1 200 OK
Server: nginx/1.15.12
Date: Wed, 22 May 2024 10:15:45 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 16 Apr 2019 13:08:19 GMT
Connection: keep-alive
ETag: "5cb5d3c3-264"
Accept-Ranges: bytes
[root@master01 yaml]]#curl -I 10.244.1.15
HTTP/1.1 200 OK
Server: nginx/1.15.12
Date: Wed, 22 May 2024 10:15:56 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 16 Apr 2019 13:08:19 GMT
Connection: keep-alive
ETag: "5cb5d3c3-264"
Accept-Ranges: bytes
[root@master01 yaml]]#curl -I 10.244.2.16
HTTP/1.1 200 OK
Server: nginx/1.15.12
Date: Wed, 22 May 2024 10:16:08 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 16 Apr 2019 13:08:19 GMT
Connection: keep-alive
ETag: "5cb5d3c3-264"
Accept-Ranges: bytes
可以通过不同的server标签,来完成金丝雀的发布,将流量分流到新旧版本的过程
二、蓝绿发布
蓝绿发布是一种部署新版本应用程序的策略,旨在减少对用户造成的影响。在蓝绿发布中,两个相同的生成环境并行存在,一个被标记为蓝色,一个被标记为绿色
1、蓝色环境:当前稳定的生产环境
2、绿色环境:新版本的生产环境
在初始阶段所用的用户流量都会指向蓝色环境。当新版本准备就绪时,流量可以逐渐转移到绿色环境中。这种逐步迁移流量的方式允许进行实时及监控,并在出现问题时快速的回滚到蓝色环境。一旦绿色环境被验证为稳定可靠,蓝色环境可以被废弃或者保留作为备份。
三、总结
1、滚动发布
就是按照一部分一部分的滚动更新;创建一定比例的pod,先创建再删除旧的pod。
2、金丝雀发布(灰度发布)
先更新一部分pod,然后暂停更新
安排一小部分的用户进行流量访问更新的pod来测试
当测试没有问题后再扩大比例,知道全部更新完成为止。
3、蓝绿发布
蓝:就是正在运行的稳定版本
绿:就是新版本的副本
进行新旧版本的切换,用户无感知,业务稳定;但是需要大量了资源,成本比较高。