三、案例:
有一个负载调度节点和三个服务节点以及一个内容发布节点构成,内容发布节点负责将用户发布的数据生成静态页面,同时将静态网页传输到三台服务节点,
而负载调度节点负责将用户请求根据负载算法调度到相应的服务节点,实现用户访问。用户要求在前端访问到的网页数据始终是最新的、一致的。
我们这里让3个WEB服务器节点,即时同步网站上的数据(通过上面inotify的介绍是个监控工具和rsync结合,两两结合,干活完美!)
这里介绍下,rsync工作原理:是在三台WEB节点当rsync的服务器端,server服务器当rsync的客户端,同步数据就是,server服务器向节点推送数据的过程

配置步骤:
1、软件的安装
三台节点:
rsync安装:#yum install xinetd
server节点:
安装inotify我们这里实验用的是编译安装
2、配置文件rsyncd.conf配置
(1)#vim /etc/rsyncd.conf(这个文件是需要自己创建)
# Global Settings
uid = nobody
gid = nobody
use chroot = no
max connections = 10
strict modes = yes
pid file = /var/run/rsyncd.pid
log file = /var/log/rsyncd.log
# Directory to be synced
[web1]
path = /web1/data
ignore errors = yes
read only = no
write only = no
hosts allow = 192.168.1.2
hosts deny = *
list = false
uid = root
gid = root
auth users = web1user
secrets file = /etc/web1.pass
# echo "web1user:123" >> /etc/web1.pass(创建帐号密码)
#chmod -R 600 /etc/web1.pass
(2)#vim /etc/rsyncd.conf(这个文件是需要自己创建)
# Global Settings
uid = nobody
gid = nobody
use chroot = no
max connections = 10
strict modes = yes
pid file = /var/run/rsyncd.pid
log file = /var/log/rsyncd.log
# Directory to be synced
[web2]
path = /web2/data
ignore errors = yes
read only = no
write only = no
hosts allow =192.168.1.3
hosts deny = *
list = false
uid = root
gid = root
auth users = web2user
secrets file = /etc/web2.pass
#echo "web2user:123" >> /etc/web2.pass
#chmod -R 600 /etc/web2.pass
(3)#vim /etc/rsyncd.conf(这个文件是需要自己创建)
# Global Settings
uid = nobody
gid = nobody
use chroot = no
max connections = 10
strict modes = yes
pid file = /var/run/rsyncd.pid
log file = /var/log/rsyncd.log
# Directory to be synced
[web3]
path = /web3/data
ignore errors = yes
read only = no
write only = no
hosts allow = 192。168.1.4
hosts deny = *
list = false
uid = root
gid = root
auth users = web3user
secrets file = /etc/web3.pass
# echo "web3user:123" >> /etc/web3.pass
#chmod -R 600 /etc/web3.pass
三个节点配置文件配置完成后,启用#chkconfig rsync on && service xinetd start;
加入开机启动echo “/usr/local/bin/rsync --daemon” >>/etc/rc.local
2、 配置内容发布节点
配置内容发布节点的主要工作是将生成的静态网页实时的同步到集群中三个服务节点
#!/bin/bash
host1=192.168.1.2
host2=192.168.1.3
host3=192.168.1.4
src=/web/wwwroot/
dst1=web1
dst2=web2
dst3=web3
user1=web1user
user2=web3user
user3=web3user
/usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e modify,delete,create,attrib $lj \
| while read files
do
/usr/bin/rsync -vzrtopg --delete --progress --password-file=/etc/server.pass $src $user1@$host1::$dst1
/usr/bin/rsync -vzrtopg --delete --progress --password-file=/etc/server.pass $src $user2@$host2::$dst2
/usr/bin/rsync -vzrtopg --delete --progress --password-file=/etc/server.pass $src $user3@$host3::$dst3
echo "${files} was rsynced" >>/tmp/rsync.log 2>&1
done
# echo "123" >> /etc/server.pass(这里只需要密码即可)
# chmod -R 600 /etc/server.pass
本相关解释如下:
--timefmt:指定时间的输出格式。
--format:指定变化文件的详细信息。
这两个参数一般配合使用,通过指定输出格式,输出类似与:
15/04/10 00:29 /web/wwwroot/ixdba.shDELETE,ISDIR was rsynced
15/04/10 00:30 /web/wwwroot/index.htmlMODIFY was rsynced
15/04/10 00:31 /web/wwwroot/pcre-8.02.tar.gzCREATE was rsynced
这个脚本的作用就是通过inotify监控文件目录的变化,进而触发rsync进行同步操作,由于这个过程是一种主动触发操作,通过系统内核完成的,所以,比起那些遍历整个目录的扫描方式,效率要高很多。
有时会遇到这样的情况:向inotify监控的目录(这里是/web/wwwroot/)写入一个很大文件时,由于写入这个大文件需要一段时间,
此时inotify就会持续不停的输出该文件被更新的信息, 这样就会持续不停的触发rsync去执行同步操作,占用了大量系统资源,那么针对这种情况,最理想的做法是等待文件写完后再去触发rsync同步。
在这种情况下,可以修改inotify的监控事件,即:“-e close_write,delete,create,attrib”是指 “监听 创建 移动 删除 写入 权限” 事件。
接着,将这个脚本命名为inotifyrsync.sh,放到/web/wwwroot目录下,然后给定可执行权限,放到后台运行:
chmod 755 /web/wwwroot/inotifyrsync.sh
/web/wwwroot/inotifyrsync.sh &
最后,将此脚本加入系统自启动文件:
echo “/web/wwwroot/inotifyrsync.sh &”>>/etc/rc.local
3 测试rsync+inotify实时同步功能
所有配置完成后,可以在网页发布节点的/web/wwwroot目录下添加、删除或者修改某个文件,
然后到三个服务节点对应的目录查看文件是否跟随网页发布节点的/web/wwwroot目录下文件发生变化,
如果你看到三个服务节点对应的目录文件跟着内容发布节点目录文件同步变化,那么我们这个系统就配置成功了。
Rsync 的详细介绍:请点这里
Rsync 的下载地址:请点这里
本文永久更新链接地址:http://www.linuxidc.com/Linux/2014-08/105851.htm
