- 问题
- 案例一:平滑升级nginx服务
- 实验环境
- 实验过程
- 开始升级
- 回滚流程
- 用来平滑升级和回滚的命令
-
平滑升级是什么?
- 是一种在热升级手段,在不中断服务的情况下升级软件。 为什么平滑升级呢?
- 因为现在软件版本功能已经无法满足上产需求,所以需要升级新的版本,来提供更好的服务。 平滑升级是则么做到的?
-
用新的进程,把旧的进程替换掉。
具体是咋么做到的呢?请看 案例1
出现为题是否能回滚呢?
- 可以,回滚流程请看 案例2
selinux:关闭状态
firewalld:关闭状态
nginx两个版本:
例如:版本为nginx-1.20.1和nginx-1.21.3
先安装nginx1.20.1版本
步骤如下:
把软件解压到当前目录 [root@localhost media]# tar -xf nginx-1.20.1.tar.gz 安装编译工具 [root@localhost media]# yum -y install gcc pcre-devel zlib-devel 进入目录编译安装nginx [root@localhost media]# cd nginx-1.20.1/ 创建个用户没有家目录,并且登入环境为nologin,用来管理nginx软件 [root@localhost nginx-1.20.1]# useradd -s /usr/sbin/nologin -M nginx [root@localhost nginx-1.20.1]# ./configure --prefix=/usr/local/nginx --user=nginx 创建软连接,添加环境变量 [root@localhost nginx-1.20.1]# ln -s /usr/local/nginx/sbin/nginx /usr/sbin/ 启动nginx服务 [root@localhost nginx-1.20.1]# nginx 查看进程 [root@localhost media]# ps -aux| grep nginx root 25803 0.0 0.0 20520 620 ? Ss 18:44 0:00 nginx: master process nginx nginx 25804 0.0 0.0 23048 1380 ? S 18:44 0:00 nginx: worker process root 26121 0.0 0.0 112668 968 pts/3 S+ 19:15 0:00 grep --color=auto nginx开始升级
# 把软件解压到当前目录 [root@localhost media]# tar -xf nginx-1.21.3.tar.gz # 把软件解压到当前目录 [root@localhost media]# tar -xf nginx-1.21.3.tar.gz # 进入目录编译安装nginx [root@localhost media]# cd nginx-1.21.3/ # 编译安装中需要注意下,编译安装的名字不能和源文件的名称一样要不然会出现,把源文件给直接替换掉 [root@localhost nginx-1.21.3]# ./configure --prefix=/usr/local/nginx01 --user=nginx [root@localhost nginx-1.21.3]# cd /usr/sbin # 把原来的nginx文件名字改一下,方便升级失败回滚 [root@localhost sbin]# mv nginx nginx01 # 创建软连接,添加环境变量 [root@localhost nginx-1.21.3]# ln -s /usr/local/nginx01/sbin/nginx /usr/sbin/ # 查看进程,查找到nginx进程编号 [root@localhost media]# ps -aux| grep nginx root 25803 0.0 0.0 20520 620 ? Ss 18:44 0:00 nginx: master process nginx nginx 25804 0.0 0.0 23048 1380 ? S 18:44 0:00 nginx: worker process root 26121 0.0 0.0 112668 968 pts/3 S+ 19:15 0:00 grep --color=auto nginx 用此命令来平滑升级 [root@localhost sbin]# kill -usr2 25803 # 现在在查看进程,多出了一个新的父进程和子进程 [root@localhost media]# ps -aux| grep nginx root 25803 0.0 0.0 20520 804 ? Ss 18:44 0:00 nginx: master process nginx nginx 25804 0.0 0.0 23048 1380 ? S 18:44 0:00 nginx: worker process root 26202 0.0 0.0 20520 1604 ? S 19:24 0:00 nginx: master process nginx nginx 26203 0.0 0.0 23048 1376 ? S 19:24 0:00 nginx: worker process root 26205 0.0 0.0 112668 972 pts/3 S+ 19:24 0:00 grep --color=auto nginx # 从容的关掉旧的进程 [root@localhost sbin]# kill -winch 25803 [root@localhost media]# ps -aux| grep nginx root 25803 0.0 0.0 20520 804 ? Ss 18:44 0:00 nginx: master process nginx root 26202 0.0 0.0 20520 1604 ? S 19:24 0:00 nginx: master process nginx nginx 26203 0.0 0.0 23048 1376 ? S 19:24 0:00 nginx: worker process root 26224 0.0 0.0 112668 972 pts/3 S+ 19:27 0:00 grep --color=auto nginx # 现在已经升级成功
注:到此位置已升级完成,没问题可以把旧的进程直接kill掉。如果出现问题在这可知执行回滚操作
回滚流程# 开始实现回滚操作 [root@localhost sbin]# kill -hup 25803 # 旧进程再次启动 [root@localhost sbin]# ps -aux | grep nginx root 25803 0.0 0.0 20520 804 ? Ss 18:44 0:00 nginx: master process nginx root 26202 0.0 0.0 20520 1604 ? S 19:24 0:00 nginx: master process nginx nginx 26203 0.0 0.0 23048 1376 ? S 19:24 0:00 nginx: worker process nginx 26330 0.0 0.0 23048 1376 ? S 19:39 0:00 nginx: worker process root 26332 0.0 0.0 112668 972 pts/3 S+ 19:39 0:00 grep --color=auto nginx [root@localhost media]# kill -winch 26202 [root@localhost media]# kill -9 26202 [root@localhost sbin]# ps -aux | grep nginx root 25803 0.0 0.0 20520 804 ? Ss 18:44 0:00 nginx: master process nginx nginx 26203 0.0 0.0 23048 1376 ? S 19:24 0:00 nginx: worker process # 回滚完成用来平滑升级和回滚的命令
| 命令 | 解释 | 系统支持的信号编号 |
|---|---|---|
| kill -usr2 | 用来平滑升级 | 12 |
| kill -winch | 从容关进程 | 28 |
| kill -hup | 激活进程 | 1 |



