阿里云 - 灰度发布/蓝绿发布

蓝绿发布

蓝绿部署是不停老版本,部署新版本然后进行测试,确认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

猜你喜欢

转载自www.cnblogs.com/gao88/p/11276760.html