实验目的:例如在一个大的集群架构中,下面有N个WEB节点,当站点须要更新操做时,若是一台一台的去更新,会大大下降系统/运维工程师的效率,然而,使用rsync构建全部节点之间的实时同步,其中有一台同步端,当同步端下面的WEB站点内容发生变化时,全部被同步端同时也会获得自动更新,这样就大大下降了系统/运维工程师比较枯燥而又无味的操做。
注:此同步不可用于数据库直接的实时同步。linux
1、系统环境
一、同步端
[root@host3 ~]# cat /etc/redhat-release
CentOS release 5.5 (Final)
[root@host3 ~]# uname -a
Linux host3.zc.com 2.6.18-194.el5 #1 SMP Fri Apr 2 14:58:14 EDT 2010 x86_64 x86_64 x86_64 GNU/Linux
[root@host3 ~]# ifconfig eth0 | grep "inet addr" | awk '{print $2}' | awk -F: '{print $2}'
192.168.10.3算法
二、被同步端
[root@host4 ~]# cat /etc/redhat-release
CentOS release 5.5 (Final)
[root@host4 ~]# uname -a
Linux host4.zc.com 2.6.18-194.el5 #1 SMP Fri Apr 2 14:58:14 EDT 2010 x86_64 x86_64 x86_64 GNU/Linux
[root@host4 ~]# ifconfig eth0 | grep "inet addr" | awk '{print $2}' | awk -F: '{print $2}'
192.168.10.4数据库
2、软件环境
rsync
rsync是一个开源快速备份工具,能够在不一样主机之间镜像同步整个目录树,支持增量备份、保持连接和权限,且采用优化的同步算法、传输前执行压缩,所以很是适用于异地备份、镜
像服务器等应用。
inotify-tools-3.14.tar.gz
Inotify 是文件系统事件监控机制,计划包含在即将发布的 Linux 内核中做为 dnotify
的有效替代。dnotify是较早内核支持的文件监控机制。Inotify一种强大的、细粒度的、异步的机制,它知足各类各样的文件监控须要,不只限于安全和性能。vim
3、安装配置rsync(CentOS5.5系统中默认已经安装rsync,这里咱们使用系统默认rpm包安装的rsync,也顺道提供了源码包的安装方法)
一、源码包
tar xzvf rsync-3.0.8.tar.gz
cd rsync-3.0.8
./configure
make
make install
mkdir /etc/rsyncd
vim rsyncd.conf安全
二、rpm包
同步端:
[root@host3 ~]# yum -y install gcc* xinetd
[root@host3 ~]# rpm -qa | grep rsync
rsync-2.6.8-3.1
[root@host3 ~]# cat /etc/rsyncd.conf #此文件为本身创建的文件
uid = nobody #运行rsync程序的用户
gid = nobody #运行rsync程序的组
use chroot = yes #禁锢在源目录
adress = 192.168.10.3 #监听地址(本机ip)
port 873 #监听端口
log file = /var/log/rsyncd.log #日志文件位置
pid file = /var/run/rsyncd.pid #存放进程ID的文件位置
hosts allow = 192.168.10.0/24 #容许访问的客户机地址
[wwwroot] #
path = /test/ #源目录的实际路径
read only = yes #是否为只读
comment = Document Root of host1.benet.com #描述
dont compress = *.gz *.bz2 *.tgz *.zip *.rar *.z #同时再也不压缩的文件类型
被同步端:
[root@host4 ~]# yum -y install xinetd
[root@host4 ~]# rpm -qa | grep rsync
rsync-2.6.8-3.1
[root@host4 ~]# cat /etc/rsyncd.conf
uid = nobody #运行rsync程序的用户
gid = nobody #运行rsync程序的组
use chroot = yes #禁锢在源目录
adress = 192.168.10.4 #监听地址(本机ip)
port 873 #监听端口
log file = /var/log/rsyncd.log #日志文件位置
pid file = /var/run/rsyncd.pid #存放进程ID的文件位置
hosts allow = 192.168.10.0/24 #容许访问的客户机地址
[wwwroot] #
path = /test/ #源目录的实际路径
read only = yes #是否为只读
comment = Document Root of host1.benet.com #描述
dont compress = *.gz *.bz2 *.tgz *.zip *.rar *.z #同时再也不压缩的文件类型
[root@host4 ~]# sed -i 's/yes/no/' /etc/xinetd.d/rsync
[root@host4 ~]# /etc/init.d/xinetd restart
[root@host4 ~]# chkconfig xinetd on
[root@host4 ~]# netstat -anpt | grep 873
tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 3749/xinetd bash
4、在同步端安装inotify软件
一、调整inotify内核参数
在linux内核中,默认的inotify机制提供了三个调控参数:max_queue_envents 监控事件队列
max_user_instances 最多监控实例数
max_user_watches 每一个实例最多监控文件数(建议大于监控目标的文件数)
[root@host3 ~]# vi /etc/sysctl.conf #末行添加
fs.inotify.max_queued_events = 16384
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 1048576
[root@host3 ~]# cd /data/
[root@host3 data]# tar -zxf inotify-tools-3.14.tar.gz
[root@host3 data]# cd inotify-tools-3.14
[root@host3 inotify-tools-3.14]# ./configure
[root@host3 inotify-tools-3.14]# make
[root@host3 inotify-tools-3.14]# make install服务器
二、编写触发式同步脚本
[root@host3 inotify-tools-3.14]# cd /script/
[root@host3 script]# cat inosync.sh
#!/bin/bash
INOTIFY_CMD="inotifywait -mrq -e modify,create,attrib,move,delete /test/"
$INOTIFY_CMD | while read DIRECTORY EVENT FILE
do
if [ $(pgrep rsync | wc -l) -le 0 ];then
rsync -azH --delete /test/ rput@192.168.10.4:/test/
rsync -e 'ssh -p 2000' -azH --delete /test/ rput@192.168.10.4:/test/ #当ssh端口被修改以后,得添加'ssh -p 2000'指定端口
fi
done
[root@host3 script]# chmod +x inosync.sh
[root@host3 script]# echo '/script/inosync.sh' >> /etc/rc.local
[root@host3 script]# sh inosync.sh &架构
四建立同步用户
被监控端
[root@host4 /]# useradd rput
[root@host4 /]# passwd rput 密码根据本身的安全要求设置
监控端
[root@host3 ~]# ssh-keygen
[root@host3 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub rput@192.168.10.4运维
设置rput用户对被同步目录的权限
被监控端
[root@host4 ~]# setfacl -R -m u:rput:rwx /test/ssh
五、测试 在同步端的/test目录下建立一个目录,而后再被同步端进行查看