nginx热部署实践


【热部署】
nginx作为一个优秀的反向代理服务器,同时具备高可用的特性,nginx也支持热部署
热部署指的是 在不重启或关闭的进程情况下,新应用直接替换掉旧的应用

热部署大致流程
1.备份旧的二进制文件
2.编译安装新的二进制文件,覆盖旧的二进制文件
3.发送USR2信号给旧master进程
4.发送WINCH信号给旧master进程
5.发送QUIT信号给旧master进程


nginx工作模式是master-worker(包工头-工人)
刚才所说的nginx支持reload重载 仅仅是nginx的master进程,检查配置文件语法是否正确,错误则返回错误、
正确也不会改变已经建立的worker,只得等待worker处理完毕请求之后,杀死旧配置文件的worker,启动
新配置文件的worker。
但是nginx这里提供了热部署功能,就是在不影响用户体验下,进行软件版本升级,也就是不主动杀死
worker,替换软件的二进制文件。

【nginx热部署环境准备】
1.准备一个旧版本的nginx
[root@backup ~]# nginx -v
Tengine version: Tengine/2.3.2
nginx version: nginx/1.17.3

2.下载一个新版本的nginx
wget http://tengine.taobao.org/download/tengine-2.2.0.tar.gz


3.目前运行的是旧的nginx版本,如下检查
[root@chaogelinux sbin]# ps -ef|grep nginx
root     20311     1  0 15:12 ?        00:00:00 nginx: master process nginx
nobody   20312 20311  0 15:12 ?        00:00:00 nginx: worker process
root     20314 13589  0 15:12 pts/0    00:00:00 grep --color=auto nginx
[root@chaogelinux sbin]#
[root@chaogelinux sbin]#
[root@chaogelinux sbin]# curl 127.0.0.1/123  #访问错误页面,返回nginx版本了
<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.14.0</center>
</body>
</html>

【热部署实践】
1.备份旧版本的nginx二进制文件
[root@chaogelinux sbin]# pwd
/home/Learn_Nginx/nginx/sbin
[root@chaogelinux sbin]# mv nginx nginx.old
[root@chaogelinux sbin]# ls
nginx.old

2.检查旧版本nginx的编译参数
[root@chaogelinux Learn_Nginx]# nginx.old -V
nginx version: nginx/1.14.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/home/Learn_Nginx/nginx/ --with-http_ssl_module --with-http_flv_module --with-http_gzip_static_module --with-http_stub_status_module --with-threads --with-file-aio

3.编译安装新版本nginx
#下载新nginx源码
[root@chaogelinux Learn_Nginx]# wget http://nginx.org/download/nginx-1.17.8.tar.gz
#编译安装新版本nginx
[root@chaogelinux Learn_Nginx]# tar -zxf nginx-1.17.8.tar.gz
#开始编译
[root@chaogelinux Learn_Nginx]# cd nginx-1.17.8/
[root@chaogelinux nginx-1.17.8]#
[root@chaogelinux nginx-1.17.8]# ./configure --prefix=/home/Learn_Nginx/nginx/ --with-http_ssl_module --with-http_flv_module --with-http_gzip_static_module --with-http_stub_status_module --with-threads --with-file-aio
#编译安装
[root@chaogelinux nginx-1.17.8]# make && make install

4.此时发现已存在2个版本nginx程序
[root@chaogelinux sbin]# ls
nginx  nginx.old

5.替换旧的nginx可执行文件
[root@chaogelinux sbin]# cp -a /home/Learn_Nginx/nginx-1.17.8/objs/nginx /home/Learn_Nginx/nginx/sbin/
cp:是否覆盖"/home/Learn_Nginx/nginx/sbin/nginx"? y

6.检查旧的nginx进程
注意这里的PID和PPID(pid是当前进程的id号,ppid是启动该进程的pid,也就是父ID,可知该pid由谁启动)
[root@chaogelinux sbin]# ps -ef|grep nginx
root     20311     1  0 15:12 ?        00:00:00 nginx: master process nginx
nobody   20312 20311  0 15:12 ?        00:00:00 nginx: worker process
root     20314 13589  0 15:12 pts/0    00:00:00 grep --color=auto nginx

6.发送USR2信号给旧版本主进程,使得nginx旧版本停止接收请求,切换为新nginx版本

[root@chaogelinux sbin]# kill -USR2 `cat ../logs/nginx.pid `

7.检查此时的nginx进程
nginx-master首先会重命名pid文件,在文件后面添加.oldbin后缀
然后会再启动一个新的master进程以及worker,且使用的是新版Nginx
nginx能够自动将新来的请求,过度到新版master进程下,实现平滑过度

#可以发现新的master进程由旧master启动,由PPID可看出
[root@chaogelinux sbin]# ps -ef|grep nginx
root     20311     1  0 15:12 ?        00:00:00 nginx: master process nginx
nobody   20312 20311  0 15:12 ?        00:00:00 nginx: worker process
root     20335 20311  0 15:13 ?        00:00:00 nginx: master process nginx
nobody   20336 20335  0 15:13 ?        00:00:00 nginx: worker process
root     20338 13589  0 15:13 pts/0    00:00:00 grep --color=auto nginx

[root@chaogelinux sbin]# ls ../logs/
access.log        error.log         nginx.pid         nginx.pid.oldbin

8.发送WINCH信号给旧master进程,优雅的关闭旧worker进程
[root@chaogelinux sbin]# kill -WINCH `cat ../logs/nginx.pid.oldbin`

#再次检查进程情况,旧master的worker已经关闭了,旧master不会自己退出,用作版本回退
[root@chaogelinux sbin]# ps -ef|grep nginx
root     20311     1  0 15:12 ?        00:00:00 nginx: master process nginx
root     20335 20311  0 15:13 ?        00:00:00 nginx: master process nginx
nobody   20336 20335  0 15:13 ?        00:00:00 nginx: worker process
root     20607 13589  0 15:25 pts/0    00:00:00 grep --color=auto nginx

9.如果你觉得没问题了,可以关闭旧master进程
[root@chaogelinux sbin]# ps -ef|grep nginx
root     20335     1  0 15:13 ?        00:00:00 nginx: master process nginx
nobody   20336 20335  0 15:13 ?        00:00:00 nginx: worker process
root     20665 13589  0 15:28 pts/0    00:00:00 grep --color=auto nginx


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM