nginx平滑升级 原理及操作

一、介绍

(1)含义:在线上业务不停止的情况下,进行nginx升级。

(2)过程:①、在不停掉老进程的情况下,启动新进程。

                    ②、老进程负载处理没有处理完的请求,但不接受新的处理请求。

                    ③、新进程接受新请求。

                    ④、老进程处理完所有请求,关闭所有连接后停止。

(3)原理

            

①、多进程模式下的请求分配模式
      nginx默认工作在多进程模式下,即进程启动完成配置加载和端口绑定等动作,fork出指定数量的工作进程,这些子进程会持有监听文件描述符(fd)并通过在该描述符上添加监听事件来接受链接。
②、信号的接收和处理
     nginx主进程在启动完成后进入等待状态。负载相应各类系统信息。

      nginx的型号介绍

        ①、主进程支撑的信号

     

信号 解释
TERM,INT 立即退出
QUIT 等待工作进程结束后再退出
KILL 强制终止进程
HUP 重新加载配置文件,使用新的配置启动进程,并逐步关闭旧进程
usr1

重新打开日志文件

 

usr2 启动新的主进程,实现热升级
WINCH 逐步关闭工作进程

     ②、工作进程支持的信号

             

信号 解释
TERM,INT 立即退出
QUIT 等待请求结束后再退出
USR 重新打开日志文件

二、nginx平滑升级实战

   (1)安装低版本nginx

   处理防火墙

[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@localhost ~]# setenforce 0

安装nginx1.14.0

 

[root@localhost /]# yum install -y gcc gcc-c++ pcre-devel openssl-devel zlib-devel
[root@localhost /]# tar xf nginx-1.14.0.tar.gz -C /usr/local/src/
[root@localhost /]# cd /usr/local/src/nginx-1.14.0/
[root@localhost nginx-1.14.0]# mkdir -p /opt/data/nginx
[root@localhost nginx-1.14.0]# useradd nginx
[root@localhost nginx-1.14.0]# ./configure --prefix=/opt/data/nginx --user=nginx --group=nginx --with-http_stub_status_module
[root@localhost nginx-1.14.0]# make && make install
[root@localhost nginx-1.14.0]# ln -s /opt/data/nginx/sbin/* /usr/sbin/
[root@localhost nginx-1.14.0]# nginx
[root@localhost nginx-1.14.0]# netstat -anput | grep nginx

查看版本与安装模块

[root@localhost nginx-1.14.0]# nginx -v
nginx version: nginx/1.14.0
[root@localhost nginx-1.14.0]# nginx -V
nginx version: nginx/1.14.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC)
configure arguments: --prefix=/opt/data/nginx --user=nginx --group=nginx --with-http_stub_status_module
[root@localhost nginx-1.14.0]# echo "nginx1.14.0" > /opt/data/nginx/html/index.html

(2)备份原nginx文件

 

[root@localhost ~]# cp /opt/data/nginx/sbin/nginx /opt/data/nginx/sbin/nginx_$(date +%F)
[root@localhost ~]# cp /opt/data/nginx/conf/nginx.conf /opt/data/nginx/conf/nginx.conf_$(date +%F)

(3)用脚本循环测试

  另起终端,做循环测试:

[root@localhost ~]# while true ; do curl 192.168.142.135 ; sleep 2 ; done
nginx1.14.0
nginx1.14.0
nginx1.14.0
nginx1.14.0
nginx1.14.0

 (4)编译新的nginx源码包

[root@localhost ~]# tar xf nginx-1.17.5.tar.gz -C /usr/local/src/
[root@localhost ~]# cd /usr/local/src/nginx-1.17.5/
[root@localhost nginx-1.17.5]# ./configure --prefix=/opt/data/nginx --user=nginx --group=nginx --with-http_stub_status_module
[root@localhost nginx-1.17.5]# make && make install

(5)发送USR2信号

    向进程发送USR2型号,nginx会启动一个新版本的master进程和工作进程,和旧版本一起处理请求

  

[root@localhost nginx-1.17.5]# ps -ef | grep nginx | grep -v grep
root      21043      1  0 10:27 ?        00:00:00 nginx: master process nginx
nginx     21044  21043  0 10:27 ?        00:00:00 nginx: worker process
[root@localhost nginx-1.17.5]# kill -USR2 21043
[root@localhost nginx-1.17.5]# ps -ef | grep nginx | grep -v grep
root      21043      1  0 10:27 ?        00:00:00 nginx: master process nginx
nginx     21044  21043  0 10:27 ?        00:00:00 nginx: worker process

(6)发送WINCH信号

     向原nginx主进程发送WINCH信号,它会逐步关闭其下的工作进程(主进程不退出),这时所有请求都会有版本处理。

 

[root@localhost nginx-1.17.5]# kill -WINCH 21043

(7)升级结束

      如果不需要回滚,可以将原nginx进程杀死,至此完成热升级。

[root@localhost nginx-1.17.5]# ps -ef | grep nginx | grep -v grep
root      21043      1  0 10:27 ?        00:00:00 nginx: master process nginx
[root@localhost nginx-1.17.5]# nginx -v
nginx version: nginx/1.17.5
[root@localhost nginx-1.17.5]# nginx -V
nginx version: nginx/1.17.5
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC)
configure arguments: --prefix=/opt/data/nginx --user=nginx --group=nginx --with-http_stub_status_module

 

你可能感兴趣的:(nginx平滑升级 原理及操作)