如何使用nginx模拟进行蓝绿部署(nginx蓝绿部署)

如何使用nginx模拟进行蓝绿部署(nginx蓝绿部署)

浏览次数:
信息来源: 用户投稿
更新日期: 2026-01-01
文章简介

蓝绿部署蓝绿部署的重点在于如下特点1.蓝色版本和绿色版本同时存在2.实际运行的环境为蓝或则绿,只能为其中之一,通过开关控制优点和缺点分析:优点在于它的速度和回滚。而缺点也显而易见。可以快速回滚是

2025阿里云双十一服务器活动

蓝绿部署

蓝绿部署的重点在于如下特点

  • 1.蓝色版本和绿色版本同时存在

  • 2.实际运行的环境为蓝或则绿,只能为其中之一,通过开关控制

  • 优点和缺点分析:优点在于它的速度和回滚。而缺点也显而易见。可以快速回滚是因为有两套环境同时存在的缘故,所以复杂度和需要的资源会增多,因为其有两套环境。另外虽然速度有所提高,但是在实现的过程中,开关的控制,无论多快的切换速度,如果不结合其他的技术,还是无法做到完全无缝切换。

    模拟蓝绿部署

    接下来我们使用nginx的upstream来简单模拟一下蓝绿部署的场景。具体场景如下,当前活跃的是蓝色版本,通过调整nginx设定,将绿色版本设定为当前活跃版本。

    事前准备

    事前在7001/7002两个端口分别启动两个服务,用于显示不同信息,为了演示方便,使用tornado做了一个镜像,通过docker容器启动时传递的参数不同用于显示服务的不同。

    dockerrun-d-p7001:8080liumiaocn/tornado:latestpython/usr/local/bin/daemon.py"helloblue/greenservice:v1in7001"

    dockerrun-d-p7002:8080liumiaocn/tornado:latestpython/usr/local/bin/daemon.py"helloblue/greenservice:v2in7002"

    执行日志

    [root@kong~]#dockerrun-d-p7001:8080liumiaocn/tornado:latestpython/usr/local/bin/daemon.py"helloblue/greenservice:v1in7001"

    70c74dc8e43d5635983f7240deb63a3fc0599d5474454c3bc5197aa5c0017348

    [root@kong~]#dockerrun-d-p7002:8080liumiaocn/tornado:latestpython/usr/local/bin/daemon.py"helloblue/greenservice:v2in7002"

    6c5c2ea322d4ac17b90feefb96e3194ec8adecedaa4c944419316a2e4bf07117

    [root@kong~]#curlhttp://192.168.163.117:7001

    hello,service:helloblue/greenservice:v1in7001

    [root@kong~]#curlhttp://192.168.163.117:7002

    hello,service:helloblue/greenservice:v2in7002

    [root@kong~]dockerrun-p9080:80--namenginx-blue-green-dnginx

    d3b7098c44890c15918dc47616b67e5e0eb0da7a443eac266dbf26d55049216a

    [root@kong~]#dockerps|grepnginx-blue-green

    d3b7098c4489nginx"nginx-g'daemon..."10secondsagoup9seconds0.0.0.0:9080->80/tcpnginx-blue-green

    [root@kong~]#

    nginx代码段

    准备如下nginx代码段将其添加到nginx的/etc/nginx/conf.d/default.conf中,模拟方式很简单,通过down来表示流量为零(nginx中无法将weight设置为零),开始的时候100%的流量都发到蓝色版本。

    upstreamnginx_blug_green{

    server192.168.163.117:7001weight=100;

    server192.168.163.117:7002down;

    server_namewww.liumiao.cn192.168.163.117;

    proxy_passhttp://nginx_blug_green;

    }

    修改default.conf的方法

    可以通过在容器中安装vim达到效果,也可以在本地修改然后通过dockercp传入,或者直接sed修改都可。如果在容器中安装vim,使用如下方式即可

    [root@kong~]#dockerexec-itnginx-lbsh

    ...省略

    修改前

    #access_log/var/log/nginx/host.access.logmain;

    root/usr/share/nginx/html;

    indexindex.htmlindex.htm;

    如何使用nginx模拟进行蓝绿部署,nginx蓝绿部署

    error_page500502503504/50x.html;

    root/usr/share/nginx/html;

    #proxythephpscriptstoapachelisteningon127.0.0.1:80

    #proxy_passhttp://127.0.0.1;

    #passthephpscriptstofastcgiserverlisteningon127.0.0.1:9000

    #fastcgi_pass127.0.0.1:9000;

    #fastcgi_paramscript_filename/scripts$fastcgi_script_name;

    #denyaccessto.htaccessfiles,ifapache'sdocumentroot

    #

    修改后

    upstreamnginx_blug_green{

    server192.168.163.117:7001weight=100;

    server192.168.163.117:7002down;

    server_namewww.liumiao.cn192.168.163.117;

    #access_log/var/log/nginx/host.access.logmain;

    #root/usr/share/nginx/html;

    #indexindex.htmlindex.htm;

    proxy_passhttp://nginx_blug_green;

    #redirectservererrorpagestothestaticpage/50x.html

    error_page500502503504/50x.html;

    root/usr/share/nginx/html;

    #proxythephpscriptstoapachelisteningon127.0.0.1:80

    #proxy_passhttp://127.0.0.1;

    #passthephpscriptstofastcgiserverlisteningon127.0.0.1:9000

    #fastcgi_pass127.0.0.1:9000;

    #fastcgi_paramscript_filename/scripts$fastcgi_script_name;

    #denyaccessto.htaccessfiles,ifapache'sdocumentroot

    #

    重新加载nginx设定

    2018/05/2804:39:47[notice]321#321:signalprocessstarted

    cnt=0;while[$cnt-lt10]>do>curl>letcnt++>donehello,service:helloblue/greenservice:v1in7001hello,service:helloblue/greenservice:v1in7001hello,service:helloblue/greenservice:v1in7001hello,service:helloblue/greenservice:v1in7001hello,service:helloblue/greenservice:v1in7001hello,service:helloblue/greenservice:v1in7001hello,service:helloblue/greenservice:v1in7001hello,service:helloblue/greenservice:v1in7001hello,service:helloblue/greenservice:v1in7001hello,service:helloblue/greenservice:v1in7001[root@kong~]#

    蓝绿部署:切换到绿色版本

    通过调整default.conf的weight,然后执行nginx-sreload的方式,在不停止nginx服务的方式下可动态的切换到绿色版本,目标将会将全部的流量都输出v2in7002

    修改default.conf的方法

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

    upstreamnginx_blug_green{

    server192.168.163.117:7001down;

    server192.168.163.117:7002weight=100;

    }

    重新加载nginx设定

    2018/05/2805:01:28[notice]330#330:signalprocessstarted

    cnt=0;while[$cnt-lt10];docurl;letcnt++;donehello,service:helloblue/greenservice:v2in7002hello,service:helloblue/greenservice:v2in7002hello,service:helloblue/greenservice:v2in7002hello,service:helloblue/greenservice:v2in7002hello,service:helloblue/greenservice:v2in7002hello,service:helloblue/greenservice:v2in7002hello,service:helloblue/greenservice:v2in7002hello,service:helloblue/greenservice:v2in7002hello,service:helloblue/greenservice:v2in7002hello,service:helloblue/greenservice:v2in7002[root@kong~]#

    以上就是如何使用nginx模拟进行蓝绿部署的详细内容,更多请关注主机测评网其它相关文章!

    标签:
    保护您的网站免受黑客攻击的习惯(网站受到黑客攻击)
    « 上一篇
    返回列表
    下一篇 »

    如本文对您有帮助,就请抽根烟吧!