主頁 > 知識庫 > 使用nginx模擬進(jìn)行金絲雀發(fā)布的方式

使用nginx模擬進(jìn)行金絲雀發(fā)布的方式

熱門標(biāo)簽:廈門營銷外呼系統(tǒng)平臺 涪陵商都400電話開通辦理 外呼系統(tǒng)的經(jīng)營范圍 外呼系統(tǒng)不彈窗 申請400電話價格多少 云會外呼系統(tǒng) 智能電話機(jī)器人坐席 安陽ai電銷機(jī)器人軟件 柳州市機(jī)器人外呼系統(tǒng)報價

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

金絲雀發(fā)布/灰度發(fā)布

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

模擬金絲雀發(fā)布

接下來我們使用nginx的upstream來簡單模擬一下金絲雀發(fā)布的場景。具體場景如下, 當(dāng)前活躍的是主版本,通過調(diào)整nginx設(shè)定,通過不斷的調(diào)節(jié)金絲雀版本的權(quán)重,最終實(shí)現(xiàn)平穩(wěn)地發(fā)布。

事前準(zhǔn)備

事前在7001/7002兩個端口分別啟動兩個服務(wù),用于顯示不同信息,為了演示方便,使用tornado做了一個鏡像,通過docker容器啟動時傳遞的參數(shù)不同用于顯示服務(wù)的不同。

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"

執(zhí)行日志

[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:7001
Hello, Service :Hello main service: v1 in 7001
[root@kong ~]# curl http://192.168.163.117:7002
Hello, Service :Hello canary deploy service: v2 in 7002
[root@kong ~]#

啟動nginx

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

nginx代碼段

準(zhǔn)備如下nginx代碼段將其添加到nginx的/etc/nginx/conf.d/default.conf中, 模擬方式很簡單,通過down來表示流量為零(nginx中無法將weight設(shè)置為零),開始的時候100%的流量都發(fā)到主版本。

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達(dá)到效果,也可以在本地修改然后通過docker cp傳入,或者直接sed修改都可。如果在容器中安裝vim,使用如下方式即可

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

修改前

# cat default.conf
server {
  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.conf
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;
  #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設(shè)定

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

確認(rèn)結(jié)果

10次調(diào)用全部輸出的都是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 ~]#

金絲雀發(fā)布: 金絲雀版本流量權(quán)重10%

通過調(diào)整default.conf的weight,然后執(zhí)行nginx -s reload的方式,調(diào)節(jié)金絲雀版本的權(quán)重為10%,流量的10%會執(zhí)行新的服務(wù)

修改default.conf的方法

只需要將upstream中的server的權(quán)重做如下調(diào)整:

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

重新加載nginx設(shè)定

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

確認(rèn)結(jié)果

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

金絲雀發(fā)布: 金絲雀版本流量權(quán)重50%

通過調(diào)整default.conf的weight,然后執(zhí)行nginx -s reload的方式,調(diào)節(jié)金絲雀版本的權(quán)重為50%,流量的50%會執(zhí)行新的服務(wù)

修改default.conf的方法

只需要將upstream中的server的權(quán)重做如下調(diào)整:

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

重新加載nginx設(shè)定

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

確認(rèn)結(jié)果

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

金絲雀發(fā)布: 金絲雀版本流量權(quán)重90%

通過調(diào)整default.conf的weight,然后執(zhí)行nginx -s reload的方式,調(diào)節(jié)金絲雀版本的權(quán)重為90%,流量的90%會執(zhí)行新的服務(wù)

修改default.conf的方法

只需要將upstream中的server的權(quán)重做如下調(diào)整:

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

重新加載nginx設(shè)定

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

確認(rèn)結(jié)果

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

金絲雀發(fā)布: 金絲雀版本流量權(quán)重100%

通過調(diào)整default.conf的weight,然后執(zhí)行nginx -s reload的方式,調(diào)節(jié)金絲雀版本的權(quán)重為100%,流量的100%會執(zhí)行新的服務(wù)

修改default.conf的方法

只需要將upstream中的server的權(quán)重做如下調(diào)整:

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

重新加載nginx設(shè)定

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

確認(rèn)結(jié)果

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

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接

標(biāo)簽:綏化 撫順 巴中 蕪湖 福州 孝感 南充 晉城

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《使用nginx模擬進(jìn)行金絲雀發(fā)布的方式》,本文關(guān)鍵詞  使用,nginx,模擬,進(jìn)行,金絲雀,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《使用nginx模擬進(jìn)行金絲雀發(fā)布的方式》相關(guān)的同類信息!
  • 本頁收集關(guān)于使用nginx模擬進(jìn)行金絲雀發(fā)布的方式的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章