Ningx平滑升级和回滚(详细版)

1.实验环境

准备:在8系统里已经安装了 nginx  1.18.0版本的nginx

2.下载安装新版本1.20

[root@Centos8 src]#wget http://nginx.org/download/nginx-1.20.1.tar.gz
[root@Centos8 src]#tar xf nginx-1.20.1.tar.gz                            #进行解压         
[root@Centos8 src]#cd nginx-1.20.1                                       #切换到最新版本目录下
[root@Centos8 nginx-1.20.1]#/apps/nginx/sbin/nginx -V                  #查看当前使用的版本及编译选项。结果如下:          
nginx version: nginx/1.18.0
built by gcc 8.4.1 20200928 (Red Hat 8.4.1-1) (GCC) 
built with OpenSSL 1.1.1g FIPS  21 Apr 2020
TLS SNI support enabled
configure arguments: --prefix=/apps/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module           
 #configure arguments后面是以前编译时的参数。现在编译使用一样的参数

3.开始编译新版本

[root@Centos8 nginx-1.20.1]#./configure --prefix=/apps/nginx \         #开始编译新版本
> --user=nginx \
> --group=nginx \
> --with-http_ssl_module \
> --with-http_v2_module \
> --with-http_realip_module \
> --with-http_stub_status_module \
> --with-http_gzip_static_module \
> --with-pcre \
> --with-stream \
> --with-stream_ssl_module \
> --with-stream_realip_module                                           #按回车         

[root@Centos8 nginx-1.20.1]#make                                        #只要make无需要make install
[root@centos8 nginx-1.20.1]#objs/nginx -v
nginx version: nginx/1.20.1

脚本如下
./configure --prefix=/apps/nginx
–user=nginx
–group=nginx
–with-http_ssl_module
–with-http_v2_module
–with-http_realip_module
–with-http_stub_status_module
–with-http_gzip_static_module
–with-pcre
–with-stream
–with-stream_ssl_module
–with-stream_realip_module

4.查看两个版本

查看

[root@Centos8 nginx-1.20.1]#ll objs/nginx /apps/nginx/sbin/nginx        
-rwxr-xr-x 1 nginx nginx 7595800 Jun 12 10:18 /apps/nginx/sbin/nginx
-rwxr-xr-x 1 root  root  7727952 Jun 12 10:33 objs/nginx

4.1,备份旧版本**

[root@Centos8 nginx-1.20.1]#mv /apps/nginx/sbin/nginx{,.bak} 

在这里插入图片描述

4.2,复制新版本进去**

[root@Centos8 nginx-1.20.1]#cp ./objs/nginx /apps/nginx/sbin/               

5.检测环境和替换升级

[root@Centos8 nginx-1.20.1]#/apps/nginx/sbin/nginx -t
nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /apps/nginx/conf/nginx.conf test is successful

在这里插入图片描述
5.1nginx是旧版,nginx.bak是新版;
在这里插入图片描述
5.2目前允许的仍然是旧版nginx;
在这里插入图片描述
5.3#USR2 平滑升级可执行程序,将存储有旧版本主进程PID的文件重命名为nginx.pid.oldbin,并启动新的nginx
#此时两个master的进程都在运行,只是旧的master不在监听,由新的master监听80
#此时Nginx开启一个新的master进程,这个master进程会生成新的worker进程,这就是升级后的Nginx进程,此时老的进程不会自动退出,但是当接收到新的请求不作处理而是交给新的进程处理。**

5.4.可以看到两个master,新的master是旧版master的子进程,并生成新版的worker进程**

[root@Centos8 nginx-1.20.1]#kill -USR2 `cat /apps/nginx/run/nginx.pid`

在这里插入图片描述
在这里插入图片描述
5.5查看目前实际是新的master接受用户请求

[root@Centos8 nginx-1.20.1]#lsof -i :80


6.测试

6.1在8主机执行

[root@Centos8 html]#dd if=/dev/zero of=m.img bs=1M count=1024

在这里插入图片描述
6.2.在其他主机上执行下载

[root@centos7 ~]#wget --limit-rate=1M http://10.0.0.8/m.img

在这里插入图片描述
6.3 在8主机上 ss -nt查看 目前已经给其他主机提供服务了;
在这里插入图片描述
6.4并且查询目前是由8654这个旧的worker在提供服务;
在这里插入图片描述
6.5.优雅的关闭:向原Nginx主进程发送WINCH信号;含义是在关闭旧的master的时候,还在执行的任务不会马上关闭,会在任务结束后才关闭,而这个时候新的请求 则会由新的master处理;

[root@Centos8 nginx-1.20.1]#kill -WINCH `cat /apps/nginx/run/nginx.pid.oldbin`
[root@Centos8 nginx-1.20.1]#ps auxf|grep nginx
root       11803  0.0  0.0  12132  1148 pts/0    S+   22:13   0:00  |           \_ grep --color=auto nginx
root        8652  0.0  0.1  42440  2608 ?        Ss   21:48   0:00 nginx: master process /apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf
nginx       8654  0.0  0.2  77096  5136 ?        S    21:48   0:00  \_ nginx: worker process is shutting down
root       11720  0.0  0.2  42468  5636 ?        S    21:55   0:00  \_ nginx: master process /apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf
nginx      11721  0.0  0.2  77212  4992 ?        S    21:55   0:00      \_ nginx: worker process
nginx      11722  0.0  0.2  77212  4972 ?        S    21:55   0:00      \_ nginx: worker process

6.6这个时候可以看到其他主机上还在执行的下载依旧继续
在这里插入图片描述
6.7.而原来处理的8654这在处理,还未关闭;但是状态已经显示shutting down;另外一个已经关闭;
在这里插入图片描述
6.8.而如果停止其他主机的任务 再次查看 会发现旧版worker已经彻底关闭
但是原来的master还在,成为新master的父进程;
在这里插入图片描述
在这里插入图片描述

[root@Centos8 nginx-1.20.1]#ps auxf|grep nginx
root       11812  0.0  0.0  12132  1124 pts/0    S+   22:16   0:00  |           \_ grep --color=auto nginx
root        8652  0.0  0.1  42440  2612 ?        Ss   21:48   0:00 nginx: master process /apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf
root       11720  0.0  0.2  42468  5636 ?        S    21:55   0:00  \_ nginx: master process /apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf
nginx      11721  0.0  0.2  77212  4992 ?        S    21:55   0:00      \_ nginx: worker process
nginx      11722  0.0  0.2  77212  4972 ?        S    21:55   0:00      \_ nginx: worker process
[root@Centos8 nginx-1.20.1]#pstree -p|grep nginx
           |-nginx(8652)---nginx(11720)-+-nginx(11721)
           |                            `-nginx(11722)

6.9.在其他主机发起操作 查看目前所有请求由新的worker提供处理;
在这里插入图片描述
7.回滚

7.1如果升级的版本发现问题需要回滚,可以重新拉起旧版本的worker

[root@centos8 nginx-1.20.1]#kill -HUP `cat /apps/nginx/run/nginx.pid.oldbin`

7.2 再次查看 发现旧的master生成了两个新的子进程;worker
在这里插入图片描述

[root@Centos8 nginx-1.20.1]#pstree -p|grep nginx
           |-nginx(8652)-+-nginx(11720)-+-nginx(11721)
           |             |              `-nginx(11722)
           |             |-nginx(11835)
           |             `-nginx(11836)

7.3.关闭新版的master

[root@centos8 nginx-1.20.1]#kill -QUIT `cat /apps/nginx/run/nginx.pid`

在这里插入图片描述
7.4,若停掉用户请求 则彻底返回用户请求;
在这里插入图片描述
7.5.查看目前系统版本,还是1.20 原因是 回退是内存中回退,二进制文件中并没有替换;
在这里插入图片描述
7.6.要确定旧版本,可以在其他主机上访问查看
在这里插入图片描述
8.再次升级

先平滑升级;由于回滚了,系统下目前只有旧版本的信息 8652,执行升级后生成了新的进程11875

[root@centos8 nginx-1.20.1]#kill -USR2 `cat /apps/nginx/run/nginx.pid`

在这里插入图片描述
11875目前是8652的子进程;
在这里插入图片描述
9.确认升级,关闭旧版nginx

目前系统里有新旧两个nginx、先关闭父进程8652,再优雅的关闭两个worker 最后查看,剩下升级后的进程11875

[root@Centos8 nginx-1.20.1]#ls /apps/nginx/run/
nginx.pid  nginx.pid.oldbin
[root@Centos8 nginx-1.20.1]#cat /apps/nginx/run/nginx.pid.oldbin
8652
[root@Centos8 nginx-1.20.1]#kill -WINCH `cat /apps/nginx/run/nginx.pid.oldbin`
[root@Centos8 nginx-1.20.1]#pstree -p |grep nginx
           |-nginx(8652)---nginx(11875)-+-nginx(11876)
           |                            `-nginx(11877)
[root@Centos8 nginx-1.20.1]#kill -QUIT `cat /apps/nginx/run/nginx.pid.oldbin`
[root@Centos8 nginx-1.20.1]#pstree -p |grep nginx
           |-nginx(11875)-+-nginx(11876)
           |              `-nginx(11877

在这里插入图片描述
这个时候确认nginx是最新版本了;
在这里插入图片描述

设置完成
谢谢观赏

在这里插入图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值