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