美文网首页
nginx平滑升级与回滚

nginx平滑升级与回滚

作者: 一剑仙人跪_ | 来源:发表于2019-07-27 09:51 被阅读0次

    1.安装配置1.6版本的 nginx

    [root@localhost ~]# yum install -y gcc gcc-c++ pcre-devel openssl-devel zlib-devel 
    
    [root@localhost ~]# tar xzf nginx-1.6.3.tar.gz -C /usr/local/ 
    
    [root@localhost ~]# cd /usr/local/nginx-1.6.3
    
    [root@localhost nginx-1.6.3]# ./configure --prefix=/usr/local/nginx --user=nginx -group=nginx --with-http_stub_status_module 
    
    [root@localhost nginx-1.6.3]# make && make install 
    
    [root@localhost nginx-1.6.3]# useradd -M -s /sbin/nologin nginx 
    
    [root@localhost nginx-1.6.3]# /usr/local/nginx/sbin/nginx -t
    nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok 
    nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful 
    
    [root@localhost nginx-1.6.3]# /usr/local/nginx/sbin/nginx 
    

    2.查看版本和模块

    [root@localhost nginx-1.6.3]# /usr/local/nginx/sbin/nginx -V 
    nginx version: nginx/1.6.3 built 
    by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC) 
    configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --withhttp_stub_status_module 
    

    3.访问验证

    [root@localhost nginx-1.6.3]# echo "nginx1.6" > /usr/local/nginx/html/index.html 
    
    [root@localhost nginx-1.6.3]# yum install -y elinks
    
    [root@localhost nginx-1.6.3]# elinks 10.0.105.189
    

    4.升级nginx

    将 nginx 版本进行升级 并在不影响业务的情况下添加 SSL 和 pcre 模块

    [root@localhost ~]# tar xzf nginx-1.12.2.tar.gz -C /usr/local/ 
    [root@localhost ~]# cd /usr/local/nginx-1.12.2/
    [root@localhost nginx-1.12.2]# ./configure --prefix=/usr/local/nginx --user=nginx -group=ngiinx --with-http_stub_status_module --with-http_ssl_module --with-pcre 
    [root@localhost nginx-1.12.2]# make 
    [root@localhost nginx-1.12.2]# cd
    [root@localhost ~]# mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx_lod
    [root@localhost ~]# cp /usr/local/nginx-1.12.2/objs/nginx /usr/local/nginx/sbin/ 
    [root@localhost ~]# ps -ef | grep nginx
    root       6324      1  0 09:06 ?        00:00:00 nginx: master process /usr/local/nginx-1.12.2/sbin/nginx
    nobody     6325   6324  0 09:06 ?        00:00:00 nginx: worker process
    root       6338   1244  0 09:11 pts/0    00:00:00 grep --color=auto nginx
    [root@localhost ~]# kill -USR2 6324  #此时查看进程 给master进程发送一个热升级进程信号
    [root@localhost ~]#  ls /usr/local/nginx/logs/  #主进程将重命名它的进程文件 .pid 文件为 .oldbin
    access.log  error.log  nginx.pid  nginx.oldbin
    [root@localhost ~]# cat  /usr/local/nginx/logs/nginx.pid 
    6340
    [root@localhost ~]# ps -ef | grep nginx
    root       6324      1  0 09:06 ?        00:00:00 nginx: master process /usr/local/nginx-1.12.2/sbin/nginx
    nobody     6325   6324  0 09:06 ?        00:00:00 nginx: worker process
    root       6340   6324  0 09:12 ?        00:00:00 nginx: master process /usr/local/nginx-1.12.2/sbin/nginx
    nobody     6341   6340  0 09:12 ?        00:00:00 nginx: worker process
    root       6343   1244  0 09:12 pts/0    00:00:00 grep --color=auto nginx
    

    在这时,两个 nginx 实例会同时运行,一起处理输入的请求。
    要逐步停止旧的实例,你必须发送 WINCH 信号给旧的主进程,然后,它的工作进程就将开始从容关闭

    [root@nginx ~]# kill -WINCH 6324
    [root@nginx ~]# ps -ef | grep nginx
    root       6324      1  0 09:06 ?        00:00:00 nginx: master process /usr/local/nginx-1.12.2/sbin/nginx
    root       6340   6324  0 09:12 ?        00:00:00 nginx: master process /usr/local/nginx-1.12.2/sbin/nginx
    nobody     6341   6340  0 09:12 ?        00:00:00 nginx: worker process
    root       6346   1244  0 09:14 pts/0    00:00:00 grep --color=auto nginx
    

    一段时间后,旧的工作进程处理了所有已连接的请求后退出,就仅由新的工作进程来处理输入的请求了
    老的worker已经没了,但是老的master还有,是准备回滚使用的

    查看当前nginx状态

    [root@localhost ~]# /usr/local/nginx/sbin/nginx -V 
    nginx version: nginx/1.12.2
    built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC) 
    built with OpenSSL 1.0.2k-fips  26 Jan 2017
    TLS SNI support enabled
    configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre
    

    如果在版本升级完成后,没有任何问题,需要关闭老的master进程.

    5.回滚

    老的master进程是一直存在,在没有手工关闭前,它是不会自已关闭的,这种设计是有好处的,好处就是为了升级新版本后,如果出现问题能及时快速的回滚到上一个稳定版本。

    [root@localhost ~]# ps aux | grep nginx
    root      12264  0.0  0.0  24860   956 ?        Ss   16:31   0:00 nginx: master process /usr/local/nginx/sbin/nginx
    root      20083  0.0  0.1  45936  3260 ?        S    16:38   0:00 nginx: master process /usr/local/nginx/sbin/nginx
    nginx     20084  0.0  0.1  46384  2128 ?        S    16:38   0:00 nginx: worker process
    root      27488  0.0  0.0 112708   984 pts/0    R+   16:47   0:00 grep --color=auto nginx
    [root@localhost ~]# kill -HUP 12264    #发送 HUP 信号给旧的主进程 - 它将在不重载配置文件的情况下启动它的工作进程
    [root@localhost ~]# ps aux | grep nginx
    root      12264  0.0  0.0  24860   956 ?        Ss   16:31   0:00 nginx: master process /usr/local/nginx/sbin/nginx
    root      20083  0.0  0.1  45936  3260 ?        S    16:38   0:00 nginx: master process /usr/local/nginx/sbin/nginx
    nginx     20084  0.0  0.1  46384  2128 ?        S    16:38   0:00 nginx: worker process
    nginx     28108  0.0  0.0  25284  1460 ?        S    16:47   0:00 nginx: worker process
    root      28154  0.0  0.0 112708   980 pts/0    R+   16:47   0:00 grep --color=auto nginx
    [root@localhost ~]# kill -QUIT 20083  #发送 QUIT 信号给新的主进程,要求其从容关闭其工作进程
    [root@localhost ~]# ps aux | grep nginx
    root      12264  0.0  0.0  24860   956 ?        Ss   16:31   0:00 nginx: master process /usr/local/nginx/sbin/nginx
    nginx     28108  0.0  0.0  25284  1460 ?        S    16:47   0:00 nginx: worker process
    root      28933  0.0  0.0 112708   984 pts/0    R+   16:48   0:00 grep --color=auto nginx
    

    如果新主进程还没退出,发送 TERM 信号给新的主进程,迫使其退出

    kill -TERM 20083
    

    如果因为某些原因新的工作进程不能退出,向其发送 KILL 信号

    kill -9 20083
    

    新的主进程退出后,旧的主进程会由移除 .oldbin 后缀,恢复为它的 .pid 文件,这样,一切就都恢复到升级之前了。

    [root@localhost ~]#  ls /usr/local/nginx/logs/ 
    access.log  error.log  nginx.pid
    [root@localhost ~]# cat  /usr/local/nginx/logs/nginx.pid 
    12264
    

    如果尝试升级成功,而你也希望保留新的服务器时,发送 QUIT 信号给旧的主进程使其退出而只留下新的服务器运行:

    kill -QUIT 12264
    

    参考:
    https://blog.csdn.net/mingongge/article/details/88097055
    https://blog.csdn.net/u011085172/article/details/71515745

    Nginx重新编译添加新的模块

    https://www.jianshu.com/p/0309cb5e7e76
    https://www.jianshu.com/p/695af00ee857

    相关文章

      网友评论

          本文标题:nginx平滑升级与回滚

          本文链接:https://www.haomeiwen.com/subject/bsxjrctx.html