1. nginx平滑升级
当我们开发了一个新的nginx模块,需要升级nginx binary时,需要进行以下的步骤:
a) 替换老的nginx binary
[root@lg-miui-file-mfs09 sbin]# mv nginx nginx.old
[root@lg-miui-file-mfs09 sbin]# scp guojun1@10.237.92.30:/usr/local/nginx/sbin/nginx .
guojun1@10.237.92.30's password:
nginx
b) 向old master process 发送SIGUSR2信号
[root@lg-miui-file-mfs09 sbin]# kill -s SIGUSR2 16236
nginx.pid文件会被重命名为nginx.pid.oldbin,并且执行新的nginx binary文件,郑州牛皮癣专科医院此时新老版本会同时运行:
[root@lg-miui-file-mfs09 sbin]# ll ../logs/
total 14392
-rw-r--r-- 1 guojun1 500 893 May 30 10:58 error.log
-rw-r--r-- 1 guojun1 500 5617929 May 12 13:12 nginx-access.log
-rw-r--r-- 1 guojun1 500 9102050 May 13 22:39 nginx-error.log
-rw-r--r-- 1 root root 6 May 30 10:58 nginx.pid
-rw-r--r-- 1 root root 6 May 30 10:58 nginx.pid.oldbin
[root@lg-miui-file-mfs09 sbin]# ps aux | grep nginx
root 16236 0.0 0.0 92920 3064 ? Ss 10:58 0:00 nginx: master process ./nginx
root 16237 0.0 0.0 94992 3988 ? S 10:58 0:00 nginx: worker process
root 16238 0.0 0.0 94992 3988 ? S 10:58 0:00 nginx: worker process
root 16239 0.0 0.0 94992 3988 ? S 10:58 0:00 nginx: worker process
root 16385 0.0 0.0 92924 6072 ? S 10:58 0:00 nginx: master process ./nginx
root 16386 0.0 0.0 94996 3908 ? S 10:58 0:00 nginx: worker process
root 16387 0.0 0.0 94996 3980 ? S 10:58 0:00 nginx: worker process
root 16388 0.0 0.0 94996 3980 ? S 10:58 0:00 nginx: worker process
root 16445 0.0 0.0 103244 852 pts/4 S+ 10:58 0:00 grep nginx
c) 向old master process发送SIGWINCH
此时,master会”优雅地”关闭它的工作进程:
[root@lg-miui-file-mfs09 sbin]# kill -s SIGWINCH 16236
[root@lg-miui-file-mfs09 sbin]# ps aux | grep nginx
root 16236 0.0 0.0 92920 3072 ? Ss 10:58 0:00 nginx: master process ./nginx
root 16385 0.0 0.0 92924 6072 ? S 10:58 0:00 nginx: master process ./nginx
root 16386 0.0 0.0 94996 3908 ? S 10:58 0:00 nginx: worker process
root 16387 0.0 0.0 94996 3980 ? S 10:58 0:00 nginx: worker process
root 16388 0.0 0.0 94996 3980 ? S 10:58 0:00 nginx: worker process
d) 关闭old master
如果查看log发送更新成功,可以关闭old master,此时向old master进程发送SIGQUIT,只留下新的server运行。
[root@lg-miui-file-mfs09 sbin]# kill -s SIGQUIT 16236
[root@lg-miui-file-mfs09 sbin]# ps aux | grep nginx
root 16385 0.0 0.0 92924 6072 ? S 10:58 0:00 nginx: master process ./nginx
root 16386 0.0 0.0 94996 3908 ? S 10:58 0:00 nginx: worker process
root 16387 0.0 0.0 94996 3980 ? S 10:58 0:00 nginx: worker process
root 16388 0.0 0.0 94996 3980 ? S 10:58 0:00 nginx: worker process
2. 回滚
如果我们在关闭老的工作进行之后查看log发现新的nginx server运行有问题,我们可以回滚到老的版本,其步骤如下:
a) 启动老的worker 进程
可以通过向old master 进程发送SIGHUP信号,让其启动老的worker 进程。
[root@lg-miui-file-mfs09 sbin]# ps aux | grep nginx
root 16385 0.0 0.0 92924 6072 ? S 10:58 0:00 nginx: master process ./nginx
root 44583 0.0 0.0 92924 6076 ? S 11:57 0:00 nginx: master process ./nginx
root 44584 0.0 0.0 94996 3984 ? S 11:57 0:00 nginx: worker process
root 44585 0.0 0.0 94996 3984 ? S 11:57 0:00 nginx: worker process
root 44586 0.0 0.0 94996 3984 ? S 11:57 0:00 nginx: worker process
root 44757 0.0 0.0 94996 3908 ? S 11:58 0:00 nginx: worker process
root 44758 0.0 0.0 94996 3980 ? S 11:58 0:00 nginx: worker process
root 44759 0.0 0.0 94996 3980 ? S 11:58 0:00 nginx: worker process
b) 关闭新的master进程和worker进程
向新的master进程发送SIGQUIT信号,郑州牛皮癣医院关闭新的master进程和worker进程;
[root@lg-miui-file-mfs09 sbin]# kill -s SIGQUIT 44583
[root@lg-miui-file-mfs09 sbin]# ps aux | grep nginx
root 16385 0.0 0.0 92924 6072 ? S 10:58 0:00 nginx: master process ./nginx
root 44757 0.0 0.0 94996 3908 ? S 11:58 0:00 nginx: worker process
root 44758 0.0 0.0 94996 3980 ? S 11:58 0:00 nginx: worker process
root 44759 0.0 0.0 94996 3980 ? S 11:58 0:00 nginx: worker process
如果新的master进程由于某些原因hung住了没有关闭,可以向它们发送SIGTERM或者SIGKILL信号。当新的master进程退出后,老的master进程会将nginx.pid.oldbin文件重命名为nginx.pid.
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29661313/viewspace-1175205/ ,如需转载,请注明出处,否则将追究法律责任。