Ali cloud - gray publish / release blue and green

Blue-green release

Blue-green deployments are kept the old version, the new version to deploy and then test to confirm flow will gradually cut OK to the new version. Blue, green and deployment without downtime and less risk.

Examples

This embodiment is an application nginx, comprising a deployment, service and ingress. deployment of foreign exposure by NodePort port, and there is a ingress of external services. Layout template below.

Example Project Address: https://code.aliyun.com/CodePipeline/kubernetes-deploy-demo.git

Application of a deployed version (old-nginx) Step 1

Layout template as follows:

 
  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

Configuration CodePipeline project deployment:

Log Master node, Ingress query access address

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

Execute curl command to check the routing of access:

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

Step two to create a new version of the application (new-nginx)

Layout template as follows:

 
  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

Here we set the $ {NEW_PER} and $ {OLD_PER} two environment variables in layout templates to help regulate the routing weight. You can set the proportion of blue-green traffic issued by the flexibility to adjust these two values.

Configuration CodePipeline project deployment:

NEW_PER set to 0, OLD_PER 100 is deployed:

Execute curl command to check the routing of access:

 
  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

Set NEW_PER to 50, OLD_PER 50 for deployment:

Execute curl command to check the routing of access:

 
  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 set to 100, OLD_PER deployed to 0:

Execute curl command to check the routing of access:

 
  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

After the completion of the new test version of the application, you can Ingress routing weight to 100 will flow entirely new service-oriented; or delete Ingress in annotations and the old version of the service, to achieve blue-green publishing.

Gray release

It refers to a gradation release release manner between black and white, can be a smooth transition. Let some users continue to use the A, B began to use part of the user, if the user does not have any objections to B, then gradually expand the scope, all users are migrated to the B to the top.

If we want to meet the request header foo = bar client requests can be routed to the new version of the service (new-nginx), then we can modify the configuration ingress rule as follows:

Layout template as follows:

 
  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

Configuration CodePipeline project deployment:

Execute curl command to check the routing of access:

 
  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

 

Source: https: //help.aliyun.com/knowledge_detail/85948.html

Guess you like

Origin www.cnblogs.com/gao88/p/11276760.html