nginx的平滑升级与失败回切

概述:

nginx方便的帮助我们实现了平滑升级,其原理简单概括如下,就是:

  • 在不停掉老进程的情况下启动新进程。
  • 老进程负责处理仍然没有请求完的请求,但不再接收处理请求。
  • 新进程接收新请求。
  • 老进程处理完所有请求后,关闭所有连接,停止。

升级过程:

  • 查看当前版本的编译参数。

[root@server3 nginx]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-4) (GCC)
built with OpenSSL 1.0.1e-fips 11 Feb 2013
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --with-threads --with-file-aio --with-http_ssl_module --with-http_stub_status_module

  • 下载新版本:http://nginx.org/en/download.html
    然后:解压 > 便以前的准备 > 编译
    [root@server3 ~]# tar zxf nginx-1.16.0.tar.gz
    [root@server3 ~]# cd nginx-1.16.0
    [root@server3 nginx-1.16.0]# ./configure --prefix=/usr/local/nginx --with-threads --with-file-aio --with-http_ssl_module --with-http_stub_status_module
    #还可根据需求再增加新的模块
    [root@server3 nginx-1.16.0]# make
    注意:不要make install了
  • 备份老版本的可执行文件
    [root@server3 nginx-1.16.0]# mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.old
    #如果有必要的话,再进行配置文件的修改,为了实验更加明显不隐藏版本号
  • 复制编译后objs目录下的nginx文件到nginx的安装目录sbin/下
    [root@server3 ~]# cp nginx-1.16.0/objs/nginx /usr/local/nginx/sbin/ -f
    cp: overwrite `/usr/local/nginx/sbin/nginx’? y
    ##-f 强制,如果不加-f,原来的nginx仍在运行,就不会覆盖
  • 进行平滑升级

-HUP 平滑启动(相当于reload)
-USR2 平滑升级可执行程序,主要用在版本升级
-WINCH 从容关闭工作进程
-USR1 重新打开日志文件,主要用在日志切割(相当于reopen)

  1. kill -USR2 旧版本主进程号
    执行新的主进程(新版本)和新的工作进程,依次启动新的主进程和新的工作进程,现在新,旧版本的nginx实例会同时运行,共同处理请求

root 3710 0.0 0.1 47304 1204 ? Ss 19:12 0:00 nginx: master p
nginx 3711 0.0 0.1 47772 1844 ? S 19:12 0:00 nginx: worker p
nginx 3712 0.0 0.1 47772 1812 ? S 19:12 0:00 nginx: worker p

[root@server3 sbin]# kill -USR2 3710
root 3710 0.0 0.1 47304 1368 ? Ss 19:12 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 3711 0.0 0.1 47772 1844 ? S 19:12 0:00 nginx: worker process
nginx 3712 0.0 0.1 47772 1812 ? S 19:12 0:00 nginx: worker process
root 6365 0.0 0.3 47308 3228 ? S 19:37 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 6366 0.0 0.1 47780 1836 ? S 19:37 0:00 nginx: worker process
nginx 6367 0.0 0.1 47780 1804 ? S 19:37 0:00 nginx: worker process

  1. kill -WINCH 旧版本主进程号
    [root@server3 sbin]# kill -WINCH 3710
    [root@server3 sbin]# ps aux #可以看到旧版本的worker进程没有了
    root 3710 0.0 0.1 47304 1372 ? Ss 19:12 0:00 nginx: master p
    root 6365 0.0 0.3 47308 3228 ? S 19:37 0:00 nginx: master p
    nginx 6366 0.0 0.1 47780 1836 ? S 19:37 0:00 nginx: worker p
    nginx 6367 0.0 0.1 47780 1804 ? S 19:37 0:00 nginx: worker p
  2. 再查看版本号, 就平滑升级成了新的版本

回切过程

  • 将原来备份好旧版本的二进制文件的再复制回去
    [root@server3 sbin]# cp nginx.old nginx -f
    cp: overwrite `nginx’? y
  • kill -HUP 旧版本的主进程号
    nginx将在不重载配置文件的情况下启动旧版的worker进程
    [root@server3 sbin]# kill -HUP 3710
    [root@server3 sbin]# ps aux
    root 3710 0.0 0.1 47304 1372 ? Ss 19:12 0:00 nginx: master p
    root 6365 0.0 0.3 47308 3228 ? S 19:37 0:00 nginx: master p
    nginx 6366 0.0 0.1 47780 1836 ? S 19:37 0:00 nginx: worker p
    nginx 6367 0.0 0.1 47780 1804 ? S 19:37 0:00 nginx: worker p
    nginx 6376 0.0 0.1 47772 1848 ? S 19:46 0:00 nginx: worker p
    nginx 6377 0.0 0.1 47772 1816 ? S 19:46 0:00 nginx: worker p
  • kill -WINCH 新版本主进程号,新版主进程就开始从容关闭

[root@server3 sbin]# kill -WINCH 6365
[root@server3 sbin]# ps aux #新版本的worker进程没有了
root 3710 0.0 0.1 47304 1372 ? Ss 19:12 0:00 nginx: master process /usr/local/nginx/sbin/nginx
root 6365 0.0 0.3 47308 3228 ? S 19:37 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 6376 0.0 0.1 47772 1848 ? S 19:46 0:00 nginx: worker process
nginx 6377 0.0 0.1 47772 1816 ? S 19:46 0:00 nginx: worker process

  • 再查看版本号, 恢复成了旧版本

猜你喜欢

转载自blog.csdn.net/weixin_43273168/article/details/89889961