暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

02-Nginx升级回滚

运维之旅 2021-02-25
441

目录

  • yum升级、回滚
  • 二进制升级、回滚

1.yum升级

说在前面:升级前,请备份相关配置文件

1.1.yum升级

# 官网
http://nginx.org/en/download.html
http://nginx.org/en/linux_packages.html

#
 这样添加默认是安装最新的文档版本
# cat /etc/yum.repos.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

#
 可以看到nginx-stable仓库下面nginx版本为1:1.18
# yum list nginx
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * epel: ftp.iij.ad.jp
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
Installed Packages
nginx.x86_64             1:1.16.1-3.el7            @epel       
Available Packages
nginx.x86_64             1:1.18.0-2.el7.ngx        nginx-stable

#
 yum update nginx    ==》看到升级的版本是我们需要的,然后再按 y  确认

Running transaction
  Updating   : 1:nginx-1.18.0-2.el7.ngx.x86_64                                                                                              1/2 
warning: /etc/nginx/nginx.conf created as /etc/nginx/nginx.conf.rpmnew
  Cleanup    : 1:nginx-1.16.1-3.el7.x86_64                                                                                                  2/2 
  Verifying  : 1:nginx-1.18.0-2.el7.ngx.x86_64                                                                                              1/2 
  Verifying  : 1:nginx-1.16.1-3.el7.x86_64                                                                                                  2/2 

Updated:
  nginx.x86_64 1:1.18.0-2.el7.ngx   
  
#
 升级成功后验证
# nginx -v
nginx version: nginx/1.18.0

#
 整个升级阶段,nginx是不会中断服务的,当然,我们做升级前需要在测试环境做充分的测试,然后在业务低峰期对软件版本进行升级。

1.2.yum回滚

# 查看历史
[root@host224-rsync soft]# yum history
Loaded plugins: fastestmirror
ID     | Login user               | Date and time    | Action(s)      | Altered
-------------------------------------------------------------------------------
    14 | root <root>              | 2021-02-22 11:23 | Update         |    1 EE
    13 | root <root>              | 2021-02-09 18:08 | Install        |    3   
......
history list

#
 查看历史详情
[root@host224-rsync soft]# yum history info 14
Loaded plugins: fastestmirror
Transaction ID : 14
Begin time     : Mon Feb 22 11:23:03 2021
Begin rpmdb    : 399:c4fc79f43ca588408ab1e222e2e950e481c6bed4
End time       :            11:23:06 2021 (3 seconds)
End rpmdb      : 399:89182633d354d156c6b6c78f2e4e293dfce28f62
User           : root <root>
Return-Code    : Success
Command Line   : update nginx -y
Transaction performed with:
    Installed     rpm-4.11.3-25.el7.x86_64                      @anaconda
    Installed     yum-3.4.3-154.el7.centos.noarch               @anaconda
    Installed     yum-plugin-fastestmirror-1.1.31-42.el7.noarch @anaconda
Packages Altered:
    Updated nginx-1:1.16.1-3.el7.x86_64     @epel
    Update        1:1.18.0-2.el7.ngx.x86_64 @nginx-stable
Scriptlet output:
   1 warning: /etc/nginx/nginx.conf created as /etc/nginx/nginx.conf.rpmnew
history info

#
 回滚操作
[root@host224-rsync soft]# yum history undo 14
Loaded plugins: fastestmirror
Undoing transaction 14, from Mon Feb 22 11:23:03 2021
    Updated nginx-1:1.16.1-3.el7.x86_64     @epel
    Update        1:1.18.0-2.el7.ngx.x86_64 @nginx-stable
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * epel: ftp.iij.ad.jp
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
Resolving Dependencies
--> Running transaction check
---> Package nginx.x86_64 1:1.16.1-3.el7 will be a downgrade
---> Package nginx.x86_64 1:1.18.0-2.el7.ngx will be erased
--> Finished Dependency Resolution

Dependencies Resolved

=====================================================================================================
 Package                         Arch                             Version                                  Repository                      Size
=====================================================================================================
Downgrading:
 nginx                           x86_64                           1:1.16.1-3.el7                           epel                           563 k

Transaction Summary
=====================================================================================================
Downgrade  1 Package

Total download size: 563 k
Is this ok [y/d/N]: y
Downloading packages:
nginx-1.16.1-3.el7.x86_64.rpm                                                                                            | 563 kB  00:00:05     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : 1:nginx-1.16.1-3.el7.x86_64                                                                                                  1/2 
warning: /etc/nginx/nginx.conf created as /etc/nginx/nginx.conf.rpmnew
  Cleanup    : 1:nginx-1.18.0-2.el7.ngx.x86_64                                                                                              2/2 
Binary upgrade failed, please check nginx's error.log
  Verifying  : 1:nginx-1.16.1-3.el7.x86_64                                                                                                  1/2 
  Verifying  : 1:nginx-1.18.0-2.el7.ngx.x86_64                                                                                              2/2 

Removed:
  nginx.x86_64 1:1.18.0-2.el7.ngx                                                                                                               

Installed:
  nginx.x86_64 1:1.16.1-3.el7                                                                                                                   

Complete!

#
 验证
[root@host224-rsync soft]# nginx -v
nginx version: nginx/1.16.1

1.3.升级到指定版本

# 我感觉如果升级,那就升级到官网最新的稳定版本,按照上面的方法

#
 以Centos7为例
http://nginx.org/packages/mainline/centos/7/x86_64/RPMS/

#
 下载指定的rpm包
wget http://nginx.org/packages/mainline/centos/7/x86_64/RPMS/nginx-1.17.9-1.el7.ngx.x86_64.rpm

#
 升级
[root@host224-rsync soft]# rpm -Uvh nginx-1.17.9-1.el7.ngx.x86_64.rpm 
Preparing...                          ################################# [100%]
Updating / installing...
   1:nginx-1:1.17.9-1.el7.ngx         warning: /etc/nginx/nginx.conf created as /etc/nginx/nginx.conf.rpmnew
################################# [ 50%]
Cleaning up / removing...
   2:nginx-1:1.16.1-3.el7             ################################# [100%]

[root@host224-rsync soft]# nginx -v
nginx version: nginx/1.17.9

1.4.rpm包回滚

# 下载原来的rpm包
官网:
https://centos.pkgs.org/7/epel-x86_64/nginx-1.16.1-3.el7.x86_64.rpm.html

#
 Install Howto
## Download latest epel-release rpm from
http://download-ib01.fedoraproject.org/pub/epel/7/x86_64/
# 下载链接
# wget https://download-ib01.fedoraproject.org/pub/epel/7/x86_64/Packages/n/nginx-1.16.1-3.el7.x86_64.rpm

#
 回滚
rpm -Uvh --oldpackage  nginx-1.16.1-3.el7.x86_64.rpm

#
 nginx -v
nginx version: nginx/1.16.1

2.二进制升级

# 此笔记为以前学习的旧笔记
1、环境介绍

今天准备的两个nginx版本如下:

[root@node1 ~]# cd /download/nginx/
[root@node1 nginx]# ll
total 1952
-rw-r--r--. 1 root root  981687 Oct 17  2017 nginx-1.12.2.tar.gz
-rw-r--r--. 1 root root 1015384 Dec  4  2018 nginx-1.14.2.tar.gz

2、编译安装新旧版本
# 编译安装nginx-1.12.2
[root@node1 ~]# cd /download/nginx/
[root@node1 nginx]# tar zxf nginx-1.12.2.tar.gz
[root@node1 nginx]# cd nginx-1.12.2
[root@node1 nginx-1.12.2]#  ./configure --prefix=/usr/local/nginx-1.12.2 
[root@node1 nginx-1.12.2]# echo $?
0
[root@node1 nginx-1.12.2]# make && make install
[root@node1 nginx-1.12.2]# echo $?
0
[root@node1 nginx-1.12.2]# ll /usr/local/nginx-1.12.2/
total 16
drwxr-xr-x. 2 root root 4096 Jul 24 15:55 conf
drwxr-xr-x. 2 root root 4096 Jul 24 15:55 html
drwxr-xr-x. 2 root root 4096 Jul 24 15:55 logs
drwxr-xr-x. 2 root root 4096 Jul 24 15:55 sbin

#
 编译安装nginx-1.14.2
[root@node1 nginx]# cd /download/nginx/   
[root@node1 nginx]# tar zxf nginx-1.14.2.tar.gz 
[root@node1 nginx]# cd nginx-1.14.2
[root@node1 nginx-1.14.2]#  ./configure --prefix=/usr/local/nginx-1.14.2
[root@node1 nginx-1.14.2]# echo $?
0
[root@node1 nginx-1.14.2]# make && make install
[root@node1 nginx-1.14.2]# echo $?
0
[root@node1 nginx-1.14.2]# ll /usr/local/nginx-1.14.2/
total 16
drwxr-xr-x. 2 root root 4096 Jul 24 16:00 conf
drwxr-xr-x. 2 root root 4096 Jul 24 16:00 html
drwxr-xr-x. 2 root root 4096 Jul 24 16:00 logs
drwxr-xr-x. 2 root root 4096 Jul 24 16:00 sbin

到这里,两个版本的nginx软件已经部署完成。

3、启动旧版本nginx
[root@node1 ~]# /usr/local/nginx-1.12.2/sbin/nginx -t
nginx: the configuration file /usr/local/nginx-1.12.2/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx-1.12.2/conf/nginx.conf test is successful
[root@node1 ~]# /usr/local/nginx-1.12.2/sbin/nginx
[root@node1 ~]# ps -ef|grep nginx|grep -v grep 
root      10206      1  0 16:01 ?        00:00:00 nginx: master process /usr/local/nginx-1.12.2/sbin/nginx
nobody    10207  10206  0 16:01 ?        00:00:00 nginx: worker process 
[root@node1 ~]# lsof -i:80      
COMMAND   PID   USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
nginx   10206   root    6u  IPv4  37529      0t0  TCP *:http (LISTEN)
nginx   10207 nobody    6u  IPv4  37529      0t0  TCP *:http (LISTEN)

4、升级到新版本
版本升级其实就是针对二进制文件的升级,过程如下:

#
查看当前nginx版本
[root@node1 ~]# /usr/local/nginx-1.12.2/sbin/nginx -v
nginx version: nginx/1.12.2
#备份旧版本nginx二进制文件
[root@node1 ~]# cd /usr/local/nginx-1.12.2/sbin/
[root@node1 sbin]#  mv nginx nginx-1.12.2
#拷贝新版本的二进制文件到当前目录
[root@node1 sbin]# cp /usr/local/nginx-1.14.2/sbin/nginx ./
[root@node1 sbin]#  ps -ef|grep nginx

#
接下来进行平滑升级操作
[root@node1 sbin]#  ps -ef|grep nginx|grep -v grep 
root      10206      1  0 16:01 ?        00:00:00 nginx: master process /usr/local/nginx-1.12.2/sbin/nginx
nobody    10207  10206  0 16:01 ?        00:00:00 nginx: worker process             
[root@node1 sbin]# kill -USR2 10206 ==>master pid
[root@node1 sbin]# ps -ef|grep nginx|grep -v grep
root      10206      1  0 16:01 ?        00:00:00 nginx: master process /usr/local/nginx-1.12.2/sbin/nginx  # 旧的master进程
nobody    10207  10206  0 16:01 ?        00:00:00 nginx: worker process   # 旧的worker进程           
root      10312  10206  0 16:14 ?        00:00:00 nginx: master process /usr/local/nginx-1.12.2/sbin/nginx  # 新的master进程
nobody    10313  10312  0 16:14 ?        00:00:00 nginx: worker process   # 新的worker进程  

这时新的master进程已经正常开启,但老的work进程也存在,所以我们使用下面的命令,将老的work进程发出平滑停止的信号,如下:

[root@node1 sbin]# kill -WINCH  10206 # 旧的master进程
[root@node1 sbin]# ps -ef|grep nginx|grep -v grep
root      10206      1  0 16:01 ?        00:00:00 nginx: master process /usr/local/nginx-1.12.2/sbin/nginx  # 旧的master进程
root      10312  10206  0 16:14 ?        00:00:00 nginx: master process /usr/local/nginx-1.12.2/sbin/nginx  # 新的master进程
nobody    10313  10312  0 16:14 ?        00:00:00 nginx: worker process     # 新的worker进程   

此时,老的work进程已经停止,接下来我们测试是否能正常访问:

结果:可以正常打开

可以正常访问,其实这一平滑升级的动作,对访问用户来说是完全感知不到,所以nginx热部署就已经完成了。

[root@node1 sbin]# /usr/local/nginx-1.12.2/sbin/nginx -v
nginx version: nginx/1.14.2

查看版本也是最新的版本,升级完成。

#
 注:如果在版本升级完成后,没有任何问题,需要关闭老的master进程的话,可以使用下面的命令:

kill -QUIT old_master_PID

5、版本回滚

对于升级来说,最难的不是升级,而是回滚,因为在实际生产环境回滚的机率是存在,比如:新版本由于某些未知bug导致与现有应用不兼容、或出现运行不稳定的情况等等。

所以,对运维工程师来说,故障回滚是重点。

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

[root@node1 sbin]# ps -ef|grep nginx |grep -v grep  
root      10385      1  0 16:30 ?        00:00:00 nginx: master process /usr/local/nginx-1.12.2/sbin/nginx
root      10396  10385  0 16:32 ?        00:00:00 nginx: master process /usr/local/nginx-1.12.2/sbin/nginx
nobody    10397  10396  0 16:32 ?        00:00:00 nginx: worker process             
[root@node1 sbin]# cd /usr/local/nginx-1.12.2/sbin/
[root@node1 sbin]# mv nginx nginx-1.14.2
[root@node1 sbin]# mv nginx-1.12.2 nginx
[root@node1 sbin]# ./nginx -v
nginx version: nginx/1.12.2

#
 向老版本的 Nginx 进程发送信号,重启其 worker 进程
[root@node1 sbin]#kill -HUP  10396 
[root@node1 sbin]# ps -ef|grep nginx |grep -v grep  
root      10385      1  0 16:30 ?        00:00:00 nginx: master process /usr/local/nginx-1.12.2/sbin/nginx
root      10396  10385  0 16:32 ?        00:00:00 nginx: master process /usr/local/nginx-1.12.2/sbin/nginx
nobody    10397  10396  0 16:32 ?        00:00:00 nginx: worker process  
nobody    10399  10385  0 16:34 ?        00:00:00 nginx: worker process   

#
 等老版本的 Nginx worker 进程启动后,向新版本的 Nginx master 进程发送信号,让其退出打开的所有 worker 进程
[root@node1 sbin]# kill -WINCH 10396
[root@node1 sbin]# ps -ef|grep nginx |grep -v grep  
root      10385      1  0 16:30 ?        00:00:00 nginx: master process /usr/local/nginx-1.12.2/sbin/nginx
root      10396  10385  0 16:32 ?        00:00:00 nginx: master process /usr/local/nginx-1.12.2/sbin/nginx
nobody    10399  10385  0 16:34 ?        00:00:00 nginx: worker process   

#
 最后退出新版本的 master 进程
[root@node1 sbin]# kill -QUIT 10396


文章转载自运维之旅,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论