Nginx 热部署
热部署(Hot Deployment)是指在应用程序运行过程中,无需停止应用程序,直接将修改后的代码部署到应用程序中,并立即生效的一种部署方式。
- 将旧的nginx文件替换成新的程序文件
- 向master进程发送sigusr2信号
- master进程修改pid文件,加后缀.oldbin
- master进程用新的pid文件启动新的master进程,此时就会有新的master进程和旧的mater进程同时存在
- 向旧的mater进程发送SIGWINCH信号,旧的worker子进程退出
- 回滚情行:向旧的master进程发送SIGHUP信号,向新的master进程发送SIGQUIT信号
一、升级案例
[root@localhost sbin]#
[root@localhost sbin]# pwd
/opt/software/middleware/nginx-install-1.22.1/sbin
[root@localhost sbin]#
[root@localhost sbin]# ps -ef | grep nginx
root 1844 1 0 Apr02 ? 00:00:00 nginx: master process ./sbin/nginx
nobody 1845 1844 0 Apr02 ? 00:00:00 nginx: worker process
nobody 1846 1844 0 Apr02 ? 00:00:00 nginx: worker process
nobody 1847 1844 0 Apr02 ? 00:00:00 nginx: worker process
nobody 1848 1844 0 Apr02 ? 00:00:00 nginx: worker process
root 5388 3957 0 01:19 pts/1 00:00:00 grep --color=auto nginx
[root@localhost sbin]#
[root@localhost sbin]# kill -s SIGUSR2 1844
[root@localhost sbin]#
[root@localhost sbin]#
[root@localhost sbin]#
[root@localhost sbin]# ps -ef | grep nginx
root 1844 1 0 Apr02 ? 00:00:00 nginx: master process ./sbin/nginx
nobody 1845 1844 0 Apr02 ? 00:00:00 nginx: worker process
nobody 1846 1844 0 Apr02 ? 00:00:00 nginx: worker process
nobody 1847 1844 0 Apr02 ? 00:00:00 nginx: worker process
nobody 1848 1844 0 Apr02 ? 00:00:00 nginx: worker process
root 5412 1844 0 01:20 ? 00:00:00 nginx: master process ./sbin/nginx
nobody 5413 5412 0 01:20 ? 00:00:00 nginx: worker process
nobody 5414 5412 0 01:20 ? 00:00:00 nginx: worker process
nobody 5415 5412 0 01:20 ? 00:00:00 nginx: worker process
nobody 5416 5412 0 01:20 ? 00:00:00 nginx: worker process
root 5420 3957 0 01:20 pts/1 00:00:00 grep --color=auto nginx
[root@localhost sbin]# cd ..
[root@localhost nginx-install-1.22.1]# ls
client_body_temp conf fastcgi_temp html logs proxy_temp sbin scgi_temp uwsgi_temp
[root@localhost nginx-install-1.22.1]# cd logs/
[root@localhost logs]# ll
total 16
-rw-r--r-- 1 root root 601 Mar 31 07:15 access.log
-rw-r--r-- 1 root root 543 Apr 3 01:20 error.log
-rw-r--r-- 1 root root 5 Apr 3 01:20 nginx.pid
-rw-r--r-- 1 root root 5 Apr 2 19:38 nginx.pid.oldbin
[root@localhost logs]#
[root@localhost logs]#
[root@localhost logs]#
[root@localhost logs]# cat nginx.pid
5412
[root@localhost logs]#
[root@localhost logs]#
[root@localhost logs]# cat nginx.pid.oldbin
1844
关闭旧的master进程的子work进程: kill -s SIGWINCH 1844
执行后,只剩旧的master进程,和新的master进程以及新的work进程。保留旧的master进程可以验证后恢复旧的环境.如果确定没问题后
在执行 kill -s SIGQUIT 1844 关闭旧的master进程。至此就完成了nginx的平滑升级
[root@localhost nginx-install-1.22.1]# kill -s SIGWINCH 1844
[root@localhost nginx-install-1.22.1]#
[root@localhost nginx-install-1.22.1]#
[root@localhost nginx-install-1.22.1]# ps -ef | grep nginx
root 1844 1 0 Apr02 ? 00:00:00 nginx: master process ./sbin/nginx
root 5412 1844 0 01:20 ? 00:00:00 nginx: master process ./sbin/nginx
nobody 5413 5412 0 01:20 ? 00:00:00 nginx: worker process
nobody 5414 5412 0 01:20 ? 00:00:00 nginx: worker process
nobody 5415 5412 0 01:20 ? 00:00:00 nginx: worker process
nobody 5416 5412 0 01:20 ? 00:00:00 nginx: worker process
root 5491 3957 0 01:26 pts/1 00:00:00 grep --color=auto nginx
[root@localhost nginx-install-1.22.1]#
[root@localhost nginx-install-1.22.1]#
[root@localhost nginx-install-1.22.1]# kill -s SIGQUIT 1844
[root@localhost nginx-install-1.22.1]# ps -ef | grep nginx
root 5412 1 0 01:20 ? 00:00:00 nginx: master process ./sbin/nginx
nobody 5413 5412 0 01:20 ? 00:00:00 nginx: worker process
nobody 5414 5412 0 01:20 ? 00:00:00 nginx: worker process
nobody 5415 5412 0 01:20 ? 00:00:00 nginx: worker process
nobody 5416 5412 0 01:20 ? 00:00:00 nginx: worker process
root 5518 3957 0 01:28 pts/1 00:00:00 grep --color=auto nginx
二、回滚案例
下面是对 5412的nginx master进程进行热部署升级时,发现新的master进程不符合预期进行回滚的操作。
- 开启新的master进程:kill -s SIGUSR2 5412
- 关闭旧的work进程: kill -s SIGWINCH 5412
- 恢复旧的work进程:kill -s SIGHUP 5412
- 关闭新开启master进程:kill -s SIGQUIT 5558
[root@localhost nginx-install-1.22.1]# ps -ef | grep nginx
root 5412 1 0 01:20 ? 00:00:00 nginx: master process ./sbin/nginx
nobody 5413 5412 0 01:20 ? 00:00:00 nginx: worker process
nobody 5414 5412 0 01:20 ? 00:00:00 nginx: worker process
nobody 5415 5412 0 01:20 ? 00:00:00 nginx: worker process
nobody 5416 5412 0 01:20 ? 00:00:00 nginx: worker process
root 5557 3957 0 01:31 pts/1 00:00:00 grep --color=auto nginx
[root@localhost nginx-install-1.22.1]#
[root@localhost nginx-install-1.22.1]#
[root@localhost nginx-install-1.22.1]# kill -s SIGUSR2 5412
[root@localhost nginx-install-1.22.1]#
[root@localhost nginx-install-1.22.1]# ps -ef | grep nginx
root 5412 1 0 01:20 ? 00:00:00 nginx: master process ./sbin/nginx
nobody 5413 5412 0 01:20 ? 00:00:00 nginx: worker process
nobody 5414 5412 0 01:20 ? 00:00:00 nginx: worker process
nobody 5415 5412 0 01:20 ? 00:00:00 nginx: worker process
nobody 5416 5412 0 01:20 ? 00:00:00 nginx: worker process
root 5558 5412 0 01:32 ? 00:00:00 nginx: master process ./sbin/nginx
nobody 5559 5558 0 01:32 ? 00:00:00 nginx: worker process
nobody 5560 5558 0 01:32 ? 00:00:00 nginx: worker process
nobody 5561 5558 0 01:32 ? 00:00:00 nginx: worker process
nobody 5562 5558 0 01:32 ? 00:00:00 nginx: worker process
root 5564 3957 0 01:32 pts/1 00:00:00 grep --color=auto nginx
[root@localhost nginx-install-1.22.1]#
[root@localhost nginx-install-1.22.1]# kill -s SIGWINCH 5412
[root@localhost nginx-install-1.22.1]# ps -ef | grep nginx
root 5412 1 0 01:20 ? 00:00:00 nginx: master process ./sbin/nginx
root 5558 5412 0 01:32 ? 00:00:00 nginx: master process ./sbin/nginx
nobody 5559 5558 0 01:32 ? 00:00:00 nginx: worker process
nobody 5560 5558 0 01:32 ? 00:00:00 nginx: worker process
nobody 5561 5558 0 01:32 ? 00:00:00 nginx: worker process
nobody 5562 5558 0 01:32 ? 00:00:00 nginx: worker process
root 5574 3957 0 01:32 pts/1 00:00:00 grep --color=auto nginx
[root@localhost nginx-install-1.22.1]#
[root@localhost nginx-install-1.22.1]#
[root@localhost nginx-install-1.22.1]# kill -s SIGHUP 5412
[root@localhost nginx-install-1.22.1]# ps -ef | grep nginx
root 5412 1 0 01:20 ? 00:00:00 nginx: master process ./sbin/nginx
root 5558 5412 0 01:32 ? 00:00:00 nginx: master process ./sbin/nginx
nobody 5559 5558 0 01:32 ? 00:00:00 nginx: worker process
nobody 5560 5558 0 01:32 ? 00:00:00 nginx: worker process
nobody 5561 5558 0 01:32 ? 00:00:00 nginx: worker process
nobody 5562 5558 0 01:32 ? 00:00:00 nginx: worker process
nobody 5575 5412 0 01:33 ? 00:00:00 nginx: worker process
nobody 5576 5412 0 01:33 ? 00:00:00 nginx: worker process
nobody 5577 5412 0 01:33 ? 00:00:00 nginx: worker process
nobody 5578 5412 0 01:33 ? 00:00:00 nginx: worker process
root 5580 3957 0 01:33 pts/1 00:00:00 grep --color=auto nginx
[root@localhost nginx-install-1.22.1]#
[root@localhost nginx-install-1.22.1]#
[root@localhost nginx-install-1.22.1]# kill -s SIGQUIT 5558
[root@localhost nginx-install-1.22.1]# ps -ef | grep nginx
root 5412 1 0 01:20 ? 00:00:00 nginx: master process ./sbin/nginx
nobody 5575 5412 0 01:33 ? 00:00:00 nginx: worker process
nobody 5576 5412 0 01:33 ? 00:00:00 nginx: worker process
nobody 5577 5412 0 01:33 ? 00:00:00 nginx: worker process
nobody 5578 5412 0 01:33 ? 00:00:00 nginx: worker process
root 5590 3957 0 01:33 pts/1 00:00:00 grep --color=auto nginx