nginx编译安装和平滑升级

1.什么是平滑升级
在进行服务版本升级的时候,对于用户访问体验无感知、不
会造成服务中断。
2.平滑升级的原理

在这里插入图片描述
3.如何实现Nginx平滑升级思路(建议准备一个大文件持续下
载验证升级是否会影响业务)

1.下载新版本Nginx
2.了解原旧版本Nginx编译参数
3.将旧的nginx二进制文件进行备份,然后替换成为新的
nginx二进制文件
4.向旧的Nginx的Master进程发送USR2信号
4.1)旧的master进程的pid文件添加后缀.oldbin
4.2)master进程会用新nginx二进制文件启动新的
master进程。
5.向旧的master进程发送WINCH信号,旧的worker子进
程优雅退出。
6.向旧的master进程发送QUIT信号,旧的master进程就
退出了。
2.平滑升级实践
1.获取原有环境的编译参数

--prefix=/etc/nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module

2.在新的环境下重新下载软件包安装
1)安装Nginx所需依赖包

[root@web01 ~]# yum install gcc redhat-rpmconfig \
libxslt-devel gd-devel perl-ExtUtils-Embed \
geoip-devel gperftools-devel pcre-devel
openssl-devel

2)下载并编译Nginx

[root@web ~]# wget
http://nginx.org/download/nginx-1.16.1.tar.gz
[root@web ~]# tar xf nginx-1.16.1.tar.gz
[root@web ~]# cd nginx-1.16.1/
#编译
[root@web ~]# ./configure --prefix=/etc/nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module
[root@web ~]# make

3)将旧的nginx二进制文件进行备份,然后替换成为新的nginx二进制文件

[root@nfs nginx-1.16.1]# mv /usr/sbin/nginx
/usr/sbin/nginx.old
[root@nfs nginx-1.16.1]# cp objs/nginx
/usr/sbin/nginx

4)向旧的Nginx的Master进程发送USR2信号。( 平滑升级二进制可执行文件 )

[root@nfs nginx-1.16.1]# ps -ef |grep nginx
root 20848 1 0 10:21 ? 00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx 20863 20848 0 10:22 ? 00:00:00 nginx: worker process
nginx 20864 20848 0 10:22 ? 00:00:00 nginx: worker process

#发送信号
[root@nfs nginx-1.16.1]# kill -USR2 20848

#pid文件会自动添加.oldbin后缀,该文件中记录的是旧master的pid进程号
[root@nfs nginx-1.16.1]# cat /var/run/nginx.pid.oldbin
20848
#新老master共存了
[root@nfs nginx-1.16.1]# ps -ef |grep nginx
root 20848 1 0 10:21 ? 00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf(OLD)
nginx 20863 20848 0 10:22 ? 00:00:00 nginx: worker process
nginx 20864 20848 0 10:22 ? 00:00:00 nginx: worker process
root 24971 20848 0 11:37 ? 00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf(NEW)
nginx 24972 24971 0 11:37 ? 00:00:00 nginx: worker process
nginx 24973 24971 0 11:37 ? 00:00:00 nginx: worker process
#验证站点是否正常

5)向旧的master进程发送WINCH信号,旧的worker子进程优
雅退出。

[root@nfs nginx-1.16.1]# kill -WINCH 20848
[root@nfs nginx-1.16.1]# ps -ef |grep nginx
root 20848 1 0 10:21 ? 00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
root 24971 20848 0 11:37 ? 00:00:00 nginx: master process
/usr/sbin/nginx -c /etc/nginx/nginx.conf 
nginx 24972 24971 0 11:37 ? 00:00:00 nginx: worker process
nginx 24973 24971 0 11:37 ? 00:00:00 nginx: worker process

6)向旧的master进程发送QUIT信号,旧的master进程就退出
了。

[root@nfs nginx-1.16.1]# kill -QUIT 20848
[root@nfs nginx-1.16.1]# ps -ef |grep nginx
root 24971 1 0 11:37 ? 00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx 24972 24971 0 11:37 ? 00:00:00 nginx: worker process
nginx 24973 24971 0 11:37 ? 00:00:00 nginx: worker process

3.Nginx平滑回滚实践
回滚思路:
  1.替换 nginx 二进制文件
  2.向旧的 master 发送USR2信号
  3.向旧的 master 发送SIGWINCH
  4.向旧的 master 发送QUIT
1)替换nginx二进制文件

[root@web ~]# mv /usr/sbin/nginx /usr/sbin/nginx-1.16
[root@web ~]# mv /usr/sbin/nginx.old /usr/sbin/nginx

2)向旧的master发送USR2信号

[root@web ~]# ps -ef |grep nginx
root 24971 1 0 11:37 ? 00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx 25124 24971 0 11:48 ? 00:00:00 nginx: worker process
nginx 25125 24971 0 11:48 ? 00:00:00 nginx: worker process
[root@web ~]# kill -USR2 24971
[root@web ~]# ps -ef |grep nginx
root 24971 1 0 11:37 ? 00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf (OLD 1.16)
nginx 25124 24971 0 11:48 ? 00:00:00 nginx: worker process
nginx 25125 24971 0 11:48 ? 00:00:00 nginx: worker process
root 25163 24971 1 11:50 ? 00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf (NEW 1.14)
nginx 25164 25163 0 11:50 ? 00:00:00 nginx: worker process
nginx 25165 25163 0 11:50 ? 00:00:00 nginx: worker process

3)向旧的master发送WINCH信号,优雅关闭旧的Worker工作进程。

[root@web ~]# kill -WINCH 24971
[root@web ~]# ps -ef |grep nginx
root 24971 1 0 11:37 ? 00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx 25124 24971 0 11:48 ? 00:00:00 nginx: worker process is shutting down
root 25163 24971 0 11:50 ? 00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
|  |  |
|--|--|
|  |  |

nginx 25164 25163 0 11:50 ? 00:00:00 nginx: worker process
nginx 25165 25163 0 11:50 ? 00:00:00 nginx: worker process

4)向旧的master发送QUIT信号,退出该master进程。

[root@web ~]# kill -QUIT 24971
[root@web ~]# ps -ef |grep nginx
root 25163 1 0 11:50 ? 00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx 25164 25163 0 11:50 ? 00:00:00 nginx: worker process
nginx 25165 25163 0 11:50 ? 00:00:00 nginx: worker process

对应升级或回滚对应的信号

信号含义
QUIT优雅关闭、quit
HUP优雅重启、reload
USR1重新打开日志文件、reopen
USR2平滑升级可执行的二进制程序
WINCH平滑关闭Worker进程
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值