热部署在nginx中还是一个强大的功能,就是在线升级
原理:首先我们先会替换master进程,同时我们替换的master是与老版本的worker兼容的下一步保持现有连接的worker进程,待其老去退休,进行替换高度的模块化加上精巧的两层模型
nginx支持热加载热部署 ,其实就是在不打断用户请求的情况下更新版本,也就是在线更新版本
热部署成功(平滑更新)
在线更新nginx服务的版本并且更新成功,这个时候nginx的新版本和旧版本进程都可以同时工作,不影响客户的正常访问
热部署失败(回滚)
在线更新nginx服务的版本并且更新失败,这个时候就直接回退到原来的nginx版本进程,保证客户可以正常访问
tar zxf nginx-1.16.0.tar.gz
cd nginx-1.16.0
./configure --help
./configure --prefix=/usr/local/nginx --with-file-aio
make && make install
/usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.16.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-11) (GCC)
configure arguments: --prefix=/usr/local/nginx --with-file-aio
Nginx
版本更新
/usr/local/nginx/sbin/nginx
ps aux | grep nginx
root 10364 0.0 0.1 20504 628 ? Ss 04:34 0:00 nginx: master process /usr/local/ngin/sbin/nginx
nobody 10365 0.0 0.2 20964 1340 ? S 04:34 0:00 nginx: worker process
root 10367 0.0 0.1 112648 960 pts/0 R+ 04:34 0:00 grep --color=auto nginx
tar zxf nginx-1.17.0.tar.gz
cd nginx-1.17.0
./configure --prefix=/usr/local/nginx --with-file-aio
make
/usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.16.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-11) (GCC)
configure arguments: --prefix=/usr/local/nginx --with-file-aio
cp -f /root/Desktop/nginx-1.17.0/objs/nginx /usr/local/nginx/sbin/nginx
ps -ef | grep nginx
root 10364 1 0 04:34 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody 12907 10364 0 04:38 ? 00:00:00 nginx: worker process
root 12915 2101 0 04:43 pts/0 00:00:00 grep --color=auto nginx
kill -USR2 10364
kill -USR2 旧版本的主进程号 (让旧版本的worker进程不再接受请求)
ps -ef | grep nginx
root 10364 1 0 04:34 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody 12907 10364 0 04:38 ? 00:00:00 nginx: worker process
root 12916 10364 0 04:44 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody 12917 12916 0 04:44 ? 00:00:00 nginx: worker process
root 12919 2101 0 04:44 pts/0 00:00:00 grep --color=auto nginx
kill -WINCH 10364
kill -WINCH 旧版本的主进程号 (关闭旧版本的worker进程)
ps -ef | grep nginx
root 10364 1 0 04:34 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
root 12916 10364 0 04:44 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody 12917 12916 0 04:44 ? 00:00:00 nginx: worker process
root 12921 2101 0 04:46 pts/0 00:00:00 grep --color=auto nginx
/usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.17.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-11) (GCC)
configure arguments: --prefix=/usr/local/nginx --with-file-aio
nginx
版本更新失败之后的回滚更新完毕后如果出现问题,我们立马停止更新的进程,回滚到原来版本的进程上面
这就是没有强制结束旧版本的master进程的原因,因为可以通过旧版本的master进程将旧版本的worker进程调用回来
加入我们刚才的更新失败,现在要回到原来的1.16.0版本
kill -HUP 10364
将原来主进程的子进程恢复
ps -ef | grep nginx
kill -WINCH 12916
新版本的进程id
ps -ef | grep nginx
root 10364 0.0 0.2 20644 1360 ? Ss 04:34 0:00 nginx: master process /usr/local/ngin/sbin/nginx
root 12916 0.0 0.3 20548 1700 ? S 04:44 0:00 nginx: master process /usr/local/ngin/sbin/nginx
nobody 12969 0.0 0.2 21080 1448 ? S 05:13 0:00 nginx: worker process
root 12971 0.0 0.1 112648 952 pts/0 R+ 05:13 0:00 grep --color=auto nginx
cp -f /root/Desktop/nginx-1.16.0/objs/nginx /usr/local/nginx/sbin/nginx
nginx version: nginx/1.16.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-11) (GCC)
configure arguments: --prefix=/usr/local/nginx --with-file-aio
kill -9 12916
ps -aux | grep nginx
root 10364 0.0 0.2 20644 1360 ? Ss 04:34 0:00 nginx: master process /usr/local/ngin/sbin/nginx
nobody 12969 0.0 0.2 21080 1448 ? S 05:13 0:00 nginx: worker process
nobody 12972 0.0 0.2 21080 1448 ? S 05:16 0:00 nginx: worker process
root 12976 0.0 0.1 112648 952 pts/0 R+ 05:16 0:00 grep --color=auto nginx