rpm包安裝的nginx熱升級

一、本地環境基本介紹

本次測試環境,是通過nginx早期提供的yum倉庫安裝的nginx。所以準確來說,算是官方提供的rpm包來安裝的,安裝文件目錄層級結構:

[root@node1 nginx]# rpm -ql nginx
/etc/logrotate.d/nginx
/etc/nginx
/etc/nginx/conf.d
/etc/nginx/conf.d/default.conf
/etc/nginx/fastcgi_params
/etc/nginx/koi-utf
/etc/nginx/koi-win
/etc/nginx/mime.types
/etc/nginx/modules
/etc/nginx/nginx.conf
/etc/nginx/scgi_params
/etc/nginx/uwsgi_params
/etc/nginx/win-utf
/etc/sysconfig/nginx
/etc/sysconfig/nginx-debug
/usr/lib/systemd/system/nginx-debug.service
/usr/lib/systemd/system/nginx.service
/usr/lib64/nginx
/usr/lib64/nginx/modules
/usr/libexec/initscripts/legacy-actions/nginx
/usr/libexec/initscripts/legacy-actions/nginx/check-reload
/usr/libexec/initscripts/legacy-actions/nginx/upgrade
/usr/sbin/nginx
/usr/sbin/nginx-debug
/usr/share/doc/nginx-1.14.0
/usr/share/doc/nginx-1.14.0/COPYRIGHT
/usr/share/man/man8/nginx.8.gz
/usr/share/nginx
/usr/share/nginx/html
/usr/share/nginx/html/50x.html
/usr/share/nginx/html/index.html
/var/cache/nginx
/var/log/nginx

[root@node1 nginx]# rpm -qa|grep nginx
nginx-1.14.0-1.el7_4.ngx.x86_64

二、yum升級命令說明

(1) 檢測nginx是否有可用更新包

[root@node1 ~]# yum check nginx
Loaded plugins: fastestmirror
check ['nginx']
[root@node1 ~]# yum check-update
[root@node1 ~]# yum check-update nginx
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: centos.ustc.edu.cn
 * epel: mirrors.aliyun.com
 * extras: mirrors.shu.edu.cn
 * updates: mirrors.shu.edu.cn

nginx.x86_64                                                  1:1.14.1-1.el7_4.ngx                                                  nginx
[root@node1 ~]# 

[root@node1 ~]# yum list updates nginx
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: centos.ustc.edu.cn
 * epel: mirrors.aliyun.com
 * extras: mirrors.shu.edu.cn
 * updates: mirrors.shu.edu.cn
Updated Packages
nginx.x86_64                                                                                                   1:1.14.1-1.el7_4.ngx                                                                                                    nginx
[root@node1 ~]# 

(2) 如何通過yum更新nginx

#檢測到有可用更新後,並且做過調查,就可以嘗試更新(生產環境不建議用最新),可以參考epel源最新版本
yum update nginx

三、升級好nginx後如何不中斷業務切換

官網關於nginx信號控制說明:http://nginx.org/en/docs/control.html

3.1、nginx相關的信號說明

nginx的管理進程(master process)可以接受指定的幾種系統信號:

  1. SIGINT, SIGTERM
    Shut down quickly.
    快速關閉;
  2. SIGHUP
    Reload configuration, start the new worker process with a new configuration, and gracefully shut down old worker processes.
    重載配置,新啓動的工作進程是重讀配置後的,然後舊的工作進程會平滑關閉,等待其處理完舊的連接會話後;
  3. SIGQUIT
    Shut down gracefully.
    平滑的關閉;
  4. SIGUSR1
    Reopen log files.
    重新打開日誌文件;
  5. SIGUSR2
    Upgrade the nginx executable on the fly.
    在線升級nginx執行程序文件;
  6. SIGWINCH
    Shut down worker processes gracefully.
    平滑關閉工作進程;

實際使用:
上邊是系統信號名稱,如果用配合nginx使用,使用形式爲kill -SIGNAL `cat /path/to/pidfile`
可用SIGNAL有:

(1) TERM, INT
fast shutdown.
快速關閉,不安全退出,強殺;
對應於系統信號:
SIGINT, SIGTERM
等價於kill -9,非特殊情況不建議使用;

(2) QUIT
graceful shutdown。
平滑關閉。
對應於系統信號:SIGQUIT
已經建立連接的請求會處理完畢後退出;

(3) HUP	
changing configuration, keeping up with a changed time zone (only for FreeBSD and Linux), starting new worker processes with a new configuration, graceful shutdown of old worker processes。
重載配置;
對應於系統信號:SIGHUP

(4) USR1
re-opening log files
重讀日誌文件。
對應於系統信號:SIGUSR1

(5) USR2	
upgrading an executable file。
升級可執行文件。
對應於系統信號:SIGUSR2

(6) WINCH
graceful shutdown of worker processes
平滑關閉nginx工作進程。
對應於系統信號:SIGWINCH

nginx的工作進程(worker process)可以接受指定的幾種系統信號:

  1. SIGTERM
    Shut down quickly.
    快速關閉;
  2. SIGQUIT
    Shut down gracefully.
    平滑關閉;
  3. SIGUSR1
    Reopen log files.
    重新打開日誌文件;

實際使用:
上邊是系統信號名稱,如果用配合nginx使用,使用形式爲kill -SIGNAL worker_pid
可用SIGNAL有(實際不需要手動對工作進程操作,因爲工作進程是由管理進程來管理的):


(1) TERM, INT	
fast shutdown
快速關閉。
先找到工作進程的pid,然後kill -TERM pid或kill -INT pid,這個是強制退出;

(2) QUIT
graceful shutdown
平滑關閉,操作方式同TERM或INT。

(3) USR1	
re-opening log files
重新打開日誌文件。

(4) WINCH	
abnormal termination for debugging (requires debug_points to be enabled)
調試異常終止(需要啓用debug_points)

nginx二進制程序可以通過-s 選項傳遞給nginx對應的參數,表示給nginx管理進程發送對應的信號。標準形式:

-s signal
其中signal可以爲stop,quit,reopen,reload;

stop:SIGTERM(快速關閉);
quit:SIGQUIT(平滑關閉);
reopen:SIGUSR1(重新打開日誌文件);
reload:SIGHUP(重載配置,新啓動的工作進程是重讀配置後的,然後舊的工作進程會平滑關閉,等待其處理完舊的連接會話後);

3.2、在線熱升級nginx可執行文件程序

  • 理論說明
    (1) 用新的nginx二進制程序文件替換舊的二進制程序文件;替換前建議先備份舊的二進制文件。
    替換的方式,如果是源碼包編譯安裝,可以直接cp替換或者其他方式替換。如果是rpm包或者yum直接更新升級,建議先用cp等工具備份一下舊的二進制程序文件;
    (2) 向nginx的管理進程發送USR2信號
    管理進程會把舊的pid文件備份成類似於nginx.pid.oldbin的形式。然後啓用一個新的管理進程,並且創建了對應的pid文件,新的管理進程會去啓動新的工作進程。此時,應該存在新舊兩個管理進程以及新舊管理進程啓動的工作進程,新舊工作進程此時會繼續接收處理請求;
    (3) 向舊的管理進程發送WINCH信號
    舊的管理進程會向舊的工作進程發送消息,請求平滑關閉它們,然後舊的工作進程將不再接受新的請求,如果正在處理舊的請求的要等處理完成後自動關閉,沒有在處理的舊的工作進程會直接退出。
    (4) 過一段時間後,通過工具確認沒有舊的工作進程在處理請求後,只剩下新舊管理進程和新的工作進程。
    默認,舊的管理進程不會關閉它監聽的套接字,如果後續有需要,它可以重新再管理他自己的工作進程,以便回滾處理連接請求。如果剛好由於某些原因,升級二進制程序不成功,要回滾的。
    比如因爲一些原因,新的執行文件不工作,要按照以下步驟回滾:
    a) 向舊的管理進程發送HUB信號
    信號接收後,舊的工作進程將會重新開啓工作進程(不會重讀加載配置)。此時,舊的工作進程開始工作後,要向之前新的管理進程發送QUIT信號,以便之前新的管理進程通過它之前管理的工作進程,讓它們平滑關閉。
    b) 發送TERM信號給新的工作進程。
    它會向它的工作進程發送消息讓它們立即退出,它們受到消息後會立即離開。如果進程由於某些原因不理解退出,KILL信號將會發送給它們迫使他們去退出。一旦新的工作管理進程退出後,舊的管理進程將會自動開啓新的工作進程。
    新的工作進程退出後,舊的工作進程會被nginx.pid.oldbin後綴給去掉。

如果升級成功,應該發送QUIT信號給舊的管理進程。

  • 實際操作
    (1) 備份nginx二進制程序文件
[root@node1 ~]# mv /usr/sbin/nginx{,.bak}
[root@node1 ~]# ls -l /usr/sbin/nginx
ls: cannot access /usr/sbin/nginx: No such file or directory
[root@node1 ~]# ls -l /usr/sbin/nginx.bak 
-rwxr-xr-x 1 root root 1307560 Apr 17  2018 /usr/sbin/nginx.bak
[root@node1 ~]# ps uax|grep nginx
root       2113  0.0  0.3  46428  1724 ?        Ss   16:22   0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx      2183  0.0  0.3  46828  1860 ?        S    16:36   0:00 nginx: worker process
nginx      2184  0.0  0.3  46828  1856 ?        S    16:36   0:00 nginx: worker process
root       2584  0.0  0.1 112640   960 pts/2    S+   17:44   0:00 grep --color=auto nginx
#這裏不用擔心由於二進制程備份導致處理連接連接請求失敗,因爲nginx的進程已經啓動,二進制程序已經
讀取到內存中去了。
#如果/usr/sbin/nginx-debug有用,最好也備份一下。

(2) yum升級nginx

[root@node1 ~]# yum update nginx
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: centos.ustc.edu.cn
 * epel: mirrors.aliyun.com
 * extras: mirrors.shu.edu.cn
 * updates: mirrors.shu.edu.cn
Resolving Dependencies
--> Running transaction check
---> Package nginx.x86_64 1:1.14.0-1.el7_4.ngx will be updated
---> Package nginx.x86_64 1:1.14.1-1.el7_4.ngx will be an update
--> Finished Dependency Resolution

Dependencies Resolved

============================================================================================================================================================================================================================================
 Package                                              Arch                                                  Version                                                              Repository                                            Size
============================================================================================================================================================================================================================================
Updating:
 nginx                                                x86_64                                                1:1.14.1-1.el7_4.ngx                                                 nginx                                                753 k

Transaction Summary
============================================================================================================================================================================================================================================
Upgrade  1 Package

Total download size: 753 k
Is this ok [y/d/N]: y
Downloading packages:
No Presto metadata available for nginx
nginx-1.14.1-1.el7_4.ngx.x86_64.rpm                                                                                                                                                                                  | 753 kB  00:00:24     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Updating   : 1:nginx-1.14.1-1.el7_4.ngx.x86_64                                                                                                                                                                                        1/2 
  Cleanup    : 1:nginx-1.14.0-1.el7_4.ngx.x86_64                                                                                                                                                                                        2/2 
  Verifying  : 1:nginx-1.14.1-1.el7_4.ngx.x86_64                                                                                                                                                                                        1/2 
  Verifying  : 1:nginx-1.14.0-1.el7_4.ngx.x86_64                                                                                                                                                                                        2/2 

Updated:
  nginx.x86_64 1:1.14.1-1.el7_4.ngx                                                                                                                                                                                                         

Complete!

此時nginx二進制程序爲新的二進制程序了。

[root@node1 ~]# which nginx
/usr/sbin/nginx
[root@node1 ~]# ls -l /usr/sbin/nginx
-rwxr-xr-x 1 root root 1307696 Nov  6 22:04 /usr/sbin/nginx
[root@node1 ~]# /usr/sbin/nginx -v
nginx version: nginx/1.14.1

(3) 向nginx的管理進程發送USR2信號

[root@node1 ~]# ls -l /var/run/nginx.pid 
-rw-r--r-- 1 root root 5 Nov 19 17:46 /var/run/nginx.pid
[root@node1 ~]# ps axw -o pid,ppid,user,%cpu,vsz,wchan,command | egrep '(nginx|PID)'
   PID   PPID USER     %CPU    VSZ WCHAN  COMMAND
  2656      1 root      0.0  46304 sigsus nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
  2658   2656 nginx     0.0  46708 ep_pol nginx: worker process
  2659   2656 nginx     0.0  46708 sleep_ nginx: worker process
  2685   2201 root      0.0 107888 inotif tailf /var/log/nginx/access.log
  2695   2387 root      0.0 112644 pipe_w grep -E --color=auto (nginx|PID)
[root@node1 ~]# cat /var/run/nginx.pid 
2656
[root@node1 ~]# kill -USR2 `cat /var/run/nginx.pid`
[root@node1 ~]# ps axw -o pid,ppid,user,%cpu,vsz,wchan,command | egrep '(nginx|PID)'
   PID   PPID USER     %CPU    VSZ WCHAN  COMMAND
  2656      1 root      0.0  46304 sigsus nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
  2658   2656 nginx     0.0  46708 ep_pol nginx: worker process
  2659   2656 nginx     0.0  46708 ep_pol nginx: worker process
  2685   2201 root      0.0 107888 inotif tailf /var/log/nginx/access.log
  2698   2656 root      0.0  46304 sigsus nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
  2699   2698 nginx     0.0  46708 ep_pol nginx: worker process
  2700   2698 nginx     0.0  46708 ep_pol nginx: worker process
  2702   2387 root      0.0 112644 -      grep -E --color=auto (nginx|PID)
[root@node1 ~]# ls -l /var/run/nginx.pid*
-rw-r--r-- 1 root root 5 Nov 19 17:53 /var/run/nginx.pid
-rw-r--r-- 1 root root 5 Nov 19 17:46 /var/run/nginx.pid.oldbin
[root@node1 ~]# cat /var/run/nginx.pid 
2698
[root@node1 ~]# cat /var/run/nginx.pid.oldbin 
2656

(4) 測試發信升級後,可以接受新的連接後。向舊的管理進程發送WINCH信號

#新連接請求,我就不截圖了,大概看一下業務沒有問題。能接受新請求,就可以開始下一步
[root@node1 ~]# kill -WINCH `cat /var/run/nginx.pid.oldbin`
[root@node1 ~]# ps axw -o pid,ppid,user,%cpu,vsz,wchan,command | egrep '(nginx|PID)'
   PID   PPID USER     %CPU    VSZ WCHAN  COMMAND
  2656      1 root      0.0  46304 sigsus nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
  2685   2201 root      0.0 107888 inotif tailf /var/log/nginx/access.log
  2698   2656 root      0.0  46304 sigsus nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
  2699   2698 nginx     0.0  46708 ep_pol nginx: worker process
  2700   2698 nginx     0.0  46708 ep_pol nginx: worker process
  2719   2387 root      0.0 112644 pipe_w grep -E --color=auto (nginx|PID)

(5) 確認舊的工作進程沒有都平滑退出後,可以看看升級是否成功。如果可以,直接向舊的
管理進程發送QUIT信號

[root@node1 ~]# ps axw -o pid,ppid,user,%cpu,vsz,wchan,command | egrep '(nginx|PID)'
   PID   PPID USER     %CPU    VSZ WCHAN  COMMAND
  2656      1 root      0.0  46304 sigsus nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
  2698   2656 root      0.0  46304 sigsus nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
  2699   2698 nginx     0.0  46708 ep_pol nginx: worker process
  2700   2698 nginx     0.0  46708 ep_pol nginx: worker process
  2724   2387 root      0.0 112644 pipe_w grep -E --color=auto (nginx|PID)
  #上邊已經沒有舊的工作進程在處理舊的請求了
 [root@node1 ~]# kill -QUIT `cat /var/run/nginx.pid.oldbin`
[root@node1 ~]# ps axw -o pid,ppid,user,%cpu,vsz,wchan,command | egrep '(nginx|PID)'
   PID   PPID USER     %CPU    VSZ WCHAN  COMMAND
  2698      1 root      0.0  46304 sigsus nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
  2699   2698 nginx     0.0  46708 ep_pol nginx: worker process
  2700   2698 nginx     0.0  46708 ep_pol nginx: worker process
  2733   2387 root      0.0 112644 pipe_w grep -E --color=auto (nginx|PID)
[root@node1 ~]# ls -l /var/run/nginx.pid 
-rw-r--r-- 1 root root 5 Nov 19 17:53 /var/run/nginx.pid
[root@node1 ~]# ls -l /var/run/nginx.pid*
-rw-r--r-- 1 root root 5 Nov 19 17:53 /var/run/nginx.pid
#爲了確認無誤,可以重載一下配置:
[root@node1 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@node1 ~]# nginx -s reload
[root@node1 ~]# ps axw -o pid,ppid,user,%cpu,vsz,wchan,command | egrep '(nginx|PID)'
   PID   PPID USER     %CPU    VSZ WCHAN  COMMAND
  2698      1 root      0.0  46436 sigsus nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
  2738   2698 nginx     0.0  46840 ep_pol nginx: worker process
  2739   2698 nginx     0.0  46840 ep_pol nginx: worker process
  2741   2387 root      0.0 112644 pipe_w grep -E --color=auto (nginx|PID)

申明:本測試是基於測試環境完成,如果有需要做類似的操作,生產環境操作請慎重。因爲nginx的可執行程序熱升級,可能會導致其他問題(鑑於學術有限,這裏沒法列出)。

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