使用 Nginx 实现平滑升级

一、使用 Nginx 实现平滑升级

实现原理:

  1. 在旧进程运行时,启动新的进程;
  2. 旧进程处理没有完成的请求,但不再接收新的处理请求,而是由新进程负责接收请求并进行处理;
  3. 最后通过关闭旧进程的 PID,使新的进程完全兼容。

1.主进程支持的信号

信号 作用
TERM INT 立即退出
QUIT 等工作进程结束后再退出
KILL 强制终止进程
HUP 重新加载配置文件,使用新的配置启动工作进程,并逐步关闭旧进程
USR1 重新打开日志文件
USR2 启动新的主进程,实现热升级
WINCH 逐步关闭工作进程及工作进程支持的信息

2.配置 Nginx 平滑升级

1)准备软件包并查看旧版安装选项

[root@localhost ~]# wget http://www.nginx.org/download/nginx-1.18.0.tar.gz
[root@localhost ~]# ls
anaconda-ks.cfg  nginx-1.12.0.tar.gz  nginx-1.18.0.tar.gz
[root@localhost ~]# nginx -V
nginx version: nginx/1.12.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC) 
configure arguments: --prefix=/usr/local/nginx/ --user=nginx --group=nginx

在这里插入图片描述
2)安装新版本(需要注意新版本是否需要安装依赖包)

[root@localhost ~]# tar zxf nginx-1.18.0.tar.gz -C /usr/src/
[root@localhost ~]# cd /usr/src/nginx-1.18.0/
[root@localhost nginx-1.18.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx && make

在这里插入图片描述
3)备份原来的二进制文件

[root@localhost nginx-1.18.0]# mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx-$(date +%F)
[root@localhost nginx-1.18.0]# cp objs/nginx /usr/local/nginx/sbin/
[root@localhost nginx-1.18.0]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost nginx-1.18.0]# cd
[root@localhost ~]# /usr/local/nginx/sbin/nginx -V

在这里插入图片描述
4)使用信号实现热升级

[root@localhost ~]# ps aux | grep nginx | grep -v grep
[root@localhost ~]# kill -USR2 Nginx主进程 (master)
  • 向主进程发送 USR2 信号,Nginx 会启动一个新版本的 Master 进程和工作进程,和旧版本一起处理请求。

在这里插入图片描述

[root@localhost ~]# kill -WINCH $(cat /usr/local/nginx/logs/nginx.pid.oldbin)			# 关闭老版本的 Worker 进程
[root@localhost ~]# kill -QUIT $(cat /usr/local/nginx/logs/nginx.pid.oldbin)			# 关闭老版本的 Master 进程
[root@localhost ~]# ps aux | grep nginx | grep -v grep
[root@localhost ~]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.18.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC) 
configure arguments: --prefix=/usr/local/nginx/ --user=nginx --group=nginx

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_46902396/article/details/116143753
今日推荐