nginx的平滑升級

$$$$$$$$$$$$$$$$$$$$$$$$$$$$理論大概$$$$$$$$$$$$$$$$$$$$$$$

一、爲什麼要對 nginx 平滑升級
隨着 nginx 越來越流行,並且 nginx 的優勢也越來越明顯,nginx 的版本迭代也來時加速 模式,1.9.0版本的nginx更新了許多新功能,例如 stream 四層代理功能,伴隨着 nginx 的廣泛應用,版本升級必然越來越快,線上業務不能停,此時 nginx 的升級就是運維的工作了。

二、nginx 方便地幫助我們實現了平滑升級。其原理簡單概括,就是:
(1)在不停掉老進程的情況下,啓動新進程。
(2)老進程負責處理仍然沒有處理完的請求,但不再接受處理請求。
(3)新進程接受新請求。
(4)老進程處理完所有請求,關閉所有連接後,停止。 這樣就很方便地實現了平滑升級。一般有兩種情況下需要升級 nginx,一種是確實要升級 nginx 的版本,另一種是要爲 nginx 添加新的模塊。

三、nginx 平滑升級原理
多進程模式下的請求分配方式
nginx 默認工作在多進程模式下,即主進程(master process)啓動後完成配置加載和端口綁定等動作,fork出指定數量的工作進程(worker process),這些子進程會持有監聽端口的文件描述符(fd),並通過在該描述符上添加監聽事件來接受連接(accept)。
信號的接收和處理
nginx 主進程在啓動完成後會進入等待狀態,負責響應各類系統消息,如SIGCHLD、SIGHUP、SIGUSR2等。

四、Nginx信號簡介:
主進程支持的信號
• TERM, INT: 立刻退出
• QUIT: 等待工作進程結束後再退出
• KILL: 強制終止進程
• HUP: 重新加載配置文件,使用新的配置啓動工作進程,並逐步關閉舊進程。
• USR1: 重新打開日誌文件
• USR2: 啓動新的主進程,實現熱升級
• WINCH: 逐步關閉工作進程

工作進程支持的信號
• TERM, INT: 立刻退出
• QUIT: 等待請求處理結束後再退出
• USR1: 重新打開日誌文件

%%%%%%%%%%%%%%%%%%%nginx 平滑升級的過程%%%%%%%%%%%%%%%%%%

1、yum安裝的nginx升級,以當前nginx爲nginx-1.12.2版本升級到nginx-1.16.1爲例

 yum -y install nginx          (爲什麼可以直接yum,因爲epel擴展源裏存在nginx-1.12.2)

systemctl start nginx
systemctl status nginx      (確定已經開啓nginx)     

nginx -V                        檢測版本與配置參數

nginx version: nginx/1.12.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --http-scgi-temp-path=/var/lib/nginx/tmp/scgi --pid-path=/run/nginx.pid --lock-path=/run/lock/subsys/nginx --user=nginx --group=nginx --with-file-aio --with-ipv6 --with-http_auth_request_module --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module=dynamic --with-http_image_filter_module=dynamic --with-http_geoip_module=dynamic --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_slice_module --with-http_stub_status_module --with-http_perl_module=dynamic --with-mail=dynamic --with-mail_ssl_module --with-pcre --with-pcre-jit --with-stream=dynamic --with-stream_ssl_module --with-google_perftools_module --with-debug --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -m64 -mtune=generic' --with-ld-opt='-Wl,-z,relro -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-E'

2,開始升級版本由nginx-1.12.2升級到nginx-1.16.1版本。按照原來的編譯參數安裝 nginx 的方法進行安裝,只需要到 make,千萬不要 make install 。如果make install 會將原來的配置文件覆蓋

wget http://nginx.org/download/nginx-1.16.1.tar.gz     

tar xzf nginx-1.16.1.tar.gz -C /usr/local 解壓壓縮包到指定位置

yum -y install GeoIP GeoIP-devel GeoIP-data perl-devel perl-ExtUtils-Embed gd-devel libxml2 libxslt-devel gperftools

yum install -y gcc gcc-c++ pcre-devel openssl-devel zlib-devel 添加環境

cd /usr/local ( cd到解壓後的目錄 )
ls
cd nginx-1.16.1/

[root@localhost nginx-1.16.1]#nginx -V (配置參數前先查看原來版本的參數)
[root@localhost nginx-1.16.1]#./configure --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --http-scgi-temp-path=/var/lib/nginx/tmp/scgi --pid-path=/run/nginx.pid --lock-path=/run/lock/subsys/nginx --user=nginx --group=nginx --with-file-aio --with-ipv6 --with-http_auth_request_module --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module=dynamic --with-http_image_filter_module=dynamic --with-http_geoip_module=dynamic --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_slice_module --with-http_stub_status_module --with-http_perl_module=dynamic --with-mail=dynamic --with-mail_ssl_module --with-pcre --with-pcre-jit --with-stream=dynamic --with-stream_ssl_module --with-google_perftools_module --with-debug
(配置升級版本的參數要與原版本相同的參數,複製到--with-debug 即可,其後的--with cc的都不需要)

make

3,備份原 nginx 二進制文件 (yum安裝的nginx,其二進制文件和配置文件默認存放於/usr/sbin/)備份二進制文件和 nginx 的配置文件(期間nginx不會停止服務)
mv /usr/sbin/nginx /usr/sbin/nginx_$(date +%F)

4,複製新版本的nginx二進制文件,進入新的nginx源碼包
cp /usr/local/nginx-1.16.1/objs/nginx /usr/sbin/

5,測試新版本的nginx是否正常
/usr/sbin/nginx -t
若出現如下報錯:
nginx: [emerg] module "/usr/lib64/nginx/modules/ngx_http_geoip_module.so" version 1012002 instead of 1016001 in /usr/share/nginx/modules/mod-http-geoip.conf:1
nginx: configuration file /etc/nginx/nginx.conf test failed
解決方法:
#vim /etc/nginx/nginx.conf (編輯nginx的主配置文件)
找到#include /usr/share/nginx/modules/*.conf;這一句並將其註釋即可

/usr/sbin/nginx -t (再次測試便不會在報錯)

6、給nginx發送平滑遷移信號(若不清楚pid路徑,請查看/etc/nginx/nginx.conf配置文件)
kill -USR2 cat /var/run/nginx.pid

(查看pid也可find / -name "nginx.pid")

7.查看nginx pid,會出現一個nginx.pid.oldbin
[root@localhost nginx-1.16.1]# ll /var/run/nginx.pid*
-rw-r--r--. 1 root root 5 8月 24 01:30 /var/run/nginx.pid
-rw-r--r--. 1 root root 5 8月 24 01:30 /var/run/nginx.pid.oldbin

8、從容關閉舊的Nginx進程
#kill -WINCH cat /var/run/nginx.pid.oldbin

9、此時不重載配置啓動舊的工作進程
#kill -HUP cat /var/run/nginx.pid.oldbin

10、結束工作進程,完成此次升級
#kill -QUIT cat /var/run/nginx.pid.oldbin

11、驗證Nginx是否升級成功
nginx -V
nginx version: nginx/1.16.1 版本升級成功爲nginx-1.16.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章