Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use nginx Simulation for Canary release

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces how to use nginx simulation for canary release, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, let the editor take you to know about it.

Canary release / grayscale release

The focus of the canary release is: trial and error. The origin of the release of the canary itself is a tragic story of the beautiful creatures of nature in the development of human industry. The canary uses its life to try and make mistakes for the safety of the miners. In the practice of continuous deployment, canaries are flow control, which is used to verify whether a version is normal with a small amount of traffic, such as 1% or 1/10. If it is abnormal, it uses the lowest cost to achieve its effect and reduces the risk. If normal, you can gradually increase the weight to 100%, switching all traffic smoothly to the new version. Generally speaking, grayscale publishing is a similar concept. Gray is a transition between black and white, which is different from blue-green deployment. Grayscale release / canary press release has a time period in which both exist at the same time, but the corresponding traffic is different. If canary release is different from grayscale release, the difference should be purposeful. Canary release aims at trial and error, while grayscale release lies in smooth release. The smooth transition under the condition that there is no problem with the canary release is the grayscale release.

Simulated canary release

Next, we use nginx's upstream to briefly simulate the canary release scenario. The specific scenarios are as follows: the current active version is the main version, which can be released smoothly by adjusting the nginx settings and constantly adjusting the weight of the canary version.

Prepare beforehand

In advance, start two services on the two ports of 7001 docker 7002 to display different information. For convenience of demonstration, use tornado to make a mirror image. Different parameters passed when starting through the docker container are used to display different services.

Docker run-d-p 7001 in 8080 liumiaocn/tornado:latest python / usr/local/bin/daemon.py "Hello main service: v1 in 7001" docker run-d-p 7002 purl 8080 liumiaocn/tornado:latest python / usr/local/bin/daemon.py "Hello canary deploy service: v2 in 7002"

Execution log

[root@kong] # docker run-d-p 7001docker run 8080 liumiaocn/tornado:latest python / usr/local/bin/daemon.py "Hello main service: v1 in 7001" 28f42bbbd21146c520b05ff2226514e62445b4cdd5d82f3791fdd47cd602a [root @ kong] # docker run-d-p 7002 liumiaocn/tornado:latest python / usr/local/bin/daemon.py "Hello canary deploy service: v2 in 7002" b86c4b83048d782fadc3edbacc19b73af20dc87f5f4cf37cf348d17c45f0215d [root@kong ~] # curl http://192.168.163.117:7001Hello, Service: Hello main service: v1 in 7001 [root@kong ~] # curl http://192.168.163.117:7002Hello, Service: Hello canary deploy service: v2 in 7002 [root@kong ~] #

Start nginx

[root@kong ~] # docker run-p 9080 nginx659f15c4d006df6fcd1fab1efe39e25a85c31f3cab1cda67838ddd282669195c 80-- name nginx-canary-d nginx659f15c4d006df6fcd1fab1efe39e25a85c31f3cab1cda67838ddd282669195c [root@kong ~] # docker ps | grep nginx-canary659f15c4d006 nginx "nginx- g 'daemon..." 7 seconds ago Up 7 seconds 0.0.0.0 name nginx-canary 9080-> 80/tcp nginx-canary [root@kong ~] #

Nginx code snippet

Prepare the following nginx code snippet to add it to the / etc/nginx/conf.d/default.conf of nginx. The simulation method is very simple. Zero traffic is indicated by down (weight cannot be set to zero in nginx). At the beginning, 100% of the traffic is sent to the major version.

Http {upstream nginx_canary {server 192.168.163.117 upstream nginx_canary 7001 weight=100; server 192.168.163.117 upstream nginx_canary 7002 down;} server {listen 80; server_name www.liumiao.cn 192.168.163.117; location / {proxy_pass http://nginx_canary;}}

The method of modifying default.conf

You can achieve this by installing vim in the container, or you can modify it locally and pass it in via docker cp, or you can modify it directly by sed. If you install vim in the container, you can use the following

[root@kong ~] # docker exec-it nginx-lb sh# apt-get update... Omit # apt-get install vim... Omit

Before modification

# cat default.confserver {listen 80; server_name localhost; # charset koi8-r; # access_log / var/log/nginx/host.access.log main; location / {root / usr/share/nginx/html; index index.html index.htm;} # error_page 404 / 404.html; # redirect server error pages to the static page / 50x.html # error_page 500 502 503 504 / 50x.html Location = / 50x.html {root / usr/share/nginx/html;} # proxy the PHP scripts to Apache listening on 127.0.0.1 proxy the PHP scripts to Apache listening on 80 # # location ~. Php$ {# proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1 php$ {# root html; # fastcgi_pass 127.0.0.1 # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME / scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # # location ~ /\ .ht {# deny all; #} #

After modification

# cat default.confupstream nginx_canary {server 192.168.163.117 down; weight=100; server 192.168.163.117 down;} server {listen 80; server_name www.liumiao.cn 192.168.163.117; # charset koi8-r; # access_log / var/log/nginx/host.access.log main; location / {# root / usr/share/nginx/html; # index index.html index.htm; proxy_pass http://nginx_canary; } # error_page 404 / 404.html; # redirect server error pages to the static page / 50x.html # error_page 500502 503504 / 50x.html; location = / 50x.html {root / usr/share/nginx/html;} # proxy the PHP scripts to Apache listening on 127.0.1 location 80 # location ~. Php$ {# proxy_pass HTML #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1 pass the PHP scripts to FastCGI server listening on 9000 # # location ~\. Php$ {# root html; # fastcgi_pass 127.0.0.1 php$ 9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME / scripts$fastcgi_script_name; # include fastcgi_params #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # # location ~ /\ .ht {# deny all; #} #

Reload nginx settin

# nginx-s reload2018/05/28 05:16:20 [notice] 319: signal process started#

Confirm the result

All of the 10 calls output v1 in 7001.

[root@kong ~] # cnt=0; while [$cnt-lt 10]; do curl http://localhost:9080; let cnt++; done

Hello, Service: Hello main service: v1 in 7001

Hello, Service: Hello main service: v1 in 7001

Hello, Service: Hello main service: v1 in 7001

Hello, Service: Hello main service: v1 in 7001

Hello, Service: Hello main service: v1 in 7001

Hello, Service: Hello main service: v1 in 7001

Hello, Service: Hello main service: v1 in 7001

Hello, Service: Hello main service: v1 in 7001

Hello, Service: Hello main service: v1 in 7001

Hello, Service: Hello main service: v1 in 7001

[root@kong ~] #

Canary release: Canary version traffic weight 10%

By adjusting the weight of default.conf, and then executing nginx-s reload, the weight of the canary version is adjusted to 10%, and 10% of the traffic will execute the new service.

The method of modifying default.conf

You only need to adjust the weight of the server in upstream as follows:

Upstream nginx_canary {server 192.168.163.117:7001 weight=10; server 192.168.163.117:7002 weight=90;}

Reload nginx settin

# nginx-s reload2018/05/28 05:20:14 [notice] 330mm 330: signal process started#

Confirm the result

[root@kong ~] # cnt=0; while [$cnt-lt 10]; do curl http://localhost:9080; let cnt++; done

Hello, Service: Hello canary deploy service: v2 in 7002

Hello, Service: Hello canary deploy service: v2 in 7002

Hello, Service: Hello canary deploy service: v2 in 7002

Hello, Service: Hello canary deploy service: v2 in 7002

Hello, Service: Hello main service: v1 in 7001

Hello, Service: Hello canary deploy service: v2 in 7002

Hello, Service: Hello canary deploy service: v2 in 7002

Hello, Service: Hello canary deploy service: v2 in 7002

Hello, Service: Hello canary deploy service: v2 in 7002

Hello, Service: Hello canary deploy service: v2 in 7002

[root@kong ~] #

Canary release: Canary version traffic weight 50%

By adjusting the weight of default.conf and then executing nginx-s reload, the weight of the canary version is adjusted to 50%, and 50% of the traffic will execute the new service.

The method of modifying default.conf

You only need to adjust the weight of the server in upstream as follows:

Upstream nginx_canary {server 192.168.163.117:7001 weight=50; server 192.168.163.117:7002 weight=50;}

Reload nginx settin

# nginx-s reload2018/05/28 05:22:26 [notice] 339: signal process started#

Confirm the result

[root@kong ~] # cnt=0; while [$cnt-lt 10]; do curl http://localhost:9080; let cnt++; done

Hello, Service: Hello main service: v1 in 7001

Hello, Service: Hello canary deploy service: v2 in 7002

Hello, Service: Hello main service: v1 in 7001

Hello, Service: Hello canary deploy service: v2 in 7002

Hello, Service: Hello main service: v1 in 7001

Hello, Service: Hello canary deploy service: v2 in 7002

Hello, Service: Hello main service: v1 in 7001

Hello, Service: Hello canary deploy service: v2 in 7002

Hello, Service: Hello main service: v1 in 7001

Hello, Service: Hello canary deploy service: v2 in 7002

[root@kong ~] #

Canary release: traffic weight of Canary version is 90%

By adjusting the weight of default.conf, and then executing nginx-s reload, the weight of the canary version is adjusted to 90%, and 90% of the traffic will execute the new service.

The method of modifying default.conf

You only need to adjust the weight of the server in upstream as follows:

Upstream nginx_canary {server 192.168.163.117:7001 weight=10; server 192.168.163.117:7002 weight=90;}

Reload nginx settin

# nginx-s reload2018/05/28 05:24:29 [notice] 346 "346: signal process started#

Confirm the result

[root@kong ~] # cnt=0; while [$cnt-lt 10]; do curl http://localhost:9080; let cnt++; done

Hello, Service: Hello canary deploy service: v2 in 7002

Hello, Service: Hello canary deploy service: v2 in 7002

Hello, Service: Hello canary deploy service: v2 in 7002

Hello, Service: Hello canary deploy service: v2 in 7002

Hello, Service: Hello main service: v1 in 7001

Hello, Service: Hello canary deploy service: v2 in 7002

Hello, Service: Hello canary deploy service: v2 in 7002

Hello, Service: Hello canary deploy service: v2 in 7002

Hello, Service: Hello canary deploy service: v2 in 7002

Hello, Service: Hello canary deploy service: v2 in 7002

[root@kong ~] #

Canary release: Canary version traffic weight 100%

By adjusting the weight of default.conf, and then executing nginx-s reload, the weight of the canary version is adjusted to 100%, and 100% of the traffic will execute the new service.

The method of modifying default.conf

You only need to adjust the weight of the server in upstream as follows:

Upstream nginx_canary {server 192.168.163.117:7001 down; server 192.168.163.117:7002 weight=100;}

Reload nginx settin

# nginx-s reload2018/05/28 05:26:37 [notice] 353: signal process started

Confirm the result

[root@kong ~] # cnt=0; while [$cnt-lt 10]; do curl http://localhost:9080; let cnt++; done

Hello, Service: Hello canary deploy service: v2 in 7002

Hello, Service: Hello canary deploy service: v2 in 7002

Hello, Service: Hello canary deploy service: v2 in 7002

Hello, Service: Hello canary deploy service: v2 in 7002

Hello, Service: Hello canary deploy service: v2 in 7002

Hello, Service: Hello canary deploy service: v2 in 7002

Hello, Service: Hello canary deploy service: v2 in 7002

Hello, Service: Hello canary deploy service: v2 in 7002

Hello, Service: Hello canary deploy service: v2 in 7002

Hello, Service: Hello canary deploy service: v2 in 7002

[root@kong ~] #

Thank you for reading this article carefully. I hope the article "how to use nginx to simulate Canary Publishing" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report