首頁 運維 Nginx 怎麼使用nginx模擬進行金絲雀發布

怎麼使用nginx模擬進行金絲雀發布

May 15, 2023 am 11:25 AM
nginx

金絲雀發布/灰階發布

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

模擬金絲雀發布

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

怎麼使用nginx模擬進行金絲雀發布

事前準備

事前在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: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程式碼段

#準備如下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.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設定

# nginx -s reload
2018/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 ; 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
hello, 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 reload
2018/05/28 05:20:14 [notice] 330#330: signal process started
#
登入後複製

###確認結果############[root@kong ~]# cnt=0; while [ $cnt -lt 10 ]; do curl ; 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 70 ###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 service##hello, 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 reload
2018/05/28 05:22:26 [notice] 339#339: signal process started
#
登入後複製

确认结果

[root@kong ~]# cnt=0; while [ $cnt -lt 10 ]; do curl ; 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 ~]#

金丝雀发布: 金丝雀版本流量权重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 reload
2018/05/28 05:24:29 [notice] 346#346: signal process started
#
登入後複製

确认结果

[root@kong ~]# cnt=0; while [ $cnt -lt 10 ]; do curl ; 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 ~]#

金丝雀发布: 金丝雀版本流量权重100%

通过调整default.conf的weight,然后执行nginx -s reload的方式,调节金丝雀版本的权重为100%,流量的100%会执行新的服务

修改default.conf的方法

只需要将upstream中的server的权重做如下调整:

upstream nginx_canary {
  server 192.168.163.117:7001 down;
  server 192.168.163.117:7002 weight=100;
}
登入後複製

重新加载nginx设定

# nginx -s reload
2018/05/28 05:26:37 [notice] 353#353: signal process started
登入後複製

确认结果

[root@kong ~]# cnt=0; while [ $cnt -lt 10 ]; do curl ; 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 ~]#

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

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

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解鎖Myrise中的所有內容
1 個月前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

nginx怎麼查看運行狀態 nginx怎麼查看運行狀態 Apr 14, 2025 am 11:48 AM

查看 Nginx 運行狀態的方法有:使用 ps 命令查看進程狀態;查看 Nginx 配置文件 /etc/nginx/nginx.conf;使用 Nginx 狀態模塊啟用狀態端點;使用 Prometheus、Zabbix 或 Nagios 等監控工具。

如何查看nginx版本信息 如何查看nginx版本信息 Apr 14, 2025 am 08:24 AM

通過以下方法查看 Nginx 版本信息:直接命令法:“nginx -v” 輸出版本信息。配置文件中查看:在配置文件頂部找到“version”部分。系統信息命令:Linux:使用“rpm -qa | grep nginx”或“dpkg -l | grep nginx”命令。 FreeBSD:使用“pkg info nginx”命令。 Windows:打開 Nginx 服務屬性,版本信息位於“常規”選項卡。

nginx如何配置負載均衡 nginx如何配置負載均衡 Apr 14, 2025 am 08:33 AM

如何配置 Nginx 進行負載均衡?定義上游服務器池,指定服務器 IP 和端口。定義虛擬主機,監聽連接並轉發到上游池。指定位置,匹配請求並轉發到上游池。

nginx中如何重定向 nginx中如何重定向 Apr 14, 2025 am 08:42 AM

通過 Nginx 進行重定向的方法有 301 永久重定向(更新鏈接或移動頁面)和 302 臨時重定向(處理錯誤或臨時更改)。配置重定向涉及在 server 塊中使用 location 指令,高級功能包括正則表達式匹配、代理重定向和基於條件的重定向。重定向的常見用途包括更新 URL、處理錯誤、將 HTTP 重定向到 HTTPS,以及引導用戶訪問特定國家或語言版本。

nginx怎麼啟用stream nginx怎麼啟用stream Apr 14, 2025 am 09:45 AM

如何啟用 Nginx 的 Stream 模塊?啟用 Stream 模塊需要六個步驟:安裝 Stream 模塊配置 Nginx創建 Stream 服務器塊配置 Stream 服務器選項重啟 Nginx驗證啟用

nginx403怎麼解決 nginx403怎麼解決 Apr 14, 2025 am 10:33 AM

如何解決 Nginx 403 Forbidden 錯誤?檢查文件或目錄權限;2. 檢查 .htaccess 文件;3. 檢查 Nginx 配置文件;4. 重啟 Nginx。其他可能原因還包括防火牆規則、SELinux 設置或應用程序問題。

nginx怎麼搭建網站 nginx怎麼搭建網站 Apr 14, 2025 am 11:21 AM

使用 Nginx 搭建網站分五步進行:一、安裝 Nginx;二、配置 Nginx,主要配置監聽端口、網站根目錄、索引文件和錯誤頁面;三、創建網站文件;四、測試 Nginx;五、可根據需要進行進階配置,如 SSL 加密、反向代理、負載均衡和緩存。

怎麼把nginx訪問地址設置成服務器ip 怎麼把nginx訪問地址設置成服務器ip Apr 14, 2025 am 11:36 AM

要在 Nginx 中將訪問地址設置為服務器 IP,請:配置服務器塊,設置監聽地址(如:listen 192.168.1.10:80)設置服務器名稱(如:server_name example.com www.example.com),或將其留空以訪問服務器 IP保存並重新加載 Nginx 以應用更改

See all articles