在線不停機升級Nginx圖解
Nginx升級1.6----->1.7
(1)查看一下我的老版本的nginx的版本信息和模塊信息,因爲升級要把老版本的模塊信息也需要編譯進去
[root@www nginx-1.17.9]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.16.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)
configure arguments: --prefix=/usr/local/nginx
(2)先來備份一下舊版本的nginx的二進制文件
[root@www src]# cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.old
(3)編譯nginx1.17版本,注意這裏只需要make,不用多此一舉make install,因爲我們只需要編譯好的新版本的nginx二進制文件
[root@www src]# cd /usr/src/nginx-1.17.9
[root@www nginx-1.17.9]# ./configure --prefix=/usr/local/nginx
[root@www nginx-1.17.9]# make
[root@www nginx-1.17.9]# ls objs/ --這個我們編譯好的最新版本的nginx的二進制文件
autoconf.err Makefile nginx nginx.8 ngx_auto_config.h ngx_auto_headers.h ngx_modules.c ngx_modules.o src
(4)複製新版本Nginx1.17二進制文件到現有Nginx工作目錄
將編譯好最新版本的nginx二進制文件替換正在運行nginx進程所使用的二進制文件,在大部分場景下面,新編譯的nginx二進制文件所指定的配置選項,比如配置文件的目錄在哪裏,日誌文件目錄在哪裏,必須保持和老的nginx是一致的,否則會導致無法複用nginx配置文件。所以在替換的時候注意備份,在Linux當中,當你正在覆蓋一個正在使用的文件時候加上-f,即cp -f才能替換掉。
[root@www nginx-1.17.9]# cp -f objs/nginx /usr/local/nginx/sbin/nginx
cp: overwrite ?.usr/local/nginx/sbin/nginx?. y
[root@www nginx-1.17.9]# ll /usr/local/nginx/sbin/
-rwxr-xr-x 1 root root 3849136 Mar 18 22:07 nginx --這個是最新版本的nginx的二進制文件
-rwxr-xr-x 1 root root 3825496 Mar 18 21:56 nginx.old --這個是舊版本的nginx的配置文件
(5)給當前的Nginx發送USR2信號(這個時候新的master進程使用是新的nginx二進制文件啓動的)
給正在運行nginx的master進程發送一個信號,告訴master我們要熱部署了,做一次版本升級,這個信號是USR2,USR2這個信號就是來傳遞熱部署的信號
[root@www nginx-1.17.9]# ps -ef | grep nginx
root 55384 1 0 19:02 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 63376 55384 0 21:24 ? 00:00:00 nginx: worker process
[root@www nginx-1.17.9]# kill -USR2 55384
[root@www nginx-1.17.9]# ps -ef | grep nginx --可以看到老的master進程新啓動了一個新的master進程,這個新的master進程使用了我們剛剛複製過來的最新版的nginx二進制文件,老的worker也在運行,新的master會新生成新的worker,老的worker會平滑的將所有請求過渡到新的worker。老的worker不會監聽80這種端口了,新的請求和連接會進入到新的nginx進程當中
root 55384 1 0 19:02 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 63376 55384 0 21:24 ? 00:00:00 nginx: worker process
root 68250 55384 0 22:12 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 68251 68250 0 22:12 ? 00:00:00 nginx: worker process
換句話總結就是:
此時Nginx會新開啓一個master進程,這個master進程會生成新的worker進程,這就是升級後的Nginx進程,此時老的進程不會自動退出,但是當接收到新的請求不作處理而是交給新的進程處理。
(6)升級成功,停止舊的worker進程和master進程
向老的master進程發送信號WINCH信號,告訴它優雅的關閉其worker進程
[root@www nginx-1.17.9]# kill -WINCH 55384
[root@www nginx-1.17.9]# ps -ef | grep nginx --可以看到老的worker進程已經退出了,老的master進程還在,這就說明所有的請求已經切換到新升級好的nginx中了
root 55384 1 0 19:02 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
root 68250 55384 0 22:12 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 68251 68250 0 22:12 ? 00:00:00 nginx: worker process
kill -QUIT old_master_PID
[root@www nginx-1.17.9]# /usr/local/nginx/sbin/nginx -V --可以看到升級到新的版本了
nginx version: nginx/1.17.9
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)
configure arguments: --prefix=/usr/local/nginx
注:如果在版本升級完成後,沒有任何問題,需要關閉老的master進程的話,可以使用下面的命令:
kill -QUIT old_master_PID
(6)升級過程中出現問題,回退到老版本
之前舊版本的主進程不要kill關閉,以防萬一升級失敗還可以進行回滾,向老的master進程發送命令,再將新版本關掉。所以第五步的老的master進程是不會自動退出的,保留着,允許我們做版本的回退
[root@www nginx-1.17.9]# cp -f /usr/local/nginx/sbin/nginx.old /usr/local/nginx/sbin/nginx
--將備份好的舊版本的腳本還原
cp: overwrite ?.usr/local/nginx/sbin/nginx?. y
[root@www nginx-1.17.9]# kill -HUP 55384 -- 重新拉起老的worker進程(喚醒舊版本的master進程,使之產生新的worker進程)
[root@www nginx-1.17.9]# ps -ef | grep nginx
root 55384 1 0 19:02 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
root 68250 55384 0 22:12 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 68251 68250 0 22:12 ? 00:00:00 nginx: worker process
nginx 69435 55384 0 22:36 ? 00:00:00 nginx: worker process
[root@www nginx-1.17.9]# kill -WINCH 68250 --關閉新版本的worker進程
[root@www nginx-1.17.9]# ps -ef | grep nginx
root 55384 1 0 19:02 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
root 68250 55384 0 22:12 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 69435 55384 0 22:36 ? 00:00:00 nginx: worker process
[root@www ~]# kill -QUIT 68250 --關閉新版本的master進程
[root@www ~]# ps -ef | grep nginx
root 55384 1 0 17:05 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 69435 55384 0 20:40 ? 00:00:00 nginx: worker process
至此回退完成!