单一的rsync工具仅可以进行数据同步,单一的Inotify仅可以实现实时文件监控,而两者的结合能班组企业对数据中心实时数据同步的要求。接下来我们用案例说明两者结合部署流程,例子中需要部署一套web服务,然后随着用户访问量的增加,单台服务器已经满足不了大量的冰法访问。因此,采用集群技术,整合多台服务器处理能力实现负载均衡,从而满足并发访问量。 由于web服务器所提供的网站数据需要保持一致,但当服务器越来越多时,公司发现在这些主机之间同步那些随时可能发生改变的网站数据非常困难。解决方案是在后端建立一个数据发布服务器,该服务器作为rsync客户端,通过Inotify机制实时监控网站的数据,当数据发生变化后调用rsync命令上传数据至多个rsync服务器,这里rsync服务器就是提供web服务的web服务器。 首先需要在多台web服务器上部署rsync服务器,这些rsync服务器要能够提供客户端上传功能,以实现客户端主机将数据推送至rsync服务器,实现数据的实时同步功能。 如图所示: 配置如下: Web1 [iyunv@localhost ~] yum -y install rsync [iyunv@localhost ~] mkdir -p /var/www/001 [iyunv@localhost ~] chmod 660 /var/www/001 [iyunv@localhost ~] chown nobody:nobody /var/www/001 [iyunv@localhost ~]vim /etc/rsync.conf #!/etc/rsyncd.conf transfer logging = yes log file=/var/log/rsync.log pid file=/var/run/rsyncd.pid lock file=/var/run/rsync.lock uid=nobody gid=nobody use chroot=no ignore errors read only=no [web1] comment = web content list=false path=/var/www/001 auth users=tom secrets file=/etc/rsyncd.secrets hosts allow=10.10.10.132 hosts deny=* [iyunv@localhost ~] echo “tom:pass” > /ect/rsyncd.secrets [iyunv@localhost ~] chmod 600 /etc/rsyncd.secrets [iyunv@localhost ~] rsync --daemon [iyunv@localhost ~] echo “rsync --daemon” >> /etc/rc.local 如果有防火墙,注意防火墙端口开启873端口 Web2 [iyunv@localhost ~] yum -y install rsync [iyunv@localhost ~] mkdir -p /var/www/002 [iyunv@localhost ~] chmod 660 /var/www/002 [iyunv@localhost ~] chown nobody:nobody /var/www/002 [iyunv@localhost ~]vim /etc/rsync.conf #!/etc/rsyncd.conf transfer logging = yes log file=/var/log/rsync.log pid file=/var/run/rsyncd.pid lock file=/var/run/rsync.lock uid=nobody gid=nobody use chroot=no ignore errors read only=no [web2] comment = web content list=false path=/var/www/002 auth users=tom secrets file=/etc/rsyncd.secrets hosts allow=10.10.10.132 hosts deny=* [iyunv@localhost ~] echo “tom:pass” > /ect/rsyncd.secrets [iyunv@localhost ~] chmod 600 /etc/rsyncd.secrets [iyunv@localhost ~] rsync --daemon [iyunv@localhost ~] echo “rsync --daemon” >> /etc/rc.local 如果有防火墙,注意防火墙端口开启873端口 在数据发布服务器(10.10.10.132)上需要下载inotify软件包,并编写监控脚本,这里的脚本名称为notify_rsync.sh。当监控到数据发生改变时,自动进行数据同步操作,将数据推送至web服务器 [iyunv@localhost ~] yum -y install rsync [iyunv@localhost ~] yum -y install automake libtool [iyunv@localhost ~] unzip inotify-tools-master.zip [iyunv@localhost ~] Cd inotify-tools [iyunv@localhost ~] ./autogen.sh configure.ac [iyunv@localhost ~] ./configure [iyunv@localhost ~] make && make install [iyunv@localhost ~] echo "pass" > /etc/rsyncd.secrets [iyunv@localhost ~] chmod 600 /etc/rsyncd.secrets [iyunv@localhost ~] vim notify_rsync.sh #!/bin/bash #this rsync scripts based on inotify #date:2015-6-23 #version:1.0 export PATH=/bin:/usr/bin:/usr/local/bin src=/tmp/ dest1=web1 dest2=web2 client1=10.10.10.136 client2=10.10.10.133 user=tom #password file must not be other-accessible passfile=/etc/rsyncd.secrets [ ! -e $passfile ] && exit 2 #wait for change inotifywait -mrq --timefmt '%y-%m-%d %H:%M' --format '%T %w%f %e' --event modify,create,move,delete,attrib $src | while read file do echo "$file" > /var/log/inotify_web 2>&1 /usr/bin/rsync -avz --delete --progress --password-file=$passfile $src ${user}@$client1::$dest1 >> /var/logsync_web1 2>&1 /usr/bin/rsync -avz --delete --progress --password-file=$passfile $src ${user}@$client2::$dest2 >> /var/logsync_web2 2>&1 done [iyunv@localhost ~] chmod a+x notify_rsync.sh [iyunv@localhost ~] /root/notify_rsync.sh [iyunv@localhost ~] Echo “/root/notify_rsync.sh” /etc/rc.local 向/tmp/下添加内容,去web1和web2上查看是否同步 # cat /tmp/123 Hello,world
|