Home > Backend Development > PHP Tutorial > nginx application: canary publishing using nginx

nginx application: canary publishing using nginx

不言
Release: 2023-03-29 18:40:02
Original
2000 people have browsed it

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

Canary Release/Grayscale Release

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.

Simulating canary release

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.

##routerUser passed http://localhost:8090 Visit the microservice under this kind of deploymentMain versionThe currently active main version provides services on port 7001. The prompt information is "Hello main service: v1 in 7001"Canary versionThe trial and error canary version provides services on port 7002, and the prompt message is "Hello canary deploy service: v2 in 7002”
Version Description
Preparation in advance

Start two services on the two ports 7001/7002 in advance to display different information , for the convenience of demonstration, I used tornado to make an image, and the different parameters passed when starting the docker container are used to display the differences in services.

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"
Copy after login

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 ~]#
Copy after login

Start nginx

[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 ~]#
Copy after login

nginx code segment

Prepare the following nginx code segment and add it to nginx’s /etc/nginx/ In conf.d/default.conf, the simulation method is very simple. Use down to indicate that the traffic is zero (weight cannot be set to zero in nginx). At the beginning, 100% of the traffic is sent to the main version.

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;
    }

}
Copy after login

How to modify default.conf

You 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...省略
Copy after login

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;
    #}}#
Copy after login

After modification

# 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;
    #}}#
Copy after login

Reload nginx settings

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

Confirm Result

10 calls all output v1 in 7001

[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 ~]#
Copy after login

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 service

Method to modify default.conf

Only 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;
}
Copy after login
Copy after login

Reload nginx settings

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

Confirm the result

[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 ~]#
Copy after login
Copy after login

Canary release: Canary version traffic Weight 50%

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

Methods to modify 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;
}
Copy after login

Reload nginx settings

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

Confirm the result

[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 ~]#
Copy after login

Canary release: Canary version traffic weight 90%

Adjust the weight of the canary version to 90 by adjusting the weight of default.conf and then executing nginx -s reload %, 90% of the traffic will execute new services

How to modify 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;
}
Copy after login
Copy after login

Re- Load nginx settings

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

Confirm the result

[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 ~]#
Copy after login
Copy after login

Canary release: Canary version traffic weight 100%

Adjust the weight of default.conf, and then execute nginx -s reload method, adjust the weight of the canary version to 100%, and 100% of the traffic will execute the new service

How to modify default.conf

Just need to change the upstream The weight of the server is adjusted as follows:

<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>
Copy after login

Reload nginx settings

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

Confirm the result

[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 ~]#
Copy after login
Related recommendations:

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template