這篇文章主要介紹了關於nginx應用:使用nginx進行藍綠部署,有著一定的參考價值,現在分享給大家,有需要的朋友可以參考一下
這篇文章介紹一下藍綠部署以及使用nginx如何最簡單地模擬一下藍綠部署的方式
#藍綠部署的重點在於以下特點
1. 藍色版本和綠色版本同時存在
2. 實際運作的環境為藍或則綠,只能為其中之一,透過開關控制
优点和缺点分析:优点在于它的速度和回滚。而缺点也显而易见。可以快速回滚是因为有两套环境同时存在的缘故,所以复杂度和需要的资源会增多,因为其有两套环境。 另外虽然速度有所提高,但是在实现的过程中,开关的控制,无论多快的切换速度,如果不结合其他的技术,还是无法做到完全无缝切换。
接下來我們使用nginx的upstream來簡單模擬一下藍綠部署的場景。具體場景如下, 目前活躍的是藍色版本,透過調整nginx設定,將綠色版本設定為當前活躍版本。
版本 | 說明 |
---|---|
#router | 使用者通過http://localhost:8090來訪此種部署下的微服務 |
藍色版本 | 目前活躍的為藍色版本,在7001埠提供服務,提示訊息為“Hello blue/green service: v1 in 7001” |
綠色版本 | 即將發布的綠色版本,在7002連接埠提供服務,提示訊息為“ Hello blue/green service: v2 in 7002” |
事前在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:7001Hello, Service :Hello blue/green service: v1 in 7001[root@kong ~]# curl http://192.168.163.117:7002Hello, Service :Hello blue/green service: v2 in 7002[root@kong ~]#
[root@kong ~]# docker run -p 9080:80 --name nginx-blue-green -d nginxd3b7098c44890c15918dc47616b67e5e0eb0da7a443eac266dbf26d55049216a [root@kong ~]# docker ps |grep nginx-blue-greend3b7098c4489 nginx "nginx -g 'daemon ..." 10 seconds ago Up 9 seconds 0.0.0.0:9080->80/tcp nginx-blue-green [root@kong ~]#
準備如下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; } }
可以透過在容器中安裝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_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 -s reload2018/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 http://localhost:9080> let cnt++ > done Hello, Service :Hello blue/green service: v1 in 7001Hello, Service :Hello blue/green service: v1 in 7001Hello, Service :Hello blue/green service: v1 in 7001Hello, Service :Hello blue/green service: v1 in 7001Hello, Service :Hello blue/green service: v1 in 7001Hello, Service :Hello blue/green service: v1 in 7001Hello, Service :Hello blue/green service: v1 in 7001Hello, Service :Hello blue/green service: v1 in 7001Hello, Service :Hello blue/green service: v1 in 7001Hello, Service :Hello blue/green service: v1 in 7001[root@kong ~]#
透過調整default.conf的weight,然後執行nginx -s reload的方式,在不停止nginx服務的方式下可動態的切換到綠色版本,目標將會將全部的流量都輸出v2 in 7002
只需要將upstream中的server的權重做如下調整:
upstream nginx_blug_green { server 192.168.163.117:7001 down; server 192.168.163.117:7002 weight=100; }
# nginx -s reload2018/05/28 05:01:28 [notice] 330#330: signal process started#
[root@kong ~]# cnt=0; while [ $cnt -lt 10 ]; do curl http://localhost:9080; let cnt++; doneHello, Service :Hello blue/green service: v2 in 7002Hello, Service :Hello blue/green service: v2 in 7002Hello, Service :Hello blue/green service: v2 in 7002Hello, Service :Hello blue/green service: v2 in 7002Hello, Service :Hello blue/green service: v2 in 7002Hello, Service :Hello blue/green service: v2 in 7002Hello, Service :Hello blue/green service: v2 in 7002Hello, Service :Hello blue/green service: v2 in 7002Hello, Service :Hello blue/green service: v2 in 7002Hello, Service :Hello blue/green service: v2 in 7002[root@kong ~]#
相關推薦:
#以上是nginx應用:使用nginx進行藍綠部署的詳細內容。更多資訊請關注PHP中文網其他相關文章!