openresty开发系列3--nginx的平滑升级

openresty开发系列3--nginx的平滑升级

nginx服务器从低版本升级为高版本,如果强行停止服务,会影响正在运行的进程。

平滑升级不会停掉正在运行中的进程,这些进程会继续处理请求。但不会接受新请求,这些老的进程在处理完请求之后会停止。此平滑升级过程中,新开的进程会被处理。

一)平滑升级
进入nginx可执行程序的目录
     #  cd /usr/local/nginx/sbin/
     # sbin/nginx -v
       nginx version: nginx/1.13.0    #查看nginx版本

1)下载高版本nginx
wget http://nginx.org/download/nginx-1.13.2.tar.gz

执行指令生成版本的Nginx二进制程序
#  ./configure
# make    #不能执行 make install
# cd objs
此目录下 有高版本的nginx
备份低版本的nginx
cd /usr/local/nginx/sbin/
cp nginx nginx.old
执行强制覆盖,将低版本的nginx替换为刚编译好的高版本的nginx
[root@node5 objs]# cp -rfp /usr/local/src/nginx-1.13.2/objs/nginx /usr/local/nginx/sbin/
cp: overwrite ‘/usr/local/nginx/sbin/nginx’? y

测试一下新复制过来文件生效情况:
# /usr/local/nginx/sbin/nginx -t
[root@node5 objs]# ps -ef|grep nginx
root      43151      1  0 19:40 ?        00:00:00 nginx: master process /usr/local//nginx/sbin/nginx
nobody    43152  43151  0 19:40 ?        00:00:00 nginx: worker process
nobody    43153  43151  0 19:40 ?        00:00:00 nginx: worker process
nobody    43154  43151  0 19:40 ?        00:00:00 nginx: worker process
nobody    43155  43151  0 19:40 ?        00:00:00 nginx: worker process
root      45585  43080  0 19:46 pts/1    00:00:00 grep --color=auto nginx
[root@node5 objs]# cat /usr/local/nginx/logs/nginx.pid
43151

2)执行信号平滑升级
# kill -USR2 `cat /usr/local/nginx/logs/nginx.pid`  更新配置文件

[root@node5 objs]# kill -USR2 `cat /usr/local/nginx/logs/nginx.pid`

给nginx发送USR2信号后,nginx会将logs/nginx.pid文件重命名为nginx.pid.oldbin,然后用新的可执行文件启动一个新的nginx主进程和对应的工作进程,并新建一个新的nginx.pid保存新的主进程号

[root@node5 objs]# cat /usr/local/nginx/logs/nginx.pid
45589


3)kill -WINCH 旧的主进程号
旧的主进程号收到WINCH信号后,将旧进程号管理的旧的工作进程优雅的关闭。即一段时间后旧的工作进程全部关闭,只有新的工作进程在处理请求连接。这时,依然可以恢复到旧的进程服务,因为旧的进程的监听socket还未停止。
处理完后,工作进程会自动关闭
[root@node5 objs]# ps -ef|grep nginx
root      43151      1  0 19:40 ?        00:00:00 nginx: master process /usr/local//nginx/sbin/nginx
nobody    43152  43151  0 19:40 ?        00:00:00 nginx: worker process
nobody    43153  43151  0 19:40 ?        00:00:00 nginx: worker process
nobody    43154  43151  0 19:40 ?        00:00:00 nginx: worker process
nobody    43155  43151  0 19:40 ?        00:00:00 nginx: worker process
root      45589  43151  0 19:46 ?        00:00:00 nginx: master process /usr/local//nginx/sbin/nginx
nobody    45590  45589  0 19:46 ?        00:00:00 nginx: worker process
nobody    45591  45589  0 19:46 ?        00:00:00 nginx: worker process
nobody    45592  45589  0 19:46 ?        00:00:00 nginx: worker process
nobody    45593  45589  0 19:46 ?        00:00:00 nginx: worker process
root      45595  43080  0 19:46 pts/1    00:00:00 grep --color=auto nginx

4)# kill -QUIT `cat /usr/local/nginx/logs/nginx.pid.oldbin` 优雅的关闭
给旧的主进程发送QUIT信号后,旧的主进程退出,并移除logs/nginx.pid.oldbin文件,nginx的升级完成。

升级完成了,最后在看一下升级后的版本

查看
[root@node5 objs]# /usr/local/nginx/sbin/nginx -v
nginx version: nginx/1.13.2

已经平滑升级成功

二)中途停止升级,回滚到旧的nginx

在步骤(3)时,如果想回到旧的nginx不再升级

(1)给旧的主进程号发送HUP命令,此时nginx不重新读取配置文件的情况下重新启动旧主进程的工作进程。
kill -HUP 43151 --旧主进程号
重启工作进程

(2)优雅的关闭新的主进程
kill -QUIT 45589  --新主进程号

猜你喜欢

转载自www.cnblogs.com/reblue520/p/11428939.html