1分钟搞定 Nginx 版本的平滑升级与回滚

服务器
Nginx无论是对于运维、开发、还是测试来说,都是日常工作需要掌握的一个知识点,今天,我们来聊一聊,在企业实际生产环境中经常遇到的一个情况,升级Nginx到新的版本和如何回滚至旧版本。

Nginx无论是对于运维、开发、还是测试来说,都是日常工作需要掌握的一个知识点,今天,我们来聊一聊,在企业实际生产环境中经常遇到的一个情况,升级Nginx到新的版本和如何回滚至旧版本。

[[258709]]

1、环境介绍

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

  1. [root@nginx ~]# cd /download/nginx/ 
  2. [root@nginx nginx]# ll 
  3. total 1952 
  4. -rw-r--r-- 1 root root  981687 Oct 17  2017 nginx-1.12.2.tar.gz 
  5. -rw-r--r-- 1 root root 1015384 Dec  4 09:58 nginx-1.14.2.tar.gz 

2、编译安装新旧版本

编译安装nginx-1.12.2

  1. [root@nginx nginx]# tar zxf nginx-1.12.2.tar.gz  
  2. [root@nginx nginx]# cd nginx-1.12.2 
  3. [root@nginx nginx-1.12.2]# ./configure --prefix=/usr/local/nginx-1.12.2 
  4. [root@nginx nginx-1.12.2]# echo $? 
  5. [root@nginx nginx-1.12.2]# make && make install 
  6. [root@nginx nginx-1.12.2]# echo $? 
  7. [root@nginx nginx-1.12.2]# ll /usr/local/nginx-1.12.2/ 
  8. total 0 
  9. drwxr-xr-x 2 root root 333 Mar  1 09:01 conf 
  10. drwxr-xr-x 2 root root  40 Mar  1 09:01 html 
  11. drwxr-xr-x 2 root root   6 Mar  1 09:01 logs 
  12. drwxr-xr-x 2 root root  19 Mar  1 09:01 sbin 

编译安装nginx-1.14.2

  1. [root@nginx ~]# cd /download/nginx/ 
  2. [root@nginx nginx]# tar zxf nginx-1.14.2.tar.gz  
  3. [root@nginx nginx]# cd nginx-1.14.2 
  4. [root@nginx nginx-1.14.2]# ./configure --prefix=/usr/local/nginx-1.14.2 
  5. [root@nginx nginx-1.14.2]# echo $? 
  6. [root@nginx nginx-1.14.2]# make && make install 
  7. [root@nginx nginx-1.14.2]# echo $? 
  8. [root@nginx nginx-1.14.2]# ls -l /usr/local/nginx-1.14.2/ 
  9. total 0 
  10. drwxr-xr-x 2 root root 333 Mar  1 09:03 conf 
  11. drwxr-xr-x 2 root root  40 Mar  1 09:03 html 
  12. drwxr-xr-x 2 root root   6 Mar  1 09:03 logs 
  13. drwxr-xr-x 2 root root  19 Mar  1 09:03 sbin 

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

3、启动旧版本nginx

  1. [root@nginx ~]# /usr/local/nginx-1.12.2/sbin/nginx -t 
  2. nginx: the configuration file /usr/local/nginx-1.12.2/conf/nginx.conf syntax is ok 
  3. nginx: configuration file /usr/local/nginx-1.12.2/conf/nginx.conf test is successful 
  4. [root@nginx ~]# /usr/local/nginx-1.12.2/sbin/nginx 
  5. [root@nginx ~]# ps -ef|grep nginx 
  6. root       6324      1  0 09:06 ?        00:00:00 nginx: master process /usr/local/nginx-1.12.2/sbin/nginx 
  7. nobody     6325   6324  0 09:06 ?        00:00:00 nginx: worker process 
  8. root       6327   1244  0 09:06 pts/0    00:00:00 grep --color=auto nginx 
  9. [root@nginx ~]# lsof -i :80 
  10. COMMAND  PID   USER   FD   TYPE DEVICE SIZE/OFF NODE NAME 
  11. nginx   6324   root    6u  IPv4  26324      0t0  TCP *:http (LISTEN) 
  12. nginx   6325 nobody    6u  IPv4  26324      0t0  TCP *:http (LISTEN) 

4、升级到新版本

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

  1. [root@nginx ~]# /usr/local/nginx-1.12.2/sbin/nginx -v 
  2. nginx version: nginx/1.12.2 
  3. [root@nginx ~]# cd /usr/local/nginx-1.12.2/sbin/ 
  4. [root@nginx sbin]# mv nginx nginx-1.12.2 
  5. #首先备份原来的旧版本nginx二进制文件 
  6. [root@nginx sbin]# cp /usr/local/nginx-1.14.2/sbin/nginx ./ 
  7. #拷贝新版本的二进制文件到当前目录 

接下来进行平滑升级操作

  1. [root@nginx ~]# ps -ef|grep nginx 
  2. root       6324      1  0 09:06 ?        00:00:00 nginx: master process /usr/local/nginx-1.12.2/sbin/nginx 
  3. nobody     6325   6324  0 09:06 ?        00:00:00 nginx: worker process 
  4. root       6338   1244  0 09:11 pts/0    00:00:00 grep --color=auto nginx 
  5. [root@nginx ~]# kill -USR2 6324 
  6. [root@nginx ~]# ps -ef|grep nginx 
  7. root       6324      1  0 09:06 ?        00:00:00 nginx: master process /usr/local/nginx-1.12.2/sbin/nginx 
  8. nobody     6325   6324  0 09:06 ?        00:00:00 nginx: worker process 
  9. root       6340   6324  0 09:12 ?        00:00:00 nginx: master process /usr/local/nginx-1.12.2/sbin/nginx 
  10. nobody     6341   6340  0 09:12 ?        00:00:00 nginx: worker process 
  11. root       6343   1244  0 09:12 pts/0    00:00:00 grep --color=auto nginx 

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

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

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

 

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

  1. [root@nginx ~]# /usr/local/nginx-1.12.2/sbin/nginx -v 
  2. nginx version: nginx/1.14.2 

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

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

  1. kill -QUIT old_master_PID 

5、版本回滚

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

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

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

  1. [root@nginx ~]# ps -ef|grep nginx 
  2. root       6324      1  0 09:06 ?        00:00:00 nginx: master process /usr/local/nginx-1.12.2/sbin/nginx 
  3. root       6340   6324  0 09:12 ?        00:00:00 nginx: master process /usr/local/nginx-1.12.2/sbin/nginx 
  4. nobody     6341   6340  0 09:12 ?        00:00:00 nginx: worker process 
  5. root       6350   1244  0 09:23 pts/0    00:00:00 grep --color=auto nginx 
  6. [root@nginx ~]# cd /usr/local/nginx-1.12.2/sbin/ 
  7. [root@nginx sbin]# mv nginx nginx-1.14.2 
  8. [root@nginx sbin]# mv nginx-1.12.2 nginx 
  9. [root@nginx sbin]# kill -USR1 6324 
  10. [root@nginx sbin]# ps -ef|grep nginx 
  11. root       6324      1  0 09:06 ?        00:00:00 nginx: master process /usr/local/nginx-1.12.2/sbin/nginx 
  12. root       6340   6324  0 09:12 ?        00:00:00 nginx: master process /usr/local/nginx-1.12.2/sbin/nginx 
  13. nobody     6341   6340  0 09:12 ?        00:00:00 nginx: worker process 
  14. root       6355   1244  0 09:24 pts/0    00:00:00 grep --color=auto nginx 
  15. [root@nginx sbin]# ./nginx -v 
  16. nginx version: nginx/1.12.2 

从上面的结果发现,已经平滑的回滚的上一个版本,接下来测试是否能正常访问:

 

一样可以正常访问,所以,这个回滚的操作对用户来说也是不可感知的。

责任编辑:武晓燕 来源: 博客园
相关推荐

2018-01-16 10:11:11

Nginx访问日志

2011-02-21 17:48:35

vsFTPd

2011-05-26 09:03:17

JSONjavascript

2009-11-26 11:19:52

NIS服务器

2021-12-01 06:50:50

Docker底层原理

2018-03-12 21:31:24

区块链

2018-08-27 16:15:20

数据库MyISAMInnoDB

2013-06-24 15:32:41

JPush极光推送Android Pus移动开发

2017-09-27 11:00:50

LinuxBash使用技巧

2023-04-12 11:18:51

甘特图前端

2010-03-05 17:28:08

2018-03-12 14:37:50

区块链比特币架构

2015-11-23 17:34:33

秒借

2020-08-25 07:47:03

Java并发队列

2017-12-20 09:42:39

PythonNginx日志

2009-11-05 16:04:19

Oracle用户表

2015-12-03 14:10:26

systemd容器Linux

2020-02-21 19:54:09

HTTPS 配置手把手教

2020-12-18 07:33:20

SpringSchedule组件

2016-04-06 11:14:48

iOS相机自定义
点赞
收藏

51CTO技术栈公众号