14.Nginx的平滑升级
孙富阳, 江湖人称没人称。多年互联网运维工作经验,曾负责过孙布斯大规模集群架构自动化运维管理工作。擅长Web集群架构与自动化运维,曾负责国内某大型博客网站运维工作。
1.Nginx平滑升级概述
1.什么是平滑升级
在进行服务版本升级的时候,对于用户访问体验无感知、不会造成服务中断。
2.Nginx平滑升级原理

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进程就 退出了。
2.Nginx平滑升级:1.18 ---> 1.19
1.创建目录,准备站点文件
[root@web01 /etc/nginx/conf.d]# mkdir /code
[root@web01 /etc/nginx/conf.d]# vim default.conf
[root@web01 /etc/nginx/conf.d]# cat default.conf
server {
listen 80 default_server;
server_name localhost;
location / {
limit_rate 200k;
root /code;
index index.html index.htm;
}
}
[root@web01 /etc/nginx/conf.d]# nginx -t
[root@web01 ~]# dd if=/dev/zero of=/code/bigdata bs=500M count=1
2.下载最新版本的nginx
[root@web01 ~]# wget http://nginx.org/download/nginx-1.21.1.tar.gz
[root@web01 ~]# tar xf nginx-1.21.1.tar.gz
[root@web01 ~]# cd nginx-1.21.1/
[root@web01 ~/nginx-1.21.1]# yum install -y pcre-devel openssl-devel libxml2-devel libxslt-devel gd-devel perl-devel perl-ExtUtils-Embed gperftools
[root@web01 ~/nginx-1.21.1]# ./configure
[root@web01 ~/nginx-1.21.1]# make
3.将旧的nginx二进制文件进行备份,然后替换成为新的nginx二进制文件
[root@web01 ~]# mv /usr/sbin/nginx /usr/sbin/nginx_1.20
[root@web01 ~]# cp /root/nginx-1.21.1/objs/nginx /usr/sbin/nginx
#检查是否替换成功
[root@web01 ~]# nginx -v
#打开浏览器输入:10.0.0.7/bigdata
4.向旧的Nginx的Master进程发送USR2信号
4.1、旧的master进程的pid文件添加后缀.oldbin
4.2、master进程会用新nginx二进制文件启动新的master进程。
[root@web01 ~]# ps -ef |grep nginx # 1.18
root 1309 1 0 16:53 ? 00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
www 1787 1309 0 19:08 ? 00:00:00 nginx: worker process
www 1788 1309 0 19:08 ? 00:00:00 nginx: worker process
www 1789 1309 0 19:08 ? 00:00:00 nginx: worker process
www 1790 1309 0 19:08 ? 00:00:00 nginx: worker process
[root@web01 ~]# kill -USR2 1309 # 发送USR2信号,平滑升级Nginx
[root@web01 ~]# ps -ef |grep nginx
root 1309 1 0 16:53 ? 00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
www 1787 1309 0 19:08 ? 00:00:00 nginx: worker process
www 1788 1309 0 19:08 ? 00:00:00 nginx: worker process
www 1789 1309 0 19:08 ? 00:00:00 nginx: worker process
www 1790 1309 0 19:08 ? 00:00:00 nginx: worker process
root 5400 1309 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
#打开浏览器刷新界面,发现依旧在下载,因为现在旧版本的master与新版本的master是共存状态。
5.向旧的master进程发送WINCH信号,旧的worker子进程优雅退出。
[root@web01 ~]# kill -WINCH 1309
[root@web01 ~]# ps -ef |grep nginx
root 1309 1 0 16:53 ? 00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
root 5400 1309 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
6.向旧的master进程发送QUIT信号,旧的master进程就退出了
[root@web01 ~]# kill -QUIT 1309
[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
ps:nginx平滑降版本,跟升级操作一样,只是版本不一样
未经允许不得转载:孙某某的运维之路 » 14.Nginx的平滑升级
评论已关闭