Nginx不管是對於運維、開發、仍是測試來講,都是平常工做須要掌握的一個知識點,以前也寫過很多關於Nginx相關的文章:
有興趣的能夠關注【民工哥技術之路】微信公衆號看看上面的文章。html
今天,咱們來聊一聊,在企業實際生產環境中常常遇到的一個狀況,升級Nginx到新的版本和如何回滾至舊版本。nginx
一、環境介紹性能優化
今天準備的兩個nginx版本以下:服務器
[root@nginx ~]# cd /download/nginx/ [root@nginx nginx]# ll total 1952 -rw-r--r-- 1 root root 981687 Oct 17 2017 nginx-1.12.2.tar.gz -rw-r--r-- 1 root root 1015384 Dec 4 09:58 nginx-1.14.2.tar.gz
二、編譯安裝新舊版本微信
編譯安裝nginx-1.12.2負載均衡
[root@nginx nginx]# tar zxf nginx-1.12.2.tar.gz [root@nginx nginx]# cd nginx-1.12.2 [root@nginx nginx-1.12.2]# ./configure --prefix=/usr/local/nginx-1.12.2 [root@nginx nginx-1.12.2]# echo $? 0 [root@nginx nginx-1.12.2]# make && make install [root@nginx nginx-1.12.2]# echo $? 0 [root@nginx nginx-1.12.2]# ll /usr/local/nginx-1.12.2/ total 0 drwxr-xr-x 2 root root 333 Mar 1 09:01 conf drwxr-xr-x 2 root root 40 Mar 1 09:01 html drwxr-xr-x 2 root root 6 Mar 1 09:01 logs drwxr-xr-x 2 root root 19 Mar 1 09:01 sbin
編譯安裝nginx-1.14.2運維
[root@nginx ~]# cd /download/nginx/ [root@nginx nginx]# tar zxf nginx-1.14.2.tar.gz [root@nginx nginx]# cd nginx-1.14.2 [root@nginx nginx-1.14.2]# ./configure --prefix=/usr/local/nginx-1.14.2 [root@nginx nginx-1.14.2]# echo $? 0 [root@nginx nginx-1.14.2]# make && make install [root@nginx nginx-1.14.2]# echo $? 0 [root@nginx nginx-1.14.2]# ls -l /usr/local/nginx-1.14.2/ total 0 drwxr-xr-x 2 root root 333 Mar 1 09:03 conf drwxr-xr-x 2 root root 40 Mar 1 09:03 html drwxr-xr-x 2 root root 6 Mar 1 09:03 logs drwxr-xr-x 2 root root 19 Mar 1 09:03 sbin
到這裏,兩個版本的nginx軟件已經部署完成。性能
三、啓動舊版本nginx測試
[root@nginx ~]# /usr/local/nginx-1.12.2/sbin/nginx -t nginx: the configuration file /usr/local/nginx-1.12.2/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx-1.12.2/conf/nginx.conf test is successful [root@nginx ~]# /usr/local/nginx-1.12.2/sbin/nginx [root@nginx ~]# ps -ef|grep nginx root 6324 1 0 09:06 ? 00:00:00 nginx: master process /usr/local/nginx-1.12.2/sbin/nginx nobody 6325 6324 0 09:06 ? 00:00:00 nginx: worker process root 6327 1244 0 09:06 pts/0 00:00:00 grep --color=auto nginx [root@nginx ~]# lsof -i :80 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME nginx 6324 root 6u IPv4 26324 0t0 TCP *:http (LISTEN) nginx 6325 nobody 6u IPv4 26324 0t0 TCP *:http (LISTEN)
四、升級到新版本優化
版本升級其實就是針對二進制文件的升級,過程以下:
[root@nginx ~]# /usr/local/nginx-1.12.2/sbin/nginx -v nginx version: nginx/1.12.2 [root@nginx ~]# cd /usr/local/nginx-1.12.2/sbin/ [root@nginx sbin]# mv nginx nginx-1.12.2 #首先備份原來的舊版本nginx二進制文件 [root@nginx sbin]# cp /usr/local/nginx-1.14.2/sbin/nginx ./ #拷貝新版本的二進制文件到當前目錄
接下來進行平滑升級操做
[root@nginx ~]# ps -ef|grep nginx root 6324 1 0 09:06 ? 00:00:00 nginx: master process /usr/local/nginx-1.12.2/sbin/nginx nobody 6325 6324 0 09:06 ? 00:00:00 nginx: worker process root 6338 1244 0 09:11 pts/0 00:00:00 grep --color=auto nginx [root@nginx ~]# kill -USR2 6324 [root@nginx ~]# ps -ef|grep nginx root 6324 1 0 09:06 ? 00:00:00 nginx: master process /usr/local/nginx-1.12.2/sbin/nginx nobody 6325 6324 0 09:06 ? 00:00:00 nginx: worker process root 6340 6324 0 09:12 ? 00:00:00 nginx: master process /usr/local/nginx-1.12.2/sbin/nginx nobody 6341 6340 0 09:12 ? 00:00:00 nginx: worker process root 6343 1244 0 09:12 pts/0 00:00:00 grep --color=auto nginx
這時新的master進程已經正常開啓,但老的work進程也存在,因此咱們使用下面的命令,將老的work進程發出平滑中止的信號,以下:
[root@nginx ~]# kill -WINCH 6324 [root@nginx ~]# ps -ef|grep nginx root 6324 1 0 09:06 ? 00:00:00 nginx: master process /usr/local/nginx-1.12.2/sbin/nginx root 6340 6324 0 09:12 ? 00:00:00 nginx: master process /usr/local/nginx-1.12.2/sbin/nginx nobody 6341 6340 0 09:12 ? 00:00:00 nginx: worker process root 6346 1244 0 09:14 pts/0 00:00:00 grep --color=auto nginx
此時,老的work進程已經中止,接下來咱們測試是否能正常訪問:
能夠正常訪問,其實這一平滑升級的動做,對訪問用戶來講是徹底感知不到,因此nginx熱部署就已經完成了。
[root@nginx ~]# /usr/local/nginx-1.12.2/sbin/nginx -v nginx version: nginx/1.14.2
查看版本也是最新的版本,升級完成。
注:若是在版本升級完成後,沒有任何問題,須要關閉老的master進程的話,可使用下面的命令:
kill -QUIT old_master_PID
五、版本回滾
對於升級來講,最難的不是升級,而是回滾,由於在實際生產環境回滾的機率是存在,好比:新版本因爲某些未知bug致使與現有應用不兼容、或出現運行不穩定的狀況等等。
因此,對運維工程師來講,故障回滾是重點。
在上面的結果中,咱們也能看到老的master進程是一直存在,在沒有手工關閉前,它是不會自已關閉的,這種設計是有好處的,好處就是爲了升級新版本後,若是出現問題能及時快速的回滾到上一個穩定版本。
[root@nginx ~]# ps -ef|grep nginx root 6324 1 0 09:06 ? 00:00:00 nginx: master process /usr/local/nginx-1.12.2/sbin/nginx root 6340 6324 0 09:12 ? 00:00:00 nginx: master process /usr/local/nginx-1.12.2/sbin/nginx nobody 6341 6340 0 09:12 ? 00:00:00 nginx: worker process root 6350 1244 0 09:23 pts/0 00:00:00 grep --color=auto nginx [root@nginx ~]# cd /usr/local/nginx-1.12.2/sbin/ [root@nginx sbin]# mv nginx nginx-1.14.2 [root@nginx sbin]# mv nginx-1.12.2 nginx [root@nginx sbin]# kill -USR1 6324 [root@nginx sbin]# ps -ef|grep nginx root 6324 1 0 09:06 ? 00:00:00 nginx: master process /usr/local/nginx-1.12.2/sbin/nginx root 6340 6324 0 09:12 ? 00:00:00 nginx: master process /usr/local/nginx-1.12.2/sbin/nginx nobody 6341 6340 0 09:12 ? 00:00:00 nginx: worker process root 6355 1244 0 09:24 pts/0 00:00:00 grep --color=auto nginx [root@nginx sbin]# ./nginx -v nginx version: nginx/1.12.2
從上面的結果發現,已經平滑的回滾的上一個版本,接下來測試是否能正常訪問:
同樣能夠正常訪問,因此,這個回滾的操做對用戶來講也是不可感知的。
好拉,今天的生產小技巧就分享到這裏,若是你以爲這篇文章對你有所幫助或參考與借鑑,請將此文章轉發分享出去,你的轉發就是對我最大的支持!!
更多精彩文章請關注民工哥的我的微信公衆號:民工哥技術之路