# Nginx 平滑升级

# 1.Nginx 平滑升级概述

# 1.1 什么是平滑升级

在进行服务版本升级的时候,对于用户访问体验无感知、不会造成服务中断。

# 1.2 平滑升级实现原理

2.jpg

# 1.3 平滑升级实现思路

如何实现 Nginx 平滑升级思路(建议准备一个大文件持续下载验证升级是否会影响业务)

  • 1. 下载新版本 nginx
  • 2. 了解原旧版本 nginx 编译参数
  • 3. 将旧 nginx 二进制文件进行备份,然后替换成为新的 nginx 二进制文件
  • 4. 向旧 nginx 的 Master 进程发送 USR2 信号
    • 旧 master 进程的 pid 文件添加后缀.oldbin
    • master 进程会用新 nginx 二进制文件启动新的 master 进程
  • 5. 向旧 master 进程发送 WINCH 信号,旧的 worker 子进程优雅退出
  • 6. 向旧 master 进程发送 QUIT 信号,旧的 master 进程就退出了
# 1.4 平滑升级所需信号
信号含义
QUIT优雅关闭、quit
HUP优雅重启、reload
USR1重新打开日志文件、reopen
USR2平滑升级可执行的二进制程序
WINCH平滑关闭 Worker 进程

# 2. Nginx 平滑升级实践

# 2.1 安装 Nginx 所需依赖
[root@proxy01 ~]# yum install gcc redhat-rpmconfig \
libxslt-devel gd-devel perl-ExtUtils-Embed \
geoip-devel gperftools-devel pcre-devel openssl-devel -y
# 2.2 编译并安装 Nginx
#1. 下载安装包
[root@proxy01 ~]# wget https://nginx.org/download/nginx-1.29.1.tar.gz
[root@proxy01 ~]# tar xf nginx-1.29.1.tar.gz 
[root@proxy01 ~]# cd nginx-1.29.1
#2. 查看编译参数
[root@proxy01 nginx-1.29.1]# nginx -V
#3. 编译
[root@proxy01 ~]# ./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'
#4. 仅 make 即可,不需要 make install。因为 make 后会生成新的二进制文件
[[root@proxy01 ~]# make
# 2.3 备份旧 Nginx 二进制
# 将旧的 nginx 二进制文件进行备份,然后替换成为新的 nginx 二进制文件
[root@proxy01 nginx-1.29.1]# mv /usr/sbin/nginx /usr/sbin/nginx.old
[root@proxy01 nginx-1.29.1]# cp objs/nginx /usr/sbin/nginx
[root@proxy02 nginx-1.29.1]# ll /usr/sbin/nginx*
-rwxr-xr-x 1 root root 10542864 Sep  8 20:44 /usr/sbin/nginx
-rwxr-xr-x 1 root root  1530552 May 30  2024 /usr/sbin/nginx-debug
-rwxr-xr-x 1 root root  1407480 May 30  2024 /usr/sbin/nginx.old
# 2.4 向旧 Master 发送 USR2 信号
# 向旧的 Nginx 的 Master 进程发送 USR2 信号。(平滑升级二进制可执行文件)
[root@proxy02 nginx-1.29.1]# ps aux|grep nginx
root       1610  0.0  0.0  59448  1240 ?        Ss   18:16   0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx      1611  0.0  0.2  60032  3740 ?        S    18:16   0:00 nginx: worker process
nginx      1612  0.0  0.1  59876  1972 ?        S    18:16   0:00 nginx: worker process
root       5674  0.0  0.0 112812   980 pts/0    S+   20:45   0:00 grep --color=auto nginx
# 发送信号
[root@proxy02 nginx-1.29.1]# kill -USR2 1610
# pid 文件会自动添加.oldbin 后缀,该文件中记录的是旧 master 的 pid 进程号
[root@proxy02 nginx-1.29.1]# cat /var/run/nginx.pid.oldbin
1610
# 新老 master 共存
[root@proxy02 nginx-1.29.1]# ps aux|grep nginx
root       1610  0.0  0.0  59448  1240 ?        Ss   18:16   0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx      1611  0.0  0.2  60032  3740 ?        S    18:16   0:00 nginx: worker process
nginx      1612  0.0  0.1  59876  1972 ?        S    18:16   0:00 nginx: worker process
root       5676  0.0  0.2  56960  4040 ?        S    20:47   0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx      5677  0.0  0.1  57404  1972 ?        S    20:47   0:00 nginx: worker process
nginx      5678  0.0  0.1  57404  1972 ?        S    20:47   0:00 nginx: worker process
root       5683  0.0  0.0 112812   980 pts/0    S+   20:48   0:00 grep --color=auto nginx

1.jpg

# 2.5 向旧 Master 发送 WINCH 信号
# 向旧的 master 进程发送 WINCH 信号,旧的 worker 子进程优雅退出
[root@proxy02 nginx-1.29.1]# kill -WINCH 1610
[root@proxy02 nginx-1.29.1]# ps aux|grep nginx
root       1610  0.0  0.0  59448  1396 ?        Ss   18:16   0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
root       5676  0.0  0.2  56960  4040 ?        S    20:47   0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx      5677  0.0  0.1  57404  1972 ?        S    20:47   0:00 nginx: worker process
nginx      5678  0.0  0.1  57404  3480 ?        S    20:47   0:00 nginx: worker process
root       5702  0.0  0.0 108096   616 pts/1    S+   20:51   0:00 tail -f /var/log/nginx/error.log
root       5712  0.0  0.0 112812   980 pts/0    S+   20:55   0:00 grep --color=auto nginx
# 2.6 向旧 Master 发送 QUIT 信号
[root@proxy02 nginx-1.29.1]# kill -QUIT 1610
[root@proxy02 nginx-1.29.1]# ps aux|grep nginx
root       5676  0.0  0.2  56960  4040 ?        S    20:47   0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx      5677  0.0  0.1  57404  1972 ?        S    20:47   0:00 nginx: worker process
nginx      5678  0.0  0.1  57404  3480 ?        S    20:47   0:00 nginx: worker process
root       5702  0.0  0.0 108096   616 pts/1    S+   20:51   0:00 tail -f /var/log/nginx/error.log
root       5714  0.0  0.0 112812   980 pts/0    S+   20:57   0:00 grep --color=auto nginx

# 3. Nginx 平滑回滚实践

# 3.1 平滑回退思路
  • 1. 替换 nginx 二进制文件
  • 2. 向旧的 1.21 master 发送 USR2 信号
  • 3. 向旧的 1.21 master 发送 WINCH
  • 4. 向旧的 1.21 master 发送 QUIT
# 3.2 替换 nginx 二进制文件
[root@proxy02 nginx-1.29.1]# mv /usr/sbin/nginx /usr/sbin/nginx-1.29.1
[root@proxy02 nginx-1.29.1]# mv /usr/sbin/nginx.old /usr/sbin/nginx
# 3.3 向旧的 master 发送 USR2 信号
[root@proxy02 nginx-1.29.1]# ps aux|grep nginx
root       5676  0.0  0.2  56960  4040 ?        S    20:47   0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx      5677  0.0  0.1  57404  1972 ?        S    20:47   0:00 nginx: worker process
nginx      5678  0.0  0.1  57404  3480 ?        S    20:47   0:00 nginx: worker process
root       5702  0.0  0.0 108096   616 pts/1    S+   20:51   0:00 tail -f /var/log/nginx/error.log
root       5743  0.0  0.0 112812   980 pts/0    S+   21:01   0:00 grep --color=auto nginx
[root@proxy02 nginx-1.29.1]# kill -USR2 5676
[root@proxy02 nginx-1.29.1]# ps aux|grep nginx
root       5676  0.0  0.2  56960  4040 ?        S    20:47   0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx      5677  0.0  0.1  57404  1972 ?        S    20:47   0:00 nginx: worker process
nginx      5678  0.0  0.1  57404  3480 ?        S    20:47   0:00 nginx: worker process
root       5702  0.0  0.0 108096   616 pts/1    S+   20:51   0:00 tail -f /var/log/nginx/error.log
root       5744  2.0  0.2  59436  4028 ?        S    21:02   0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx      5745  0.0  0.1  59880  1976 ?        S    21:02   0:00 nginx: worker process
nginx      5746  0.0  0.1  59880  1976 ?        S    21:02   0:00 nginx: worker process
root       5748  0.0  0.0 112812   976 pts/0    S+   21:02   0:00 grep --color=auto nginx
# 3.4 向旧的 master 发送 WINCH 信号
# 向旧的 master 发送 WINCH 信号,优雅关闭旧的 Worker 工作进程
[root@proxy02 nginx-1.29.1]# kill -WINCH 5676
[root@proxy02 nginx-1.29.1]# ps aux|grep nginx
root       5676  0.0  0.2  56960  4040 ?        S    20:47   0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
root       5702  0.0  0.0 108096   616 pts/1    S+   20:51   0:00 tail -f /var/log/nginx/error.log
root       5744  0.0  0.2  59436  4028 ?        S    21:02   0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx      5745  0.0  0.1  59880  1976 ?        S    21:02   0:00 nginx: worker process
nginx      5746  0.0  0.1  59880  3500 ?        S    21:02   0:00 nginx: worker process
root       5751  0.0  0.0 112812   976 pts/0    S+   21:03   0:00 grep --color=auto nginx
# 3.5 向旧的 master 发送 QUIT 信号
# 向旧的 master 发送 QUIT 信号,退出 master 进程
[root@proxy02 nginx-1.29.1]# kill -QUIT 5676
[root@proxy02 nginx-1.29.1]# ps aux|grep nginx
root       5702  0.0  0.0 108096   616 pts/1    S+   20:51   0:00 tail -f /var/log/nginx/error.log
root       5744  0.0  0.2  59436  4028 ?        S    21:02   0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx      5745  0.0  0.1  59880  1976 ?        S    21:02   0:00 nginx: worker process
nginx      5746  0.0  0.1  59880  3500 ?        S    21:02   0:00 nginx: worker process
root       5761  0.0  0.0 112812   980 pts/0    S+   21:05   0:00 grep --color=auto nginx
此文章已被阅读次数:正在加载...更新于

请我喝[茶]~( ̄▽ ̄)~*

Xu Yong 微信支付

微信支付

Xu Yong 支付宝

支付宝