您好,登錄后才能下訂單哦!
這篇文章介紹一下藍(lán)綠部署以及使用nginx如何最簡(jiǎn)單地模擬一下藍(lán)綠部署的方式
藍(lán)綠部署
藍(lán)綠部署的重點(diǎn)在于如下特點(diǎn)
優(yōu)點(diǎn)和缺點(diǎn)分析:優(yōu)點(diǎn)在于它的速度和回滾。而缺點(diǎn)也顯而易見(jiàn)??梢钥焖倩貪L是因?yàn)橛袃商篆h(huán)境同時(shí)存在的緣故,所以復(fù)雜度和需要的資源會(huì)增多,因?yàn)槠溆袃商篆h(huán)境。
另外雖然速度有所提高,但是在實(shí)現(xiàn)的過(guò)程中,開(kāi)關(guān)的控制,無(wú)論多快的切換速度,如果不結(jié)合其他的技術(shù),還是無(wú)法做到完全無(wú)縫切換。
模擬藍(lán)綠部署
接下來(lái)我們使用nginx的upstream來(lái)簡(jiǎn)單模擬一下藍(lán)綠部署的場(chǎng)景。具體場(chǎng)景如下, 當(dāng)前活躍的是藍(lán)色版本,通過(guò)調(diào)整nginx設(shè)定,將綠色版本設(shè)定為當(dāng)前活躍版本。
事前準(zhǔn)備
事前在7001/7002兩個(gè)端口分別啟動(dòng)兩個(gè)服務(wù),用于顯示不同信息,為了演示方便,使用tornado做了一個(gè)鏡像,通過(guò)docker容器啟動(dòng)時(shí)傳遞的參數(shù)不同用于顯示服務(wù)的不同。
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"
執(zhí)行日志
[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 ~]#
啟動(dòng)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代碼段
準(zhǔn)備如下nginx代碼段將其添加到nginx的/etc/nginx/conf.d/default.conf中, 模擬方式很簡(jiǎn)單,通過(guò)down來(lái)表示流量為零(nginx中無(wú)法將weight設(shè)置為零),開(kāi)始的時(shí)候100%的流量都發(fā)到藍(lán)色版本。
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的方法
可以通過(guò)在容器中安裝vim達(dá)到效果,也可以在本地修改然后通過(guò)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設(shè)定
# nginx -s reload 2018/05/28 04:39:47 [notice] 321#321: 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 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
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 ~]#
藍(lán)綠部署:切換到綠色版本
通過(guò)調(diào)整default.conf的weight,然后執(zhí)行nginx -s reload的方式,在不停止nginx服務(wù)的方式下可動(dòng)態(tài)的切換到綠色版本,目標(biāo)將會(huì)將全部的流量都輸出v2 in 7002
修改default.conf的方法
只需要將upstream中的server的權(quán)重做如下調(diào)整:
upstream nginx_blug_green { server 192.168.163.117:7001 down; server 192.168.163.117:7002 weight=100; }
重新加載nginx設(shè)定
# nginx -s reload 2018/05/28 05:01:28 [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 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
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 ~]#
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)億速云的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。