藍綠發布
藍綠部署是不停老版本,部署新版本然后進行測試,確認OK后將流量逐步切到新版本。藍綠部署無需停機,並且風險較小。
示例
本例是一個 nginx 應用,包含一個 deployment、 service 以及 ingress。deployment 通過 NodePort 對外暴露端口,並且有一個 ingress 正在對外提供服務。編排模板如下。
示例項目地址:https://code.aliyun.com/CodePipeline/kubernetes-deploy-demo.git
步驟一 部署版本1的應用(old-nginx)
編排模板如下:
apiVersion: extensions/v1beta1kind: Deploymentmetadata:name: old-nginxspec:replicas: 2selector:matchLabels:run: old-nginxtemplate:metadata:labels:run: old-nginxspec:containers:- image: registry.cn-hangzhou.aliyuncs.com/xianlu/old-nginximagePullPolicy: Alwaysname: old-nginxports:- containerPort: 80protocol: TCPrestartPolicy: AlwaysapiVersion: v1kind: Servicemetadata:name: old-nginxspec:ports:- port: 80protocol: TCPtargetPort: 80selector:run: old-nginxsessionAffinity: Nonetype: NodePortapiVersion: extensions/v1beta1kind: Ingressmetadata:name: gray-releasespec:rules:- host: www.example.comhttp:paths:- path: /backend:serviceName: old-nginxservicePort: 80
配置CodePipeline項目進行部署:
登錄Master節點,查詢Ingress的訪問地址
NAME HOSTS ADDRESS PORTS AGEgray-release www.example.com 47.97.238.11 80 8m
執行curl命令,查看路由的訪問情況:
#curl -H "Host: www.example.com" http://47.97.238.11old
步驟二 創建新版本的應用(new-nginx)
編排模板如下:
apiVersion: extensions/v1beta1kind: Deploymentmetadata:name: new-nginxspec:replicas: 1selector:matchLabels:run: new-nginxtemplate:metadata:labels:run: new-nginxspec:containers:- image: registry.cn-hangzhou.aliyuncs.com/xianlu/new-nginximagePullPolicy: Alwaysname: new-nginxports:- containerPort: 80protocol: TCPrestartPolicy: AlwaysapiVersion: v1kind: Servicemetadata:name: new-nginxspec:ports:- port: 80protocol: TCPtargetPort: 80selector:run: new-nginxsessionAffinity: Nonetype: NodePortapiVersion: extensions/v1beta1kind: Ingressmetadata:name: gray-releaseannotations:# 請求頭中滿足正則匹配foo=bar的請求才會被路由到新版本服務new-nginx中#nginx.ingress.kubernetes.io/service-match: |# new-nginx: header("foo", /^bar$/)nginx.ingress.kubernetes.io/service-weight: |new-nginx: ${NEW_PER}, old-nginx: ${OLD_PER}spec:rules:- host: www.example.comhttp:paths:- path: /backend:serviceName: old-nginxservicePort: 80- path: /backend:serviceName: new-nginxservicePort: 80
此處我們在編排模板中設置了${NEW_PER}和${OLD_PER}兩個環境變量來幫助調節路由權重。 您可通過靈活調整這兩個值來設置藍綠發布的流量占比。
配置CodePipeline項目進行部署:
設置NEW_PER為0,OLD_PER為100進行部署:
執行curl命令,查看路由的訪問情況:
#curl -H "Host: www.example.com" http://47.97.238.11old#curl -H "Host: www.example.com" http://47.97.238.11old#curl -H "Host: www.example.com" http://47.97.238.11old
設置NEW_PER為50,OLD_PER為50進行部署:
執行curl命令,查看路由的訪問情況:
#curl -H "Host: www.example.com" http://47.97.238.11old#curl -H "Host: www.example.com" http://47.97.238.11old#curl -H "Host: www.example.com" http://47.97.238.11old#curl -H "Host: www.example.com" http://47.97.238.11new#curl -H "Host: www.example.com" http://47.97.238.11new#curl -H "Host: www.example.com" http://47.97.238.11old
設置NEW_PER為100,OLD_PER為0進行部署:
執行curl命令,查看路由的訪問情況:
#curl -H "Host: www.example.com" http://47.97.238.11new#curl -H "Host: www.example.com" http://47.97.238.11new#curl -H "Host: www.example.com" http://47.97.238.11new#curl -H "Host: www.example.com" http://47.97.238.11new
完成新版本應用測試后,您可將 Ingress 的路由權重設置為 100 將流量完全導向新服務;或者刪除 Ingress 中的注解和舊版本服務,實現藍綠發布。
灰度發布
灰度發布是指在黑與白之間,能夠平滑過渡的一種發布方式。讓一部分用戶繼續用A,一部分用戶開始用B,如果用戶對B沒有什么反對意見,那么逐步擴大范圍,把所有用戶都遷移到B上面來。
假若我們希望請求頭中滿足foo=bar的客戶端請求才能路由到新版本服務(new-nginx)中,那么我們可以如下修改配置ingress規則:
編排模板如下:
apiVersion: extensions/v1beta1kind: Deploymentmetadata:name: new-nginxspec:replicas: 1selector:matchLabels:run: new-nginxtemplate:metadata:labels:run: new-nginxspec:containers:- image: registry.cn-hangzhou.aliyuncs.com/xianlu/new-nginximagePullPolicy: Alwaysname: new-nginxports:- containerPort: 80protocol: TCPrestartPolicy: AlwaysapiVersion: v1kind: Servicemetadata:name: new-nginxspec:ports:- port: 80protocol: TCPtargetPort: 80selector:run: new-nginxsessionAffinity: Nonetype: NodePortapiVersion: extensions/v1beta1kind: Ingressmetadata:name: gray-releaseannotations:# 請求頭中滿足正則匹配foo=bar的請求才會被路由到新版本服務new-nginx中nginx.ingress.kubernetes.io/service-match: |new-nginx: header("foo", /^bar$/)spec:rules:- host: www.example.comhttp:paths:- path: /backend:serviceName: old-nginxservicePort: 80- path: /backend:serviceName: new-nginxservicePort: 80
配置CodePipeline項目進行部署:
執行curl命令,查看路由的訪問情況:
#curl -H "Host: www.example.com" http://47.97.238.11old#curl -H "Host: www.example.com" http://47.97.238.11old#curl -H "Host: www.example.com" http://47.97.238.11old#curl -H "Host: www.example.com" -H "foo: bar" http://47.97.238.11new#curl -H "Host: www.example.com" -H "foo: bar" http://47.97.238.11new#curl -H "Host: www.example.com" -H "foo: bar" http://47.97.238.11new
來源:https://help.aliyun.com/knowledge_detail/85948.html









