设为首页 收藏本站
查看: 710|回复: 1

[经验分享] 使用Rsync+sersync架构(多实例),保持web服务器网页发布目录...

[复制链接]

尚未签到

发表于 2017-11-3 13:47:17 | 显示全部楼层 |阅读模式
使用Rsync+sersync架构(多实例),保持web服务器网页发布目录实时同步
一、为什么要用 rsync+sersync 架构?
1、sersync 是基于 inotfy 开发的,类似于 inotfy-tools 的工具
2、sersync 可以记录下被监听目录中发生变化的(包括增加、删除、修改)具体某一个文
件或者某一个目录的名字,然后使用 rsync 同步的时候,只同步发生变化的文件或者目录
二、sersync 多实例安装配置
sersync  同步逻辑图

1dd88100be62c2bb34490b0197e6de84.jpg-wh_500x0-wm_3-wmp_4-s_1849699053.jpg
当前版本的 sersync 依赖于 rsync 进行数据同步;
原理步骤:
1. 在同步服务器( workstation/172.25.17.9 )上开启 sersync 服务, sersync 负载监控配置路径中的文件系统事件变化;
2. 调用 rsync 命令把更新的文件同步到目标服务器( servere ,serverf,serverg 和 serverh );
3. 需要在主服务器workstation配置 sersync ,在同步目标服务器配置 rsync 守护进程服务

在servere和serverf上部署rsync  服务
1、部署 rsync 服务
[iyunv@servere ~]# yum -y install rsync
[iyunv@servere ~]# vim /etc/rsyncd.conf
uid = nginx
gid = nginx
use chroot = yes
max connections = 4
pid file = /var/run/rsyncd.pid
exclude = lost+found/
transfer logging = yes
timeout = 900
ignore nonreadable = yes
dont compress   = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2
read only = no
auth users= rsync_backup
secrets file= /etc/rsync.password
[php]     #--------------------- 模块配置 ---------------------------#
        path = /usr/share/nginx/php-f1.com/
        comment = www.php-f1.com html page

2、创建 rsync 同步密码文件,并设置权限为 600
[iyunv@servere ~]# echo "rsync_backup:123">/etc/rsync.password
[iyunv@servere ~]# chmod 600 /etc/rsync.password
[iyunv@servere ~]# cat /etc/rsync.password

3、启动守护进程,并写入开机自启动
[iyunv@servere ~]# rsync --daemon
[iyunv@servere ~]# echo "/usr/bin/rsync --daemon" >> /etc/rc.local
[iyunv@servere ~]# chmod +x /etc/rc.d/rc.local
[iyunv@servere ~]# pkill -9 rsync ; rm -fr /var/run/rsyncd.pid
[iyunv@servere ~]# source  /etc/rc.local
[iyunv@servere ~]# netstat -tnlp |grep :873
tcp        0      0 0.0.0.0:873             0.0.0.0:*               LISTEN      2949/rsync           
tcp6       0      0 :::873                    :::*                    LISTEN      2949/rsync

同样的,在serverg和serverh上部署rsync  服务
1、部署 rsync 服务
[iyunv@serverg ~]# yum -y install rsync
[iyunv@serverg ~]# vim /etc/rsyncd.conf
uid = tomcat
gid = tomcat
use chroot = yes
max connections = 4
pid file = /var/run/rsyncd.pid
exclude = lost+found/
transfer logging = yes
timeout = 900
ignore nonreadable = yes
dont compress   = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2
read only = no
auth users= rsync_backup
secrets file= /etc/rsync.password
[jsp]    #--------------------- 模块配置 ---------------------------#
        path = /usr/local/tomcat/apache-tomcat-8.0.24/jsp-f1.com/ROOT/
        comment = www.jsp-f1.com html page

2、创建 rsync 同步密码文件,并设置权限为 600
[iyunv@serverg ~]# echo "rsync_backup:123">/etc/rsync.password
[iyunv@serverg ~]# chmod 600 /etc/rsync.password
[iyunv@serverg ~]# cat /etc/rsync.password

3、启动守护进程,并写入开机自启动
[iyunv@serverg ~]# rsync --daemon
[iyunv@serverg ~]# echo "/usr/bin/rsync --daemon" >> /etc/rc.local
[iyunv@serverg ~]# chmod +x /etc/rc.d/rc.local
[iyunv@serverg ~]# pkill -9 rsync ; rm -fr /var/run/rsyncd.pid
[iyunv@serverg ~]# source  /etc/rc.local
[iyunv@serverg ~]# netstat -tnlp |grep :873
tcp        0      0 0.0.0.0:873             0.0.0.0:*               LISTEN      2984/rsync           
tcp6       0           0 :::873                  :::*                    LISTEN      2984/rsync

master  上手动测试  rsync  同步情况
特别提示:此步非常关键,如果测试不成功,后面的 sersync 配好了也不会同步数据;
1、在 master 上配置 rsync 客户端相关权限认证:
[iyunv@workstation ~]# echo "123">/etc/rsync.password
[iyunv@workstation ~]# chmod 600 /etc/rsync.password
2、创建待同步数据,执行同步命令
[iyunv@workstation ~]# mkdir -p /phpdata
[iyunv@workstation ~]# mkdir -p /jspdata
[iyunv@workstation ~]# touch /phpdata/php.log
[iyunv@workstation ~]# touch /jspdata/jsp.log
[iyunv@workstation ~]# rsync -avzP /phpdata/  rsync_backup@172.25.17.14::php/ --password-file=/etc/rsync.password
[iyunv@workstation ~]# rsync -avzP /phpdata/  rsync_backup@172.25.17.15::php/ --password-file=/etc/rsync.password
[iyunv@workstation ~]# rsync -avzP /jspdata/ rsync_backup@172.25.17.16::jsp/ --password-file=/etc/rsync.password
[iyunv@workstation ~]# rsync -avzP /jspdata/ rsync_backup@172.25.17.17::jsp/ --password-file=/etc/rsync.password
提示:
在后面进行部署 sersync 之前, sersync主服务器上必须要确保手工可以把文件推送到
servere,serverf,serverg,serverh上,这样后续 sersync 才能调用这些命令来自动推送
在推送前关闭 iptables
3、查看同步结果
[iyunv@servere ~]# ls /usr/share/nginx/php-f1.com/
[iyunv@serverf ~]# ls /usr/share/nginx/php-f1.com/
[iyunv@serverg ~]# ls /usr/local/tomcat/apache-tomcat-8.0.24/jsp-f1.com/ROOT/
[iyunv@serverh ~]# ls /usr/local/tomcat/apache-tomcat-8.0.24/jsp-f1.com/ROOT/

在主服务器workstation上  开始部署 sersync  服务
多实例情况
1 )配置多个 confxml.xml 文件(比如: www 、php 、 blog 、jsp 等等)
2 )根据不同的需求同步对应的实例文件

1 、下载 sersync
[iyunv@workstation ~]# yum -y install rsync
[iyunv@workstation ~]# tar xf sersync2.5.4_64bit_binary_stable_final.tar.gz -C /usr/local/
[iyunv@workstation ~]# mv /usr/local/GNU-Linux-x86 /usr/local/sersync
2 、配置 sersync
[iyunv@workstation ~]# cd /usr/local/sersync
[iyunv@workstation sersync ]# cp confxml.xml confxml.xml.$(date +%F)
[iyunv@workstation sersync ]# mv confxml.xml php_confxml.xml

修改第一个模块的配置文件
a) 修改 24--28 行;
b )修改 31--34 行,认证部分【 rsync 密码认证】;
c )修改 37 行。
[iyunv@workstation sersync ]# vim php_confxml.xml    修改后的完整配置文件为:
<?xmlversion="1.0" encoding="ISO-8859-1"?>
<headversion="2.5">
<host hostip="localhost" port="8008"></host>
<debug start="false"/>
<fileSystem xfs="false"/>
<filter start="false">
<excludeexpression="(.*)\.svn"></exclude>
<excludeexpression="(.*)\.gz"></exclude>
<excludeexpression="^info/*"></exclude>
<excludeexpression="^static/*"></exclude>
</filter>
<inotify>
<delete start="true"/>
<createFolderstart="true"/>
<createFilestart="false"/>
<closeWritestart="true"/>
<moveFromstart="true"/>
<moveTo start="true"/>
<attrib start="false"/>
<modify start="false"/>
</inotify>

<sersync>
<localpath watch="/phpdata">     # 本地需要同步的目录
<remote ip="172.25.17.14" name="php"/>      # 远程端服务 ip ,模块
<remote ip="172.25.17.15" name="php"/>         
</localpath>       #提示: 此步 watch="/phpdata" 就是定义服务端待同步的目录,和目标服务器的模块name="php"
<rsync>
<commonParams params="-artuz"/>
<auth start="true" users="rsync_backup" passwordfile="/etc/rsync.password"/>
<userDefinedPort start="false" port="874"/><!-- port=874 -->
<timeout start="true" time="100"/><!-- timeout=100 -->
<ssh start="false"/>
</rsync>

<failLog path="/usr/local/sersync/logs/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once-->
<crontab start="false"schedule="600"><!--600mins-->
<crontabfilterstart="false">
<excludeexpression="*.php"></exclude>
<excludeexpression="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 su
ffix-->
<filter start="false">
<include expression="(.*)\.php"/>
<includeexpression="(.*)\.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/">
<cdninfodomainname="ccms.chinacache.com" port="80"username="xxxx" passwd="xxx
x"/>
<sendurlbase="http://pic.xoyo.com/cms"/>
<regexurlregex="false"match="cms.xoyo.com/site([/a-zA-Z0-9]*).xoyo.com/images"/>
</localpath>
</plugin>
</head>

修改第二个模块的配置文件
[iyunv@workstation sersync ]# cp php_confxml.xml jsp_confxml.xml
[iyunv@workstation sersync ]# vim jsp_confxml.xml   
#红色部分都需要修改
<?xmlversion="1.0" encoding="ISO-8859-1"?>
<headversion="2.5">
<host hostip="localhost" port="8009"></host># 第二个端口要修改,否则跟上一个冲突
<sersync>
<localpath watch="/jspdata">   
<remote ip="172.25.17.16" name="jsp"/>      # 远程端服务 ip ,模块
<remote ip="172.25.17.17" name="jsp"/>         
</localpath>
<rsync>
<commonParams params="-artuz"/>
<auth start="true" users="rsync_backup" passwordfile="/etc/rsync.password"/>
<userDefinedPort start="false" port="874"/><!-- port=874 -->
<timeout start="true" time="100"/><!-- timeout=100 -->
<sshstart="false"/>
</rsync>

3、分别启动 sersync 守护进程同步数据
[iyunv@workstation ~]# /usr/local/sersync/sersync2 -d -o /usr/local/sersync/php_confxml.xml
[iyunv@workstation ~]# /usr/local/sersync/sersync2 -d -o /usr/local/sersync/jsp_confxml.xml
配置 sersync 环境变量
[iyunv@workstation ~]# echo"PATH=$PATH:/usr/local/sersync/">>/etc/profile
[iyunv@workstation ~]# source /etc/profile
[iyunv@workstation ~]# sersync2
启动命令后返回结果如下为正常:
[iyunv@workstation ~]# /usr/local/sersync/sersync2 -d -o /usr/local/sersync/php_confxml.xml
set the system param
execute:echo 50000000 > /proc/sys/fs/inotify/max_user_watches
execute:echo 327679 > /proc/sys/fs/inotify/max_queued_events
parse the command param
option: -d  run as a daemon
option: -o  config xml name:  /usr/local/sersync/php_confxml.xml
daemon thread num: 10
parse xml config file
host ip : localhost host port: 8008
daemon start,sersync run behind the console  
use rsync password-file :
user is rsync_backup
passwordfile is  /etc/rsync.password
config xml parse success
please set /etc/rsyncd.conf max connections=0 Manually
sersync working thread 12  = 1(primary thread) + 1(fail retry thread) + 10(daemon sub threads)
Max threads numbers is: 32 = 12(Thread pool nums) + 20(Sub threads)
please according your cpu ,use -n param to adjust the cpu rate
chmod: cannot access ‘/usr/local/sersync/logs/rsync_fail_log.sh’: No such file or directory
run the sersync:  
watch path is: /phpdata
  
[iyunv@workstation ~]# /usr/local/sersync/sersync2 -d -o /usr/local/sersync/jsp_confxml.xml
set the system param
execute:echo 50000000 > /proc/sys/fs/inotify/max_user_watches
execute:echo 327679 > /proc/sys/fs/inotify/max_queued_events
parse the command param
option: -d  run as a daemon
option: -o  config xml name:  /usr/local/sersync/jsp_confxml.xml
daemon thread num: 10
parse xml config file
host ip : localhost host port: 8009
daemon start,sersync run behind the console  
use rsync password-file :
user is rsync_backup
passwordfile is  /etc/rsync.password
config xml parse success
please set /etc/rsyncd.conf max connections=0 Manually
sersync working thread 12  = 1(primary thread) + 1(fail retry thread) + 10(daemon sub threads)
Max threads numbers is: 32 = 12(Thread pool nums) + 20(Sub threads)
please according your cpu ,use -n param to adjust the cpu rate
chmod: cannot access ‘/usr/local/sersync/logs/rsync_fail_log.sh’: No such file or directory
run the sersync:  
watch path is: /jspdata
三、命令参数说明
Sersync参数
说明
./sersync  -r
-r参数作用是:开启实时监控的之前对主服务器目录与远程目标机器的目录进行一次整体同步;如果需要将sersync运行前,主服务器目录下已经存在的所有文件或目录全部同步到远端,则要以-r参数运行sersync,将本地与远程整体同步一次;
提别说明:如果设置了过滤器,即在xml文件中,filter为true,则暂时不能使用-r参数进行整体同步;
./sersync  -o xx.xml
不指定-o参数: sersync使用sersync可执行文件目录下的默认配置文件confxml.xml
指定-o 参数:可以指定多个不同的配置文件,从而实现sersync多进程多实例的数据同步
./sersync  -n num
-n参数为:指定默认的线程池的线程总数;
例如:./sersync -n 5 则指定线程总数为5,如果不指定,默认启动线程池数量是10,如果cpu使用过高,可以通过该参数调低,如果机器配置较高,可以调高默认的线程总数,提升同步效率;
./sersync  -d
-d参数为:后台服务,通常情况下使用-r参数对本地到远端整体同步一遍后,在后台运行此参数启动守护进程实时同步;在第一次整体同步时,-d 和-r参数经常会联合使用;
./sersync  -m
pluginName
-m参数:不进行同步,只运行插件 ./sersync -m pluginName
例如:./sersync -m command,则在监控到事件后,不对远程目标服务器进行同步,而是直接运行command插件
组合命令使用说明:
-n  8 -o liubl.xml -r -d
多个参数可以配合使用,例如:./sersync -n 16 -o config.xml -r -d 表示设置线程池工作线程为16个,指定liubl.xml作为配置文件,在实时监控前做一次整体同步,以守护进程方式在后台运行;
./sersync  --help
很遗憾,它没有查看帮助(需要的话2条路,要么看源代码,要么自测求验证)



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-406526-1-1.html 上篇帖子: Centos 安装ffmpeg 转码工具 下篇帖子: Varnish缓存负载均衡backend的实现
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表