搜尋
首頁運維Nginx如何使用nginx模擬進行藍綠部署

藍綠部署

藍綠部署的重點在於以下特點

  • 1. 藍色版本和綠色版本同時存在

  • 2. 實際運作的環境為藍或則綠,只能為其中之一,透過開關控制

##優點和缺點分析:優點在於它的速度和回滾。而缺點也顯而易見。可以快速回滾是因為有兩套環境同時存在的緣故,所以複雜度和需要的資源會增多,因為其有兩套環境。

另外雖然速度有所提高,但是在實現的過程中,開關的控制,無論多快的切換速度,如果不結合其他的技術,還是無法做到完全無縫切換。

模擬藍綠部署

接下來我們使用nginx的upstream來簡單模擬藍綠部署的場景。具體場景如下, 目前活躍的是藍色版本,透過調整nginx設定,將綠色版本設定為當前活躍版本。

如何使用nginx模擬進行藍綠部署

事前準備

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

docker run -d -p 7001:8080 liumiaocn/tornado:latest python /usr/local/bin/daemon.py "hello blue/green service: v1 in 7001"
docker run -d -p 7002:8080 liumiaocn/tornado:latest python /usr/local/bin/daemon.py "hello blue/green service: v2 in 7002"

執行日誌

[root@kong ~]# docker run -d -p 7001:8080 liumiaocn/tornado:latest python /usr/local/bin/daemon.py "hello blue/green service: v1 in 7001"
70c74dc8e43d5635983f7240deb63a3fc0599d5474454c3bc5197aa5c0017348
[root@kong ~]# docker run -d -p 7002:8080 liumiaocn/tornado:latest python /usr/local/bin/daemon.py "hello blue/green service: v2 in 7002"
6c5c2ea322d4ac17b90feefb96e3194ec8adecedaa4c944419316a2e4bf07117
[root@kong ~]# curl http://192.168.163.117:7001
hello, service :hello blue/green service: v1 in 7001
[root@kong ~]# curl http://192.168.163.117:7002
hello, service :hello blue/green service: v2 in 7002
[root@kong ~]#

啟動nginx

[root@kong ~]# docker run -p 9080:80 --name nginx-blue-green -d nginx
d3b7098c44890c15918dc47616b67e5e0eb0da7a443eac266dbf26d55049216a
[root@kong ~]# docker ps |grep nginx-blue-green
d3b7098c4489    nginx           "nginx -g 'daemon ..."  10 seconds ago    up 9 seconds    0.0.0.0:9080->80/tcp   nginx-blue-green
[root@kong ~]#

nginx程式碼段

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

http {
upstream nginx_blug_green {
  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_blug_green;
  }
}

修改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_blug_green {
  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_blug_green;
  }
  #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 04:39:47 [notice] 321#321: signal process started
#

確認結果

#10次呼叫全部輸出的都是v1 in 7001

#[root@kong ~]# cnt=0; while [ $cnt -lt 10 ]> do> curl

> let cnt
> done

hello, service :hello blue/green service: v1 in 7001
hello, service :hello blue/green service: v1 in 7001
hello, service :hello blue/green service: v1 in 7001
hello, service :#hello, service : hello blue/green service: v1 in 7001
hello, service :hello blue/green service: v1 in 7001
hello, service :hello blue/green service: v1 in 7001
hello, service :hello blue /green service: v1 in 7001
hello, service :hello blue/green service: v1 in 7001
hello, service :hello blue/green service: v1 in 7001
hello, service :hello blue/green service: v1 in 7001
[root@kong ~]

#####藍綠部署:切換到綠色版本#########透過調整default.conf的weight,然後執行nginx -s reload的方式,在不停止nginx服務的方式下可動態的切換到綠色版本,目標將會將全部的流量都輸出v2 in 7002##########修改default .conf的方法#########只需要將upstream中的server的權重做如下調整:###
upstream nginx_blug_green {
  server 192.168.163.117:7001 down;
  server 192.168.163.117:7002 weight=100;
}
#######重新載入nginx設定######
# nginx -s reload
2018/05/28 05:01:28 [notice] 330#330: signal process started
#
## ####確認結果############[root@kong ~]# cnt=0; while [ $cnt -lt 10 ]; do curl ; let cnt ; done###hello, service :hello blue/green service: v2 in 7002###hello, service :hello blue/green service: v2 in 7002###hello, service :hello blue/green service: v2 in 7002###hello, service :#hello, service : hello blue/green service: v2 in 7002###hello, service :hello blue/green service: v2 in 7002###hello, service :hello blue/green service: v2 in 7002###hello, service :hello blue /green service: v2 in 7002###hello, service :hello blue/green service: v2 in 7002###hello, service :hello blue/green service: v2 in 7002###hello, service :hello blue/green service: v2 in 7002###[root@kong ~]#######

以上是如何使用nginx模擬進行藍綠部署的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文轉載於:亿速云。如有侵權,請聯絡admin@php.cn刪除
在Nginx和Apache之間進行選擇:適合您的需求在Nginx和Apache之間進行選擇:適合您的需求Apr 15, 2025 am 12:04 AM

NGINX和Apache各有優劣,適合不同場景。 1.NGINX適合高並發和低資源消耗場景。 2.Apache適合需要復雜配置和豐富模塊的場景。通過比較它們的核心特性、性能差異和最佳實踐,可以幫助你選擇最適合需求的服務器軟件。

nginx怎麼啟動nginx怎麼啟動Apr 14, 2025 pm 01:06 PM

問題:如何啟動 Nginx?答案:安裝 Nginx啟動 Nginx驗證 Nginx 是否已啟動探索其他啟動選項自動啟動 Nginx

怎麼查看nginx是否啟動怎麼查看nginx是否啟動Apr 14, 2025 pm 01:03 PM

確認 Nginx 是否啟動的方法:1. 使用命令行:systemctl status nginx(Linux/Unix)、netstat -ano | findstr 80(Windows);2. 檢查端口 80 是否開放;3. 查看系統日誌中 Nginx 啟動消息;4. 使用第三方工具,如 Nagios、Zabbix、Icinga。

nginx怎麼關閉nginx怎麼關閉Apr 14, 2025 pm 01:00 PM

要關閉 Nginx 服務,請按以下步驟操作:確定安裝類型:Red Hat/CentOS(systemctl status nginx)或 Debian/Ubuntu(service nginx status)停止服務:Red Hat/CentOS(systemctl stop nginx)或 Debian/Ubuntu(service nginx stop)禁用自動啟動(可選):Red Hat/CentOS(systemctl disable nginx)或 Debian/Ubuntu(syst

nginx在windows中怎麼配置nginx在windows中怎麼配置Apr 14, 2025 pm 12:57 PM

如何在 Windows 中配置 Nginx?安裝 Nginx 並創建虛擬主機配置。修改主配置文件並包含虛擬主機配置。啟動或重新加載 Nginx。測試配置並查看網站。選擇性啟用 SSL 並配置 SSL 證書。選擇性設置防火牆允許 80 和 443 端口流量。

nginx403錯誤怎麼解決nginx403錯誤怎麼解決Apr 14, 2025 pm 12:54 PM

服務器無權訪問所請求的資源,導致 nginx 403 錯誤。解決方法包括:檢查文件權限。檢查 .htaccess 配置。檢查 nginx 配置。配置 SELinux 權限。檢查防火牆規則。排除其他原因,如瀏覽器問題、服務器故障或其他可能的錯誤。

linux怎麼啟動nginxlinux怎麼啟動nginxApr 14, 2025 pm 12:51 PM

在 Linux 中啟動 Nginx 的步驟:檢查 Nginx 是否已安裝。使用 systemctl start nginx 啟動 Nginx 服務。使用 systemctl enable nginx 啟用在系統啟動時自動啟動 Nginx。使用 systemctl status nginx 驗證啟動是否成功。在 Web 瀏覽器中訪問 http://localhost 查看默認歡迎頁面。

linux怎麼查看nginx是否啟動linux怎麼查看nginx是否啟動Apr 14, 2025 pm 12:48 PM

在 Linux 中,使用以下命令檢查 Nginx 是否已啟動:systemctl status nginx根據命令輸出進行判斷:如果顯示 "Active: active (running)",則 Nginx 已啟動。如果顯示 "Active: inactive (dead)",則 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.能量晶體解釋及其做什麼(黃色晶體)
4 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
4 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
4 週前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解鎖Myrise中的所有內容
1 個月前By尊渡假赌尊渡假赌尊渡假赌

熱工具

Atom編輯器mac版下載

Atom編輯器mac版下載

最受歡迎的的開源編輯器

記事本++7.3.1

記事本++7.3.1

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

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

強大的PHP整合開發環境

VSCode Windows 64位元 下載

VSCode Windows 64位元 下載

微軟推出的免費、功能強大的一款IDE編輯器

WebStorm Mac版

WebStorm Mac版

好用的JavaScript開發工具