Nginx 专题-热部署平滑升级

Nginx 热部署

热部署(Hot Deployment)是指在应用程序运行过程中,无需停止应用程序,直接将修改后的代码部署到应用程序中,并立即生效的一种部署方式。

  1. 将旧的nginx文件替换成新的程序文件
  2. 向master进程发送sigusr2信号
  3. master进程修改pid文件,加后缀.oldbin
  4. master进程用新的pid文件启动新的master进程,此时就会有新的master进程和旧的mater进程同时存在
  5. 向旧的mater进程发送SIGWINCH信号,旧的worker子进程退出
  6. 回滚情行:向旧的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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值