1. 程式人生 > 其它 >nginx 熱部署

nginx 熱部署

nginx作為一個優秀的web伺服器,優秀的反向代理伺服器,並且nginx也支援高可用的特性,

nginx還支援熱部署的特點。

熱部署的特點 :再不重啟或者關閉程序的情況下,新的應用直接替換舊的應用

更換nginx的二進位制命令版本

熱部署大致流程

1.備份舊的程式 二進位制檔案 備份nginx命令,/opt/nginx1.8/sbin/nginx

2.編譯安裝新的二進位制檔案,覆蓋舊的二進位制檔案 (再裝一個版本的nginx,且替換舊的nginx命令)

3.傳送USR2訊號發給舊的master程序

4.傳送WINCH訊號給舊的master程序

5.傳送QUIT訊號給舊的master程序

環境準備

準備舊的nginx程式版本

[root@localhost ~]# nginx -v
nginx version: nginx/1.18.0

準備一個新的nginx程式版本

wget https://nginx.org/download/nginx-1.20.1.tar.gz

nginx熱部署操作

nginx工作模式是master-worker(包工頭---幹活工人)

nginx支援reload過載,僅僅是nginx的master程序,在檢查配置檔案正確之後,正確則更新,

錯誤則返回異常,正確的情況下也不會更改已經建立的worker,只會等待worker處理完畢請求之後,殺死舊的worker,

然後再從新的配置檔案中,執行新的worker(一旦更換了新的配置檔案,reload master 主程序,那麼手底下的工人也就會被換一批了)

nginx熱部署功能,在不影響使用者提體驗下,進行軟體版本升級,也就是不主動的殺死worker,就能夠更換軟體的二進位制命令

檢視當前機器環境的nginx版本

[root@localhost ~]# nginx -v
nginx version: nginx/1.18.0

備份舊的目錄

[root@localhost sbin]# mv nginx nginx1.8

檢查舊的二進位制命令的編譯引數

[root@localhost sbin]# nginx1.8 -V
nginx version: nginx/1.18.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/opt/nginx1.8 --with-http_ssl_module --with-http_flv_module --with-http_gzip_static_module --with-http_stub_status_module --with-threads --with-file-aio

下載編譯安裝新版本的nginx

wget https://nginx.org/download/nginx-1.20.1.tar.gz

tar -zxvf nginx-1.20.1.tar.gz

進行編譯安裝

cd nginx-1.2.0.1

新版本的nginx編譯引數和舊的保持一致

./configure --prefix=/opt/nginx1.8 --with-http_ssl_module --with-http_flv_module --with-http_gzip_static_module --with-http_stub_status_module --with-threads --with-file-aio

編譯三部曲 後兩部

make

make install 或者 make && make install

檢查新版的nginx資訊,發現此時已經有2個版本的nginx命令

[root@localhost nginx1.8]# cd sbin/

[root@localhost sbin]# ls
nginx nginx1.8
[root@localhost sbin]# ./nginx -v
nginx version: nginx/1.20.1

再次檢查當前系統的nginx狀態

root 5959 1 0 15:56 ? 00:00:00 nginx: master process nginx1.8
www 5960 5959 0 15:56 ? 00:00:00 nginx: worker process
www 5961 5959 0 15:56 ? 00:00:00 nginx: worker process

通過pid ppid可以驗證,worker process是由master process建立的

此時傳送一個USR2訊號給 舊的master process,作用是使得ngixn舊的版本停止接收使用者請求,

並且切換為新的nginx版本

執行如下命令,給舊的nginx傳送訊號

kill -USR2 `cat /opt/nginx1.8/logs/nginx.pid`

當執行完畢上述的命令,ningx-master舊的,首先會重新命名它的pid檔案

然後新增上.oldbin字尾,然後會在啟動一個新的master主程序,以及worker,

使用的是新版本的nginx二進位制命令,此時新的nginx就能夠自動的接收使用者發來的請求,

過度到新的nginx-worker工作程序上,因此實現了一個平滑過度

此時再次檢查新的nginx程序狀態

[root@localhost sbin]# ps -ef |grep nginx
root 5959 1 0 15:56 ? 00:00:00 nginx: master process nginx1.8
www 5960 5959 0 15:56 ? 00:00:00 nginx: worker process
www 5961 5959 0 15:56 ? 00:00:00 nginx: worker process
root 5963 5959 0 15:56 ? 00:00:00 nginx: master process nginx1.8
www 5964 5963 0 15:56 ? 00:00:00 nginx: worker process
www 5965 5963 0 15:56 ? 00:00:00 nginx: worker process
root 5969 2517 0 15:56 pts/0 00:00:00 grep --color=auto nginx

再檢查一下,新的pid檔案資訊

[root@localhost nginx1.8]# cd logs/
[root@localhost logs]# ls
access.log error.log nginx.pid nginx.pid.oldbin
[root@localhost logs]# cat nginx.pid.oldbin
5959
[root@localhost logs]# cat nginx.pid
5963

此時傳送WINCH訊號,給舊的master程序,讓舊的master程序優雅的退出

kill -WINCH `cat /opt/nginx1.8/logs/nginx.pid.oldbin`

此時nginx服務一切正常,就可以幹掉舊的master主程序了