您的位置:首页 > 运维架构 > Nginx

Unison(rpm方式)安装及应用--nginx高可用

2016-06-26 17:28 531 查看
简介

在项目中,使用nginx代理后端的多台tomcat服务器,为了保证nginx的高可用,必须有一台备用nginx服务器(也可以是双活),其配置和主nginx服务器保持一致。

而实际运用过程中,nginx的配置文件可能会动态地发生变化,为了保证两台服务器的配置文件一样,可以有多种方式。

1. 使用NFS,两台服务器都挂载NFS,指向同一个实际的存储位置,所有的配置文件都写入到NFS中,这样,两台nginx服务器的配置信息都会保持一致,此方案实现简单,缺点是NFS会成为单点,如果NFS服务器挂了,那两台nginx服务器都挂了

2. 配置文件生成在单独的服务器上,两台nginx服务器都单独同步到自己的服务器上,这种同步只需要单向即可,使用rsync即可,rsync的安装也很简单,直接使用yum -y install rsync即可,rsync支持ssh, 以及原始的socket方式进行数据同步。

3. 配置文件直接生成在其中一台nginx服务器上,两台服务器进行数据同步,至于是哪一台,依赖于虚拟ip绑定到哪台nginx服务器上,此时rsync不能满足要求,因为rsync的同步是单向的,不过有Unison可以实现双向的文件同步,其同步算法和rsync相同,支持增量同步。

具体使用哪种方法得结合实际使用场景,本文介绍使用Unison的方式实现两台服务器上的文件同步。

安装Unison

关于Unison的安装,网上最常见的方式是使用源码方式,该方式下依赖Ocaml,安装步骤繁琐且容易出错,不推荐。寻寻觅觅,找到了一种使用rpm的方式安装,操作只需要三条命令,且不容易出错。

下载并导入rpm源

`wgethttp://apt.sw.be/redhat/el6/en/x86_64/rpmforge/RPMS/rpmforge-release-0.5.2-2.el6.rf.x86_64.rpm`

`sudo rpm -Uvhrpmforge-release-0.5.2-2.el6.rf.x86_64.rpm`

`sudo yum install -y unison`

测试,直接运行`unison`,可以看到Unison的使用方式有三种

unison [options]

unison root1 root2 [options]

unison profilename [options]

也可以通过unison -help查看unison的使用参数说明。

运行unison如果不指定profilename,将默认使用当前用户目录下的.unison目录下的default.prf文件,通常,我们会将需要使用的options参数写入到profile文件中再直接运行unison,这种方式的好处就是维护性和易用性更高,不用每次都输入冗长的命令。

如下图:配置将当前机器的/home/paas/Unison目录与172.16.4.182服务器上的/home/paas/Unison目录保持同步,在一次同步失败时会重试3次。



note: 1.  我们必须为两台需要同步的服务器设置免密码登录(使用ssh-key-gen -t rsa和ssh-copy-id)。

           2. 没有要求同步的两个服务器上的目录也一致,可以进行服务器1上的A目录与服务器2上的B目录同步

         

服务器上创建定时任务进行同步

最常见的定时任务就是使用crontab

首先编辑shell脚本,运行unison,检测是否有nginx配置文件发生变化,如果有发生变化需要reload nginx工作进程。本例中脚本保存为StartUnison.sh,并添加可执行权限。

#!/bin/sh
#use crontab -e to add timely cycle task
#crontab 3/* * * * * /<path>/StartUnison.sh

unisonProcess=`ps -ef | grep unison | grep -v grep | wc -l`
if [ $unisonProcess -gt 0 ]; then
echo "`date` detached unison is running, which means the sync is in progress, waiting the process exit." >> unison.log
else
echo "`date` begin to sync file" >> unison.log
unison

#the unison always return 1 when the connection is connected
#echo "unison precoess finished and return $?" | tee unison.log

echo "finished sync file and begin to fresh nginx" | tee unison.log

#check the file number which have been updated within 3 minutes
fileUpdateCount=`find /home/paas/Union/ -depth -mmin -3 | wc -l`
nginxProcessNum=`ps -ef | grep nginx | grep -v grep | wc -l`
if [ $fileUpdateCount -gt 0 ];then
echo "`date` find total $fileUpdateCount has been updated" >> freshNginx.log
if [ $nginxProcessNum -eq 0 ];then
echo "try to start nginx `date`" >> freshNginx.log
sudo nginx
echo "succeed start nginx" >> freshNginx.log
else
echo "try to reload nginx `date`" >> freshNginx.log
sudo nginx -s reload
echo "succeed to reload nginx `date`" >> freshNginx.log
fi
fi
echo "finished fresh nginx" >> unison.log
fi


其次,编辑crontab文件,启动定时任务。

`crontab -e`

输入

crontab 3/* * * * * /<path>/StartUnison.sh

保存。

这样,本地3分钟运行一次unison,如果发现配置文件发生变化就会启动或reload nginx。

注: 为了保证高可用,所以两台服务器上都需要添加该脚本,保证nginx进程都在运行中。

        这只是保证高可用的基础,而要实现高可用常见的硬件支持如F5,或软件支持keepalived。

本文链接:http://blog.csdn.net/musa875643dn/article/details/51763314, 欢迎转载,转载请标明出处。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  centos nginx shell