搜尋
首頁運維Nginx怎麼使用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刪除
NGINX:現代Web應用程序的多功能工具NGINX:現代Web應用程序的多功能工具Apr 11, 2025 am 12:03 AM

NGINXisessentialformodernwebapplicationsduetoitsrolesasareverseproxy,loadbalancer,andwebserver,offeringhighperformanceandscalability.1)Itactsasareverseproxy,enhancingsecurityandperformancebycachingandloadbalancing.2)NGINXsupportsvariousloadbalancingm

NGINX SSL/TLS配置:使用HTTPS確保您的網站NGINX SSL/TLS配置:使用HTTPS確保您的網站Apr 10, 2025 am 09:38 AM

通過Nginx配置SSL/TLS來確保網站安全,需要以下步驟:1.創建基本配置,指定SSL證書和私鑰;2.優化配置,啟用HTTP/2和OCSPStapling;3.調試常見錯誤,如證書路徑和加密套件問題;4.應用性能優化建議,如使用Let'sEncrypt和會話復用。

NGINX面試問題:ACE您的DevOps/System Admin面試NGINX面試問題:ACE您的DevOps/System Admin面試Apr 09, 2025 am 12:14 AM

Nginx是高性能的HTTP和反向代理服務器,擅長處理高並發連接。 1)基本配置:監聽端口並提供靜態文件服務。 2)高級配置:實現反向代理和負載均衡。 3)調試技巧:檢查錯誤日誌和測試配置文件。 4)性能優化:啟用Gzip壓縮和調整緩存策略。

NGINX緩存技術:改善網站性能NGINX緩存技術:改善網站性能Apr 08, 2025 am 12:18 AM

Nginx缓存可以通过以下步骤显著提升网站性能:1)定义缓存区和设置缓存路径;2)配置缓存有效期;3)根据不同内容设置不同的缓存策略;4)优化缓存存储和负载均衡;5)监控和调试缓存效果。通过这些方法,Nginx缓存能减少后端服务器压力,提升响应速度和用户体验。

帶Docker的NGINX:部署和縮放容器化應用程序帶Docker的NGINX:部署和縮放容器化應用程序Apr 07, 2025 am 12:08 AM

使用DockerCompose可以簡化Nginx的部署和管理,通過DockerSwarm或Kubernetes進行擴展是常見的做法。 1)使用DockerCompose定義和運行Nginx容器,2)通過DockerSwarm或Kubernetes實現集群管理和自動擴展。

高級NGINX配置:掌握服務器塊和反向代理高級NGINX配置:掌握服務器塊和反向代理Apr 06, 2025 am 12:05 AM

Nginx的高級配置可以通過服務器塊和反向代理實現:1.服務器塊允許在一個實例中運行多個網站,每個塊獨立配置。 2.反向代理將請求轉發到後端服務器,實現負載均衡和緩存加速。

NGINX性能調整:針對速度和低潛伏期進行優化NGINX性能調整:針對速度和低潛伏期進行優化Apr 05, 2025 am 12:08 AM

Nginx性能調優可以通過調整worker進程數、連接池大小、啟用Gzip壓縮和HTTP/2協議、使用緩存和負載均衡來實現。 1.調整worker進程數和連接池大小:worker_processesauto;events{worker_connections1024;}。 2.啟用Gzip壓縮和HTTP/2協議:http{gzipon;server{listen443sslhttp2;}}。 3.使用緩存優化:http{proxy_cache_path/path/to/cachelevels=1:2k

NGINX安全性硬化:保護您的Web服務器免受攻擊NGINX安全性硬化:保護您的Web服務器免受攻擊Apr 04, 2025 am 12:06 AM

Nginx安全強化可以通過以下步驟實現:1)確保所有流量通過HTTPS傳輸,2)配置HTTP頭增強通信安全性,3)設置SSL/TLS加密數據傳輸,4)實施訪問控制和速率限制防範惡意流量,5)使用ngx_http_secure_link_module模塊防範SQL注入攻擊,這些措施能有效提升Nginx服務器的安全性。

See all articles

熱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中的所有內容
3 週前By尊渡假赌尊渡假赌尊渡假赌

熱工具

DVWA

DVWA

Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

將Eclipse與SAP NetWeaver應用伺服器整合。

EditPlus 中文破解版

EditPlus 中文破解版

體積小,語法高亮,不支援程式碼提示功能

Dreamweaver Mac版

Dreamweaver Mac版

視覺化網頁開發工具

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境