1.Nginx平滑升级概述
1.1 什么是平滑升级
在进行服务版本升级的时候,对于用户访问体验无感知、不 会造成服务中断。
1.2 Nginx进行平滑升级的原理
1.3 如何实现Nginx平滑升级思路(建议准备一个大文件持续下载验证升级是否会影响业务)
- 1.下载新版本Nginx
- 2.了解原旧版本Nginx编译参数
- 3.将旧的nginx二进制文件进行备份,然后替换成为新的nginx二进制文件
- 4.向旧的Nginx的Master进程发送USR2信号
– 4.1 旧的master进程的pid文件添加后缀.oldbin
– 4.2 master进程会用新nginx二进制文件启动新的master进程。 - 5.向旧的master进程发送WINCH信号,旧的worker子进程优雅退出。
- 6.向旧的master进程发送QUIT信号,旧的master进程就退出了。
1.4 对应升级或回滚对应的信号
信号 | 含义 |
QUIT | 优雅关闭,quit |
HUP | 优雅重启,reload |
USR1 | 重新打开日志文件,reopen |
USR2 | 平滑升级可执行的二进制程序 |
WINCH | 平滑关闭Worker进程 |
2.Nginx平滑升级实操(1.18–>1.19)
2.1 准备一个nginx服务,提供一个下载的站点
①我们先查看当前的nginx版本
nginx -v
②搭建一个站点
我们用默认的站点配置文件 default.conf,进行配置
vim default.conf 进行编辑
③在搭建的站点目录下/usr/share/nginx/html
创建一个大文件,我们通过访问这个nginx页面进行下载,再进行升级,来看是否会干扰这个下载操作,展现出我们平滑升级的效果。
dd if=/dev/zero of=/usr/share/nginx/html/bigdata bs=500M count=1
通过浏览器进行下载,查看现在nginx的版本
2.2下载新版本nginx(1.19),对其进行编译(具体编译安装看这里!!)
①从官网找到nginx1.19源码包进行下载
wget http://nginx.org/download/nginx-1.19.4.tar.gz #网上在线下载一种方式
tar xf nginx-1.19.4.tar.gz #解压操作
②从nginx(1.18)原环境中提取编译参数
nginx -V
–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’
③进行nginx(1.19)的编译
./configure( 进入nginx-1.19 目录中)
./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’
手动配置缺少的依赖包
yum -y install openssl-devel
make (生成1.19新版本的二进制文件)
2.3 将旧的nginx二进制文件进行备份,然后替换成为新的nginx二进制文件.
[root@web01 ~]# mv /usr/sbin/nginx /usr/sbin/nginx_1.18
[root@web01 ~]# cp /root/nginx-1.19.4/objs/nginx /usr/sbin/nginx
我们这个时候去查看版本已经变成1.19版本,但是其实并没有真正的升级。
2.4 向旧的nginx的Master进程发送USR2,开始平滑升级。
①找到旧的nginx的进程号
[root@web01 objs]# ps -ef |grep nginx
子进程 父进程
root 1602 1 0 19:13 ? 00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
www 1603 1602 0 19:13 ? 00:00:00 nginx: worker process
root 7031 1507 0 19:34 pts/0 00:00:00 grep --color=auto nginx
第二列是子进程
第三列是父进程
我们要找到的旧nginx的master进程号是1602
1602父进程是系统进程1 ,1602是子进程1603worker.
②向旧的nginx的master发送USR2,平滑升级nginx
[root@web01 objs]# kill -USR2 1602
[root@web01 objs]# ps -ef |grep nginx
root 1602 1 0 19:13 ? 00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
www 1603 1602 0 19:13 ? 00:00:00 nginx: worker process
root 7039 1602 0 19:42 ? 00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
www 7040 7039 0 19:42 ? 00:00:00 nginx: worker process
root 7042 1507 0 19:42 pts/0 00:00:00 grep --color=auto nginx
我们发现发送USR2后,会拉起一个新的nginx的master.新旧nginx的master共存。
2.5 向旧的master进程发送WINCH信号,旧的worker子进程优雅退出。
[root@web01 objs]# kill -WINCH 1602
[root@web01 objs]# ps -ef |grep nginx
root 1602 1 0 19:13 ? 00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
www 1603 1602 0 19:13 ? 00:00:00 nginx: worker process is shutting down
root 7039 1602 0 19:42 ? 00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
www 7040 7039 0 19:42 ? 00:00:00 nginx: worker process
root 7045 1507 0 19:46 pts/0 00:00:00 grep --color=auto nginx
[root@web01 objs]#
2.6 向旧的master进程发送QUIT信号,旧的master进程就退出了
[root@web01 objs]# kill -QUIT 1602
[root@web01 objs]# ps -ef |grep nginx
root 7039 1 0 19:42 ? 00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
www 7040 7039 0 19:42 ? 00:00:00 nginx: worker process
root 7080 1507 0 19:50 pts/0 00:00:00 grep --color=auto nginx
# 这个时候旧的master进程号1602已经退出,而是由新的master的7039管理
2.7 我们在通过浏览器访问一下,查看版本变成了nginx1.19版本
3.Nginx平滑回降实操(1.19–>1.18)
3.1 替换二进制文件
[root@web01 ~]# mv /usr/sbin/nginx /usr/sbin/nginx_1.19
[root@web01 ~]# mv /usr/sbin/nginx_1.18 /usr/sbin/nginx
3.2 执行USR2信号,平滑过度
[root@web01 ~]# ps -ef |grep nginx (旧版本的Nginx1.19)
root 5400 1 0 19:15 ? 00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
www 5401 5400 0 19:15 ? 00:00:00 nginx: worker process
www 5402 5400 0 19:15 ? 00:00:00 nginx: worker process
www 5403 5400 0 19:15 ? 00:00:00 nginx: worker process
www 5404 5400 0 19:15 ? 00:00:00 nginx: worker process
[root@web01 ~]# kill -USR2 5400
[root@web01 ~]# ps -ef |grep nginx
root 5400 1 0 19:15 ? 00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
www 5401 5400 0 19:15 ? 00:00:00 nginx: worker process
www 5402 5400 0 19:15 ? 00:00:00 nginx: worker process
www 5403 5400 0 19:15 ? 00:00:00 nginx: worker process
www 5404 5400 0 19:15 ? 00:00:00 nginx: worker process
root 5459 5400 1 19:24 ? 00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
www 5460 5459 0 19:24 ? 00:00:00 nginx: worker process
www 5461 5459 0 19:24 ? 00:00:00 nginx: worker process
www 5462 5459 0 19:24 ? 00:00:00 nginx: worker process
www 5463 5459 0 19:24 ? 00:00:00 nginx: worker process
3.3 将1.19版本的Nginx的Work关闭
[root@web01 ~]# kill -WINCH 5400
[root@web01 ~]# ps -ef |grep nginx
root 5400 1 0 19:15 ? 00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
root 5459 5400 0 19:24 ? 00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
www 5460 5459 0 19:24 ? 00:00:00 nginx: worker process
www 5461 5459 0 19:24 ? 00:00:00 nginx: worker process
www 5462 5459 0 19:24 ? 00:00:00 nginx: worker process
www 5463 5459 0 19:24 ? 00:00:00 nginx: worker process
3.4 将1.19版本的Nginx的Master关闭
[root@web01 ~]# kill -QUIT 5400
[root@web01 ~]# ps -ef |grep nginx # 1.18
root 5459 1 0 19:24 ? 00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
www 5460 5459 0 19:24 ? 00:00:00 nginx: worker process
www 5461 5459 0 19:24 ? 00:00:00 nginx: worker process
www 5462 5459 0 19:24 ? 00:00:00 nginx: worker process
www 5463 5459 0 19:24 ? 00:00:00 nginx: worker process