0

0

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

WBOY

WBOY

发布时间:2023-06-03 12:31:03

|

990人浏览过

|

来源于亿速云

转载

蓝绿部署

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

  • 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,使用如下方式即可

Outwrite
Outwrite

AI写作浏览器插件,将您的想法变成有力的句子

下载
[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 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 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 重启
nginx 重启

nginx重启对于网站的运维来说是非常重要的,根据不同的需求,可以选择简单重启、平滑重启或定时重启等方式。本专题为大家提供nginx重启的相关的文章、下载、课程内容,供大家免费下载体验。

227

2023.07.27

nginx 配置详解
nginx 配置详解

Nginx的配置是指设置和调整Nginx服务器的行为和功能的过程。通过配置文件,可以定义虚拟主机、HTTP请求处理、反向代理、缓存和负载均衡等功能。Nginx的配置语法简洁而强大,允许管理员根据自己的需要进行灵活的调整。php中文网给大家带来了相关的教程以及文章,欢迎大家前来学习阅读。

490

2023.08.04

nginx配置详解
nginx配置详解

NGINX与其他服务类似,因为它具有以特定格式编写的基于文本的配置文件。本专题为大家提供nginx配置相关的文章,大家可以免费学习。

496

2023.08.04

tomcat和nginx有哪些区别
tomcat和nginx有哪些区别

tomcat和nginx的区别:1、应用领域;2、性能;3、功能;4、配置;5、安全性;6、扩展性;7、部署复杂性;8、社区支持;9、成本;10、日志管理。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

219

2024.02.23

nginx报404怎么解决
nginx报404怎么解决

当访问 nginx 网页服务器时遇到 404 错误,表明服务器无法找到请求资源,可以通过以下步骤解决:1. 检查文件是否存在且路径正确;2. 检查文件权限并更改为 644 或 755;3. 检查 nginx 配置,确保根目录设置正确、没有冲突配置等等。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

327

2024.07.09

Nginx报404错误解决方法
Nginx报404错误解决方法

解决方法:只需要加上这段配置:try_files $uri $uri/ /index.html;即可。想了解更多Nginx的相关内容,可以阅读本专题下面的文章。

3500

2024.08.07

while的用法
while的用法

while的用法是“while 条件: 代码块”,条件是一个表达式,当条件为真时,执行代码块,然后再次判断条件是否为真,如果为真则继续执行代码块,直到条件为假为止。本专题为大家提供while相关的文章、下载、课程内容,供大家免费下载体验。

80

2023.09.25

curl_exec
curl_exec

curl_exec函数是PHP cURL函数列表中的一种,它的功能是执行一个cURL会话。给大家总结了一下php curl_exec函数的一些用法实例,这个函数应该在初始化一个cURL会话并且全部的选项都被设置后被调用。他的返回值成功时返回TRUE, 或者在失败时返回FALSE。

420

2023.06.14

虚拟号码教程汇总
虚拟号码教程汇总

本专题整合了虚拟号码接收验证码相关教程,阅读下面的文章了解更多详细操作。

25

2025.12.25

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
进程与SOCKET
进程与SOCKET

共6课时 | 0.3万人学习

nginx浅谈
nginx浅谈

共15课时 | 0.8万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号