nginx热部署-在线更新和回滚

本文详细介绍了如何在系统中进行Nginx的热部署,包括新版本的编译、在线升级过程,以及在升级失败情况下的热回滚步骤,确保服务的连续性和稳定性。

系统中已经运行了一个版本的nginx的进程:

1、编译生成心得可执行程序
tar zxf nginx-1.16.0.tar.gz
vim auto/cc/gcc
./configure --prefix=/usr/local/nginx --with-file-aio
make 只make不安装

2、在线升级
cd ~/nginx-1.16.0/objs
cp -f nginx /usr/local/nginx/sbin/nginx
ps -ef | grep nginx


root 18034 1 0 10:45 ? 00:00:00 nginx: master process nginx
nginx 18035 18034 0 10:45 ? 00:00:00 nginx: worker process
nginx 18036 18034 0 10:45 ? 00:00:00 nginx: worker process
root 18232 1406 0 10:48 pts/0 00:00:00 grep --color=auto nginx


kill -USR2 18034(master进程) //使worker不在接收请求,同时打开新版本的nginx的master进程,和它的两个线程,实现热升级
ps -ef | grep nginx //旧版进程和新版进程同时存在


root 18034 1 0 10:45 ? 00:00:00 nginx: master process nginx
nginx 18035 18034 0 10:45 ? 00:00:00 nginx: worker process
nginx 18036 18034 0 10:45 ? 00:00:00 nginx: worker process
root 18126 18034 0 10:46 ? 00:00:00 nginx: master process nginx
nginx 18127 18126 0 10:46 ? 00:00:00 nginx: worker process
nginx 18128 18126 0 10:46 ? 00:00:00 nginx: worker process
root 18232 1406 0 10:48 pts/0 00:00:00 grep --color=auto nginx


kill -WINCH 18034 //关闭旧版本master进程的两个worker线程
ps -ef | grep nginx //旧版本的两个worker线程已经关闭


root 18034 1 0 10:45 ? 00:00:00 nginx: master process nginx
root 18126 18034 0 10:46 ? 00:00:00 nginx: master process nginx
nginx 18127 18126 0 10:46 ? 00:00:00 nginx: worker process
nginx 18128 18126 0 10:46 ? 00:00:00 nginx: worker process
root 18829 1406 0 11:00 pts/0 00:00:00 grep --color=auto nginx


/usr/local/nginx/sbin/nginx -V //查看此时的nginx版本为 nginx/1.16.0

注:新版本上线完成后不要结束原来的master进程,防止上线失败需要版本回退;若完全成功后,便可结束原master进程

3、热回退:若上线失败,需在线回退为原来的版本
cd ~/nginx-1.17.1/objs
cp -f nginx /usr/local/nginx/sbin/nginx
kill -UIP 18034 //唤醒旧版本的master进程,使之产生新的worker线程
ps -ef | grep nginx


root 18034 1 0 10:45 ? 00:00:00 nginx: master process nginx
root 18126 18034 0 10:46 ? 00:00:00 nginx: master process nginx
nginx 20016 18126 0 11:24 ? 00:00:00 nginx: worker process
nginx 20017 18126 0 11:24 ? 00:00:00 nginx: worker process
nginx 20146 18034 0 11:26 ? 00:00:00 nginx: worker process
nginx 20147 18034 0 11:26 ? 00:00:00 nginx: worker process
root 20275 1406 0 11:28 pts/0 00:00:00 grep --color=auto nginx


kill -USR2 18126 //新版本的master进程的worker线程不再接收新的用户请求,使回退版本的worker进程接收新的用户请求。
ps -ef | grep nginx


root 18034 1 0 10:45 ? 00:00:00 nginx: master process nginx
root 18126 18034 0 10:46 ? 00:00:00 nginx: master process nginx
nginx 20016 18126 0 11:24 ? 00:00:00 nginx: worker process
nginx 20017 18126 0 11:24 ? 00:00:00 nginx: worker process
nginx 20146 18034 0 11:26 ? 00:00:00 nginx: worker process
nginx 20147 18034 0 11:26 ? 00:00:00 nginx: worker process
root 20626 1406 0 11:35 pts/0 00:00:00 grep --color=auto nginx


kill -WINCH 18126 //关闭新版本master进程的两个worker进程
ps -ef | grep nginx //新版本的两个worker线程已经关闭


root 18034 1 0 10:45 ? 00:00:00 nginx: master process nginx
root 18126 18034 0 10:46 ? 00:00:00 nginx: master process nginx
nginx 20146 18034 0 11:26 ? 00:00:00 nginx: worker process
nginx 20147 18034 0 11:26 ? 00:00:00 nginx: worker process
root 20734 1406 0 11:38 pts/0 00:00:00 grep --color=auto nginx


kill -9 18126 //回退完成后可以结束掉新版本的master进程

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值