Home > Article > Backend Development > nginx application: canary publishing using nginx
This article mainly introduces the nginx application: using nginx for canary publishing has a certain reference value. Now I share it with you. Friends in need can refer to it.
This article introduces it Blue-green deployment and how to use nginx to simulate canary release in the simplest way
The focus of canary release is: trial and error. The origin of the canary release 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. A small cost is used to exchange for overall security. In the practice of continuous deployment, the canary is traffic control. A very small amount of traffic, such as one percent or one tenth, is used to verify whether a certain version is normal. If it is abnormal, its function will be achieved at the lowest cost and the risk will be reduced. If it is normal, you can gradually increase the weight until it reaches 100%, and switch all traffic to the new version smoothly. Grayscale publishing is generally a similar concept. Gray is a transition between black and white. It is different from blue and green deployment, which is either blue or green. Grayscale release/canary release has a time period when both exist at the same time, but the corresponding traffic of the two is different. If canary release is different from grayscale release, the difference should be the purpose. The purpose of canary release is trial and error, while grayscale release is about stable release, while there is no problem in canary release. It is a smooth transition under the circumstances of grayscale publishing.
Next, we use nginx’s upstream to simply simulate the canary release scenario. The specific scenario is as follows. The main version is currently active. By adjusting the nginx settings and constantly adjusting the weight of the canary version, a smooth release is finally achieved.
Version | Description |
---|---|
User passed http://localhost:8090 Visit the microservice under this kind of deployment | |
The currently active main version provides services on port 7001. The prompt information is "Hello main service: v1 in 7001" | |
The trial and error canary version provides services on port 7002, and the prompt message is "Hello canary deploy service: v2 in 7002” |
docker run -d -p 7001:8080 liumiaocn/tornado:latest python /usr/local/bin/daemon.py "Hello main service: v1 in 7001"docker run -d -p 7002: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 7001:8080 liumiaocn/tornado:latest python /usr/local/bin/daemon.py "Hello main service: v1 in 7001"28f42bbd21146c520b05ff2226514e62445b4cdd5d82f372b3791fdd47cd602a [root@kong ~]# docker run -d -p 7002:8080 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 ~]#
[root@kong ~]# docker run -p 9080: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:9080->80/tcp nginx-canary [root@kong ~]#
http { upstream nginx_canary { server 192.168.163.117:7001 weight=100; server 192.168.163.117:7002 down; }server { listen 80; server_name www.liumiao.cn 192.168.163.117; location / { proxy_pass http://nginx_canary; } }How to modify default.confYou can achieve the effect by installing vim in the container, you can also modify it locally and pass it in through docker cp, or modify it directly with sed. If you install vim in a container, use the following method
[root@kong ~]# docker exec -it nginx-lb sh# apt-get update...省略# apt-get install vim...省略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:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1: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; #}}#
# cat default.confupstream nginx_canary { server 192.168.163.117:7001 weight=100; server 192.168.163.117:7002 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 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:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1: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; #}}#
# nginx -s reload2018/05/28 05:16:20 [notice] 319#319: signal process started#
[root@kong ~]# cnt=0; while [ $cnt -lt 10 ]; do curl http://localhost:9080; let cnt++; doneHello, Service :Hello main service: v1 in 7001Hello, Service :Hello main service: v1 in 7001Hello, Service :Hello main service: v1 in 7001Hello, Service :Hello main service: v1 in 7001Hello, Service :Hello main service: v1 in 7001Hello, Service :Hello main service: v1 in 7001Hello, Service :Hello main service: v1 in 7001Hello, Service :Hello main service: v1 in 7001Hello, Service :Hello main service: v1 in 7001Hello, Service :Hello main service: v1 in 7001[root@kong ~]#Canary release: Canary version traffic weight 10%By adjusting default.conf weight, and then execute nginx -s reload to adjust the weight of the canary version to 10%, and 10% of the traffic will execute the new serviceMethod to modify default.confOnly The weight of the server in upstream needs to be adjusted as follows:
upstream nginx_canary { server 192.168.163.117:7001 weight=10; server 192.168.163.117:7002 weight=90; }Reload nginx settings
# nginx -s reload2018/05/28 05:20:14 [notice] 330#330: signal process started#
[root@kong ~]# cnt=0; while [ $cnt -lt 10 ]; do curl http://localhost:9080; let cnt++; doneHello, Service :Hello canary deploy service: v2 in 7002Hello, Service :Hello canary deploy service: v2 in 7002Hello, Service :Hello canary deploy service: v2 in 7002Hello, Service :Hello canary deploy service: v2 in 7002Hello, Service :Hello main service: v1 in 7001Hello, Service :Hello canary deploy service: v2 in 7002Hello, Service :Hello canary deploy service: v2 in 7002Hello, Service :Hello canary deploy service: v2 in 7002Hello, Service :Hello canary deploy service: v2 in 7002Hello, Service :Hello canary deploy service: v2 in 7002[root@kong ~]#
upstream nginx_canary { server 192.168.163.117:7001 weight=50; server 192.168.163.117:7002 weight=50; }Reload nginx settings
# nginx -s reload2018/05/28 05:22:26 [notice] 339#339: signal process started#
[root@kong ~]# cnt=0; while [ $cnt -lt 10 ]; do curl http://localhost:9080; let cnt++; doneHello, Service :Hello main service: v1 in 7001Hello, Service :Hello canary deploy service: v2 in 7002Hello, Service :Hello main service: v1 in 7001Hello, Service :Hello canary deploy service: v2 in 7002Hello, Service :Hello main service: v1 in 7001Hello, Service :Hello canary deploy service: v2 in 7002Hello, Service :Hello main service: v1 in 7001Hello, Service :Hello canary deploy service: v2 in 7002Hello, Service :Hello main service: v1 in 7001Hello, Service :Hello canary deploy service: v2 in 7002[root@kong ~]#
upstream nginx_canary { server 192.168.163.117:7001 weight=10; server 192.168.163.117:7002 weight=90; }Re- Load nginx settings
# nginx -s reload2018/05/28 05:24:29 [notice] 346#346: signal process started#
[root@kong ~]# cnt=0; while [ $cnt -lt 10 ]; do curl http://localhost:9080; let cnt++; doneHello, Service :Hello canary deploy service: v2 in 7002Hello, Service :Hello canary deploy service: v2 in 7002Hello, Service :Hello canary deploy service: v2 in 7002Hello, Service :Hello canary deploy service: v2 in 7002Hello, Service :Hello main service: v1 in 7001Hello, Service :Hello canary deploy service: v2 in 7002Hello, Service :Hello canary deploy service: v2 in 7002Hello, Service :Hello canary deploy service: v2 in 7002Hello, Service :Hello canary deploy service: v2 in 7002Hello, Service :Hello canary deploy service: v2 in 7002[root@kong ~]#
<p style="margin-bottom: 7px; margin-top: 14px;">upstream nginx_canary { server 192.168.163.117:7001 down; server 192.168.163.117:7002 weight=100;<br/>}<br/></p>Reload nginx settings
# nginx -s reload2018/05/28 05:26:37 [notice] 353#353: signal process started#
[root@kong ~]# cnt=0; while [ $cnt -lt 10 ]; do curl http://localhost:9080; let cnt++; doneHello, Service :Hello canary deploy service: v2 in 7002Hello, Service :Hello canary deploy service: v2 in 7002Hello, Service :Hello canary deploy service: v2 in 7002Hello, Service :Hello canary deploy service: v2 in 7002Hello, Service :Hello canary deploy service: v2 in 7002Hello, Service :Hello canary deploy service: v2 in 7002Hello, Service :Hello canary deploy service: v2 in 7002Hello, Service :Hello canary deploy service: v2 in 7002Hello, Service :Hello canary deploy service: v2 in 7002Hello, Service :Hello canary deploy service: v2 in 7002[root@kong ~]#
nginx application: Using nginx for load balancing
The above is the detailed content of nginx application: canary publishing using nginx. For more information, please follow other related articles on the PHP Chinese website!