搜索
首页运维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 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模拟进行蓝绿部署的详细内容。更多信息请关注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开发工具