Nginx不管是对于运维、开发、仍是测试来讲,都是平常工做须要掌握的一个知识点,以前也写过很多关于Nginx相关的文章:
有兴趣的能够关注【民工哥技术之路】微信公众号看看上面的文章。html
今天,咱们来聊一聊,在企业实际生产环境中常常遇到的一个状况,升级Nginx到新的版本和如何回滚至旧版本。nginx
一、环境介绍性能优化
今天准备的两个nginx版本以下:服务器
[root@nginx ~]# cd /download/nginx/ [root@nginx nginx]# ll total 1952 -rw-r--r-- 1 root root 981687 Oct 17 2017 nginx-1.12.2.tar.gz -rw-r--r-- 1 root root 1015384 Dec 4 09:58 nginx-1.14.2.tar.gz
二、编译安装新旧版本微信
编译安装nginx-1.12.2负载均衡
[root@nginx nginx]# tar zxf nginx-1.12.2.tar.gz [root@nginx nginx]# cd nginx-1.12.2 [root@nginx nginx-1.12.2]# ./configure --prefix=/usr/local/nginx-1.12.2 [root@nginx nginx-1.12.2]# echo $? 0 [root@nginx nginx-1.12.2]# make && make install [root@nginx nginx-1.12.2]# echo $? 0 [root@nginx nginx-1.12.2]# ll /usr/local/nginx-1.12.2/ total 0 drwxr-xr-x 2 root root 333 Mar 1 09:01 conf drwxr-xr-x 2 root root 40 Mar 1 09:01 html drwxr-xr-x 2 root root 6 Mar 1 09:01 logs drwxr-xr-x 2 root root 19 Mar 1 09:01 sbin
编译安装nginx-1.14.2运维
[root@nginx ~]# cd /download/nginx/ [root@nginx nginx]# tar zxf nginx-1.14.2.tar.gz [root@nginx nginx]# cd nginx-1.14.2 [root@nginx nginx-1.14.2]# ./configure --prefix=/usr/local/nginx-1.14.2 [root@nginx nginx-1.14.2]# echo $? 0 [root@nginx nginx-1.14.2]# make && make install [root@nginx nginx-1.14.2]# echo $? 0 [root@nginx nginx-1.14.2]# ls -l /usr/local/nginx-1.14.2/ total 0 drwxr-xr-x 2 root root 333 Mar 1 09:03 conf drwxr-xr-x 2 root root 40 Mar 1 09:03 html drwxr-xr-x 2 root root 6 Mar 1 09:03 logs drwxr-xr-x 2 root root 19 Mar 1 09:03 sbin
到这里,两个版本的nginx软件已经部署完成。性能
三、启动旧版本nginx测试
[root@nginx ~]# /usr/local/nginx-1.12.2/sbin/nginx -t nginx: the configuration file /usr/local/nginx-1.12.2/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx-1.12.2/conf/nginx.conf test is successful [root@nginx ~]# /usr/local/nginx-1.12.2/sbin/nginx [root@nginx ~]# ps -ef|grep nginx root 6324 1 0 09:06 ? 00:00:00 nginx: master process /usr/local/nginx-1.12.2/sbin/nginx nobody 6325 6324 0 09:06 ? 00:00:00 nginx: worker process root 6327 1244 0 09:06 pts/0 00:00:00 grep --color=auto nginx [root@nginx ~]# lsof -i :80 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME nginx 6324 root 6u IPv4 26324 0t0 TCP *:http (LISTEN) nginx 6325 nobody 6u IPv4 26324 0t0 TCP *:http (LISTEN)
四、升级到新版本优化
版本升级其实就是针对二进制文件的升级,过程以下:
[root@nginx ~]# /usr/local/nginx-1.12.2/sbin/nginx -v nginx version: nginx/1.12.2 [root@nginx ~]# cd /usr/local/nginx-1.12.2/sbin/ [root@nginx sbin]# mv nginx nginx-1.12.2 #首先备份原来的旧版本nginx二进制文件 [root@nginx sbin]# cp /usr/local/nginx-1.14.2/sbin/nginx ./ #拷贝新版本的二进制文件到当前目录
接下来进行平滑升级操做
[root@nginx ~]# ps -ef|grep nginx root 6324 1 0 09:06 ? 00:00:00 nginx: master process /usr/local/nginx-1.12.2/sbin/nginx nobody 6325 6324 0 09:06 ? 00:00:00 nginx: worker process root 6338 1244 0 09:11 pts/0 00:00:00 grep --color=auto nginx [root@nginx ~]# kill -USR2 6324 [root@nginx ~]# ps -ef|grep nginx root 6324 1 0 09:06 ? 00:00:00 nginx: master process /usr/local/nginx-1.12.2/sbin/nginx nobody 6325 6324 0 09:06 ? 00:00:00 nginx: worker process root 6340 6324 0 09:12 ? 00:00:00 nginx: master process /usr/local/nginx-1.12.2/sbin/nginx nobody 6341 6340 0 09:12 ? 00:00:00 nginx: worker process root 6343 1244 0 09:12 pts/0 00:00:00 grep --color=auto nginx
这时新的master进程已经正常开启,但老的work进程也存在,因此咱们使用下面的命令,将老的work进程发出平滑中止的信号,以下:
[root@nginx ~]# kill -WINCH 6324 [root@nginx ~]# ps -ef|grep nginx root 6324 1 0 09:06 ? 00:00:00 nginx: master process /usr/local/nginx-1.12.2/sbin/nginx root 6340 6324 0 09:12 ? 00:00:00 nginx: master process /usr/local/nginx-1.12.2/sbin/nginx nobody 6341 6340 0 09:12 ? 00:00:00 nginx: worker process root 6346 1244 0 09:14 pts/0 00:00:00 grep --color=auto nginx
此时,老的work进程已经中止,接下来咱们测试是否能正常访问:
能够正常访问,其实这一平滑升级的动做,对访问用户来讲是彻底感知不到,因此nginx热部署就已经完成了。
[root@nginx ~]# /usr/local/nginx-1.12.2/sbin/nginx -v nginx version: nginx/1.14.2
查看版本也是最新的版本,升级完成。
注:若是在版本升级完成后,没有任何问题,须要关闭老的master进程的话,可使用下面的命令:
kill -QUIT old_master_PID
五、版本回滚
对于升级来讲,最难的不是升级,而是回滚,由于在实际生产环境回滚的机率是存在,好比:新版本因为某些未知bug致使与现有应用不兼容、或出现运行不稳定的状况等等。
因此,对运维工程师来讲,故障回滚是重点。
在上面的结果中,咱们也能看到老的master进程是一直存在,在没有手工关闭前,它是不会自已关闭的,这种设计是有好处的,好处就是为了升级新版本后,若是出现问题能及时快速的回滚到上一个稳定版本。
[root@nginx ~]# ps -ef|grep nginx root 6324 1 0 09:06 ? 00:00:00 nginx: master process /usr/local/nginx-1.12.2/sbin/nginx root 6340 6324 0 09:12 ? 00:00:00 nginx: master process /usr/local/nginx-1.12.2/sbin/nginx nobody 6341 6340 0 09:12 ? 00:00:00 nginx: worker process root 6350 1244 0 09:23 pts/0 00:00:00 grep --color=auto nginx [root@nginx ~]# cd /usr/local/nginx-1.12.2/sbin/ [root@nginx sbin]# mv nginx nginx-1.14.2 [root@nginx sbin]# mv nginx-1.12.2 nginx [root@nginx sbin]# kill -USR1 6324 [root@nginx sbin]# ps -ef|grep nginx root 6324 1 0 09:06 ? 00:00:00 nginx: master process /usr/local/nginx-1.12.2/sbin/nginx root 6340 6324 0 09:12 ? 00:00:00 nginx: master process /usr/local/nginx-1.12.2/sbin/nginx nobody 6341 6340 0 09:12 ? 00:00:00 nginx: worker process root 6355 1244 0 09:24 pts/0 00:00:00 grep --color=auto nginx [root@nginx sbin]# ./nginx -v nginx version: nginx/1.12.2
从上面的结果发现,已经平滑的回滚的上一个版本,接下来测试是否能正常访问:
同样能够正常访问,因此,这个回滚的操做对用户来讲也是不可感知的。
好拉,今天的生产小技巧就分享到这里,若是你以为这篇文章对你有所帮助或参考与借鉴,请将此文章转发分享出去,你的转发就是对我最大的支持!!
更多精彩文章请关注民工哥的我的微信公众号:民工哥技术之路