刚好前面用rsync+sersync做了主从数据同步,现在有新的需求了,P2P系统两台服务器做负载均衡,但是由于P2P会生成N多的合同,N多的数字证书,会上传文件,所以根据现实情况需要两台相互同步。
感觉sersync应该能够实现这个功能,感觉应该是能满足要求的,接下来就部署起来了。
安装就参照前一篇文章了,下面是具体步骤:
步骤一:规划好需要同步的目录
为方便起见,两台服务器要同步的目录我建成一样的结构,/home/p2padm/
步骤二:配置好两台服务器的rsync(两台都要配置)
Centos缺省是安装了rsync的,只是缺省没有配置和启动后台进程而已,做下配置,以下是Server1的配置过程,Server2的配置过程类似,只是hosts allow要换成Server1的。
vi /etc/rsyncd.conf
uid = p2padm
gid = p2p
read only = no
use chroot = true
transfer logging = true
log format = %h %o %f %l %b
log file = /var/log/rsyncd.log
pid file = /var/run/rsyncd.pid
lock file=/var/run/rsyncd.lock
#hosts allow = trusted.hosts
#slp refresh = 300
max connections=30
[p2padm]
path = /home/p2padm/
comment = p2padm rsync
auth users = p2padm 同步用的用户名
secrets file = /etc/rsyncd.secrets
hosts allow = 172.16.17.11 对方机器IP
hosts deny = 0.0.0.0/32
exclude=tmp/ log/ trc/ backup/ dump/ jboss5/ 排队哪些文件夹下的内容不需要同步
vi /etc/rsyncd.secrets p2padm:p2padm chmod 666 rsyncd.secrets
步骤三:下载 sersync 并配置(两台都要配置)
以下是Server1的配置过程,Server2的配置过程类似,只是remote allow要换成Server1的,安装参照前一篇,配置文件见下:
vi confxml.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<head version="2.5">
<host hostip="localhost" port="8008"></host>
<debug start="false"/>
<fileSystem xfs="false"/>
<filter start="true">
<exclude expression="(.*)\.svn"></exclude>
<exclude expression="(.*)\.gz"></exclude>
<exclude expression="^info/*"></exclude>
<exclude expression="^data/*"></exclude>
<exclude expression="^data/*/*"></exclude>
<exclude expression="^trc/*/*"></exclude>
<exclude expression="^static/*"></exclude>
</filter>
<inotify>
<delete start="true"/>
<createFolder start="true"/>
<createFile start="false"/>
<closeWrite start="true"/>
<moveFrom start="true"/>
<moveTo start="true"/>
<attrib start="false"/>
<modify start="false"/>
</inotify>
<sersync>
<localpath watch="/home/p2padm">
<remote ip="172.16.17.11" name="p2padm"/>
</localpath>
<rsync>
<commonParams params="-artuz"/>
<auth start="true" users="p2padm" passwordfile="/etc/rsync.pas"/> <!--这里一定要设置为true,才会自动同步-->
<userDefinedPort start="false" port="874"/><!-- port=874 -->
<timeout start="false" time="100"/><!-- timeout=100 -->
<ssh start="false"/>
</rsync>
<failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once-->
<crontab start="false" schedule="600"><!--600mins-->
<crontabfilter start="false">
<exclude expression="*.php"></exclude>
<exclude expression="info/*"></exclude>
</crontabfilter>
</crontab>
<plugin start="false" name="command"/>
</sersync>
<plugin name="command">
<param prefix="/bin/sh" suffix="" ignoreError="true"/> <!--prefix /opt/tongbu/mmm.sh suffix-->
<filter start="false">
<include expression="(.*)\.php"/>
<include expression="(.*)\.sh"/>
</filter>
</plugin>
<plugin name="socket">
<localpath watch="/opt/tongbu">
<deshost ip="192.168.138.20" port="8009"/>
</localpath>
</plugin>
<plugin name="refreshCDN">
<localpath watch="/data0/htdocs/cms.xoyo.com/site/">
<cdninfo domainname="ccms.chinacache.com" port="80" username="xxxx" passwd="xxxx"/>
<sendurl base="http://pic.xoyo.com/cms"/>
<regexurl regex="false" match="cms.xoyo.com/site([/a-zA-Z0-9]*).xoyo.com/images"/>
</localpath>
</plugin>
</head>
步骤四:启动sersync后台daemon程序,开始进入监控(两台都要运行)
启动两台的rsync
/usr/bin/rsync --daemon--config=/etc/rsyncd.conf
再启动两台的sersync
/usr/local/sersync/sersync2-d -r -o /usr/local/sersync/confxml.xml
查看同步日志tail -f 100 /var/log/rsync.log
完成,收工,在两台服务器的这个目录下建目录,加文件,删除文件看,是不是同步了。
当然我这配置的都是手动启动的,可以添加到系统自动启动运行,我就先不弄了,留点后门。