1.为什么要平滑升级
随着nginx越来越流行,并且nginx的优势也越来越明显,nginx的版本迭代也进行加速,最新版本更新了许多新功能,伴随着nginx的广泛应用,版本升级必然也越来越快,线上业务不能停下来,此时nginx的升级就需要平滑升级
nginx 方便地帮助我们实现了平滑升级。其原理简单概括,就是:
(1)在不停掉老进程的情况下,启动新进程。
(2)老进程负责处理仍然没有处理完的请求,但不再接受新请求。
(3)新进程接受新请求。
(4)老进程处理完所有请求,关闭所有连接后,停止。
这样就很方便地实现了平滑升级。一般有两种情况下需要升级 nginx,一种是确实要升级 nginx 的版本,另一种是要为 nginx 添加新的模块。
2.nginx平滑升级的描述
多进程模式下的请求分配方式
nginx默认工作在多进程模式下,即主进程(master process)启动后完成配置加载和端口绑定等动作,fork出指定数量的工作进程(worker process),这些子进程会持有监听端口的文件描述符(fd),并通过在该描述符上添加监听事件来接受连接
信号的接收和处理
nginx主进程在启动完成后会进入等待状态,负责响应各类系统信息,如SIGCHLD SLGHUP SIGUSR2等
3.nginx信号简介
主进程支持的信号
TERM,INT : 立刻退出
QUIT :等待工作进程结束后在退出
KILL : 强制终止进程
HUP : 重新加载配置文件,使用新的配置启动工作进程,并逐步关闭旧进程。
USR1:重新打开日志文件
USR2:启动新的主进程,实现热升级
WITHCH:逐步关闭工作进程
工作进程支持的信号
TERM,INT:立刻退出
QUIT:等待请求处理结束后在退出
USR1:重新打开日志文件
4.nginx平滑升级实战操作
1.查看现有的nginx编译参数
[root@b-2 ~]# nginx -v
nginx version: nginx/1.24.0
[root@b-2 ~]# nginx -V
nginx version: nginx/1.24.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --group=nginx --user=nginx --sbin-path=/usr/local/nginx/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/tmp/nginx/client_body --http-proxy-temp-path=/tmp/nginx/proxy --http-fastcgi-temp-path=/tmp/nginx/fastcgi --pid-path=/var/run/nginx.pid --lock-path=/var/lock/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-pcre --with-http_realip_module --with-stream
2.下载解压新版本
[root@b-2 ~]# wget https://nginx.org/download/nginx-1.25.2.tar.gz
解压
[root@b-2 ~]# tar -zxf nginx-1.25.2.tar.gz -C /usr/local/
[root@b-2 ~]# cd /usr/local/nginx-1.25.2/
[root@b-2 nginx-1.25.2]# ls
auto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src
我们可以按照原来的编译参数来继续进行安装,也可以新添加模块,但是我们只需要make,千万不要make install,否则原先的配置文件会被覆盖
[root@b-2 nginx-1.25.2]# ./configure --prefix=/usr/local/nginx --group=nginx --user=nginx --sbin-path=/usr/local/nginx/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/tmp/nginx/client_body --http-proxy-temp-path=/tmp/nginx/proxy --http-fastcgi-temp-path=/tmp/nginx/fastcgi --pid-path=/var/run/nginx.pid --lock-path=/var/lock/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-pcre --with-http_realip_module --with-stream
[root@b-2 nginx-1.25.2]# echo $?
0
[root@b-2 nginx-1.25.2]# make
[root@b-2 nginx-1.25.2]# echo $?
0
3.备份原来的nginx文件
备份的时候,期间nginx是不会停止服务的
如果我们之前设置过nginx命令的软连接,一定要解除一下
[root@b-2 nginx-1.25.2]# rm /usr/sbin/nginx
rm:是否删除符号链接 "/usr/sbin/nginx"?y
[root@b-2 nginx-1.25.2]# mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx_$(date +%F)
[root@b-2 nginx-1.25.2]# ls /usr/local/nginx/sbin/
nginx_2023-08-22
4.复制新版本的文件到源码包
我们没有make install nginx启动文件是在解压文件下的/objs目录下 旧版本的备份了,我们把新版本的的替换过去
[root@b-2 nginx-1.25.2]# cp /usr/local/nginx-1.25.2/objs/nginx /usr/local/nginx/sbin/
[root@b-2 nginx-1.25.2]# ls /usr/local/nginx/sbin/
nginx nginx_2023-08-22
5.测试新版本的nginx是否正常
[root@b-2 nginx-1.25.2]# /usr/local/nginx/sbin/nginx -v
nginx version: nginx/1.25.2
[root@b-2 nginx-1.25.2]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
6.启动新的主进程,实现热升级(利用pid)
先查看一下旧版本的pid
[root@b-2 nginx-1.25.2]# cat /var/run/nginx.pid
119435
我们要用USR2来启动新的进程,实现热升级
[root@b-2 nginx-1.25.2]# kill -USR2 `cat /var/run/nginx.pid`
7.查看nginx pid 会出现一个nginx.pid.oldbin
[root@b-2 nginx-1.25.2]# ll /var/run/nginx.pid*
-rw-r--r-- 1 root root 6 8月 22 20:59 /var/run/nginx.pid
-rw-r--r-- 1 root root 7 8月 15 17:02 /var/run/nginx.pid.oldbin
8.关闭旧的nginx进程(winch逐步关闭work进程)
此时有两个进程,所以我们要平滑逐步的关闭旧的work进程,这里我们用WINCH
[root@b-2 nginx-1.25.2]# kill -WINCH `cat /var/run/nginx.pid.oldbin`
9.使用HUP重新加载配置文件,使用新的配置,并逐步关闭旧进程
HUP: 重新加载配置文件,使用新的配置启动工作进程,并逐步关闭旧进程。
[root@b-2 nginx-1.25.2]# kill -HUP `cat /var/run/nginx.pid.oldbin`
10.结束工作进程完成升级(QUIT等待请求处理结束后退出)
QUIT: 等待请求处理结束后再退出
[root@b-2 nginx-1.25.2]# kill -QUIT `cat /var/run/nginx.pid.oldbin
>
> ^C
11.验证nginx是否升级成功
[root@b-2 nginx-1.25.2]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.25.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --group=nginx --user=nginx --sbin-path=/usr/local/nginx/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/tmp/nginx/client_body --http-proxy-temp-path=/tmp/nginx/proxy --http-fastcgi-temp-path=/tmp/nginx/fastcgi --pid-path=/var/run/nginx.pid --lock-path=/var/lock/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-pcre --with-http_realip_module --with-stream
12.访问验证
完成热升级