首頁  >  文章  >  後端開發  >  nginx應用:使用nginx進行金絲雀發布

nginx應用:使用nginx進行金絲雀發布

不言
不言原創
2018-06-05 09:36:261957瀏覽

這篇文章主要介紹了關於nginx應用:使用nginx進行金絲雀發布,有著一定的參考價值,現在分享給大家,有需要的朋友可以參考一下

這篇文章介紹一下藍綠部署以及使用nginx如何最簡單地模擬金絲雀發布的方式

金絲雀發布/灰度發布

金絲雀發布的重點在於:試錯。金絲雀發布的來歷本身就是自然界的美麗生物在人類工業發展過程中的一個悲慘的故事。金絲雀就是用它的生命來為礦工的安全來試錯的。用很小的成本來換取整體的安全,在持續部署的實踐中,金絲雀就是流量控制,用很少的流量比如百分之一或者十分之一用於檢證某個版本是否正常,如果不正常則就用最低的成本實現了其作用,降低了風險。如果正常,則可以逐漸加大權重直至百分之百,將所有的流量都平穩地切換至新的版本。灰階發布,一般來說也是類似的概念。灰色是介於黑和白之前的過渡,區別於藍綠部署的非藍即綠,灰度發布/金絲雀發布會有一個兩者同時存在的時間段,只是兩者對應的流量不同,金絲雀發佈如果說和灰度發布有所不同的話,其不同點應該是目的性的不同,金絲雀發布目的在於試錯,而灰度發佈在於平穩發布,而在金絲雀發布沒有問題的狀況下進行的平穩過渡則正是灰階發布。

模擬金絲雀發布

接下來我們使用nginx的upstream來簡單模擬金絲雀發布的場景。具體場景如下, 當前活躍的是主版本,透過調整nginx設定,透過不斷的調節金絲雀版本的權重,最終實現平穩地發布。

版本 說明
#router 使用者通過http://localhost:8090來訪此種部署下的微服務
主版本 目前活躍的主版本,在7001埠提供服務,提示訊息為“Hello main service: v1 in 7001”
金絲雀版本 試錯的金絲雀版本,在7002埠提供服務,提示訊息為「Hello canary deploy service: v2 in 7002”

#事前準備

事前在7001/7002兩個連接埠分別啟動兩個服務,用於顯示不同訊息,為了示範方便,使用tornado做了一個映像,透過docker容器啟動時傳遞的參數不同用於顯示服務的不同。

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"

執行日誌

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

啟動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 ~]#

nginx程式碼段

準備如下nginx程式碼段將其新增至nginx的/etc/nginx/ conf.d/default.conf中, 模擬方式很簡單,透過down來表示流量為零(nginx中無法將weight設為零),開始的時候100%的流量都會發到主版本。

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

}

修改default.conf的方法

可以透過在容器中安裝vim達到效果,也可以在本機修改然後透過docker cp傳入,或直接sed修改都可以。如果在容器中安裝vim,使用以下方式即可

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

修改前

# 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設定

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

#確認結果

10次呼叫全部輸出的都是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 ~]#

金絲雀發佈: 金絲雀版本流量權重10%

透過調整default.conf的weight,然後執行nginx -s reload的方式,調節金絲雀版本的權重為10%,流量的10%會執行新的服務

修改default.conf的方法

#只需要將upstream中的server的權重做如下調整:

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

重新載入nginx設定

# 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 ~]#

金絲雀發布: 金絲雀版本流量權重50%

透過調整default.conf的weight,然後執行nginx -s reload的方式,調節金絲雀版本的權重為50%,流量的50%會執行新的服務

修改default.conf的方法

只需要將upstream中的server的權重做如下調整:

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

重新載入nginx設定

# 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 ~]#

金絲雀發布: 金絲雀版本流量權重90%

透過調整default.conf的weight,然後執行nginx -s reload的方式,調節金絲雀版本的權重為90 %,流量的90%會執行新的服務

修改default.conf的方法

#只需要將upstream中的server的權重做如下調整:

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

重新載入nginx設定

# 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 ~]#

金絲雀發布: 金絲雀版本流量權重100%

透過調整default.conf的weight,然後執行nginx -s reload的方式,調整金絲雀版本的權重為100%,流量的100%會執行新的服務

修改default.conf的方法

只需要將upstream中的server的權重做如下調整:

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

重新載入nginx設定

# 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應用程式:使用nginx進行負載平衡

以上是nginx應用:使用nginx進行金絲雀發布的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn