Nginx--熱部署

熱部署

熱部署在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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章