阿里雲 - 灰度發布/藍綠發布


藍綠發布

藍綠部署是不停老版本,部署新版本然后進行測試,確認OK后將流量逐步切到新版本。藍綠部署無需停機,並且風險較小。

示例

本例是一個 nginx 應用,包含一個 deployment、 service 以及 ingress。deployment 通過 NodePort 對外暴露端口,並且有一個 ingress 正在對外提供服務。編排模板如下。

示例項目地址:https://code.aliyun.com/CodePipeline/kubernetes-deploy-demo.git

步驟一 部署版本1的應用(old-nginx)

編排模板如下:

 
  1. apiVersion: extensions/v1beta1
  2. kind: Deployment
  3. metadata:
  4. name: old-nginx
  5. spec:
  6. replicas: 2
  7. selector:
  8. matchLabels:
  9. run: old-nginx
  10. template:
  11. metadata:
  12. labels:
  13. run: old-nginx
  14. spec:
  15. containers:
  16. - image: registry.cn-hangzhou.aliyuncs.com/xianlu/old-nginx
  17. imagePullPolicy: Always
  18. name: old-nginx
  19. ports:
  20. - containerPort: 80
  21. protocol: TCP
  22. restartPolicy: Always
  23. ---
  24. apiVersion: v1
  25. kind: Service
  26. metadata:
  27. name: old-nginx
  28. spec:
  29. ports:
  30. - port: 80
  31. protocol: TCP
  32. targetPort: 80
  33. selector:
  34. run: old-nginx
  35. sessionAffinity: None
  36. type: NodePort
  37. ---
  38. apiVersion: extensions/v1beta1
  39. kind: Ingress
  40. metadata:
  41. name: gray-release
  42. spec:
  43. rules:
  44. - host: www.example.com
  45. http:
  46. paths:
  47. # 老版本服務
  48. - path: /
  49. backend:
  50. serviceName: old-nginx
  51. servicePort: 80

配置CodePipeline項目進行部署:

登錄Master節點,查詢Ingress的訪問地址

 
  1. # kubectl get ing
  2. NAME HOSTS ADDRESS PORTS AGE
  3. gray-release www.example.com 47.97.238.11 80 8m

執行curl命令,查看路由的訪問情況:

 
  1. #curl -H "Host: www.example.com" http://47.97.238.11
  2. old

步驟二 創建新版本的應用(new-nginx)

編排模板如下:

 
  1. apiVersion: extensions/v1beta1
  2. kind: Deployment
  3. metadata:
  4. name: new-nginx
  5. spec:
  6. replicas: 1
  7. selector:
  8. matchLabels:
  9. run: new-nginx
  10. template:
  11. metadata:
  12. labels:
  13. run: new-nginx
  14. spec:
  15. containers:
  16. - image: registry.cn-hangzhou.aliyuncs.com/xianlu/new-nginx
  17. imagePullPolicy: Always
  18. name: new-nginx
  19. ports:
  20. - containerPort: 80
  21. protocol: TCP
  22. restartPolicy: Always
  23. ---
  24. apiVersion: v1
  25. kind: Service
  26. metadata:
  27. name: new-nginx
  28. spec:
  29. ports:
  30. - port: 80
  31. protocol: TCP
  32. targetPort: 80
  33. selector:
  34. run: new-nginx
  35. sessionAffinity: None
  36. type: NodePort
  37. ---
  38. apiVersion: extensions/v1beta1
  39. kind: Ingress
  40. metadata:
  41. name: gray-release
  42. annotations:
  43. # 請求頭中滿足正則匹配foo=bar的請求才會被路由到新版本服務new-nginx中
  44. #nginx.ingress.kubernetes.io/service-match: |
  45. # new-nginx: header("foo", /^bar$/)
  46. nginx.ingress.kubernetes.io/service-weight: |
  47. new-nginx: ${NEW_PER}, old-nginx: ${OLD_PER}
  48. spec:
  49. rules:
  50. - host: www.example.com
  51. http:
  52. paths:
  53. # 老版本服務
  54. - path: /
  55. backend:
  56. serviceName: old-nginx
  57. servicePort: 80
  58. # 新版本服務
  59. - path: /
  60. backend:
  61. serviceName: new-nginx
  62. servicePort: 80

此處我們在編排模板中設置了${NEW_PER}和${OLD_PER}兩個環境變量來幫助調節路由權重。 您可通過靈活調整這兩個值來設置藍綠發布的流量占比。

配置CodePipeline項目進行部署:

設置NEW_PER為0,OLD_PER為100進行部署:

執行curl命令,查看路由的訪問情況:

 
  1. #curl -H "Host: www.example.com" http://47.97.238.11
  2. old
  3. #curl -H "Host: www.example.com" http://47.97.238.11
  4. old
  5. #curl -H "Host: www.example.com" http://47.97.238.11
  6. old

設置NEW_PER為50,OLD_PER為50進行部署:

執行curl命令,查看路由的訪問情況:

 
  1. #curl -H "Host: www.example.com" http://47.97.238.11
  2. old
  3. #curl -H "Host: www.example.com" http://47.97.238.11
  4. old
  5. #curl -H "Host: www.example.com" http://47.97.238.11
  6. old
  7. #curl -H "Host: www.example.com" http://47.97.238.11
  8. new
  9. #curl -H "Host: www.example.com" http://47.97.238.11
  10. new
  11. #curl -H "Host: www.example.com" http://47.97.238.11
  12. old

設置NEW_PER為100,OLD_PER為0進行部署:

執行curl命令,查看路由的訪問情況:

 
  1. #curl -H "Host: www.example.com" http://47.97.238.11
  2. new
  3. #curl -H "Host: www.example.com" http://47.97.238.11
  4. new
  5. #curl -H "Host: www.example.com" http://47.97.238.11
  6. new
  7. #curl -H "Host: www.example.com" http://47.97.238.11
  8. new

完成新版本應用測試后,您可將 Ingress 的路由權重設置為 100 將流量完全導向新服務;或者刪除 Ingress 中的注解和舊版本服務,實現藍綠發布。

灰度發布

灰度發布是指在黑與白之間,能夠平滑過渡的一種發布方式。讓一部分用戶繼續用A,一部分用戶開始用B,如果用戶對B沒有什么反對意見,那么逐步擴大范圍,把所有用戶都遷移到B上面來。

假若我們希望請求頭中滿足foo=bar的客戶端請求才能路由到新版本服務(new-nginx)中,那么我們可以如下修改配置ingress規則:

編排模板如下:

 
  1. apiVersion: extensions/v1beta1
  2. kind: Deployment
  3. metadata:
  4. name: new-nginx
  5. spec:
  6. replicas: 1
  7. selector:
  8. matchLabels:
  9. run: new-nginx
  10. template:
  11. metadata:
  12. labels:
  13. run: new-nginx
  14. spec:
  15. containers:
  16. - image: registry.cn-hangzhou.aliyuncs.com/xianlu/new-nginx
  17. imagePullPolicy: Always
  18. name: new-nginx
  19. ports:
  20. - containerPort: 80
  21. protocol: TCP
  22. restartPolicy: Always
  23. ---
  24. apiVersion: v1
  25. kind: Service
  26. metadata:
  27. name: new-nginx
  28. spec:
  29. ports:
  30. - port: 80
  31. protocol: TCP
  32. targetPort: 80
  33. selector:
  34. run: new-nginx
  35. sessionAffinity: None
  36. type: NodePort
  37. ---
  38. apiVersion: extensions/v1beta1
  39. kind: Ingress
  40. metadata:
  41. name: gray-release
  42. annotations:
  43. # 請求頭中滿足正則匹配foo=bar的請求才會被路由到新版本服務new-nginx中
  44. nginx.ingress.kubernetes.io/service-match: |
  45. new-nginx: header("foo", /^bar$/)
  46. spec:
  47. rules:
  48. - host: www.example.com
  49. http:
  50. paths:
  51. # 老版本服務
  52. - path: /
  53. backend:
  54. serviceName: old-nginx
  55. servicePort: 80
  56. # 新版本服務
  57. - path: /
  58. backend:
  59. serviceName: new-nginx
  60. servicePort: 80

配置CodePipeline項目進行部署:

執行curl命令,查看路由的訪問情況:

 
  1. #curl -H "Host: www.example.com" http://47.97.238.11
  2. old
  3. #curl -H "Host: www.example.com" http://47.97.238.11
  4. old
  5. #curl -H "Host: www.example.com" http://47.97.238.11
  6. old
  7. #curl -H "Host: www.example.com" -H "foo: bar" http://47.97.238.11
  8. new
  9. #curl -H "Host: www.example.com" -H "foo: bar" http://47.97.238.11
  10. new
  11. #curl -H "Host: www.example.com" -H "foo: bar" http://47.97.238.11
  12. new

 

來源:https://help.aliyun.com/knowledge_detail/85948.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM