|
如何实现两台web服务器的文件同步的,答案是rsync,但是,如何做到实时同步呢,cron已经达不到这样的要求了,同步的再快,也会有时间间隔,cron时刻执行,也会浪费系统的资源,下面,我将介绍ssh+rsync+inotify来实现两台web间的文件实时同步。 拓扑如下:
实验的linux系统为CentOS 6.5,实验之前确保开发环境已安装完毕
首先,我们介绍一下inotify,这是linux的一个新特性,在2.6的内核开始加入,它是监控文件系统,并且及时的向我们的rsync发出相关信息,例如增删改查等操作,操作灵敏,简单,比cron对系统的资源损耗小,是一个好的选择。
下面的实验,我们主要在两台web主机和rsync的主机展开,由于知识有限,我这里用了DNS做负载均衡,并没有采用nginx做负载均衡,测试时,我们仍采用地址访问。
1、在rsync主机上安装inotify和rsync的软件
1
2
| [iyunv@rsync ~]# rpm -qa | grep rsync
rsync-3.0.6-9.el6_4.1.x86_64
|
CentOS6.5上已经安装过了rsync了
1
2
3
4
| [iyunv@rsync ~]# tar zxf inotify-tools-3.13.tar.gz
[iyunv@rsync ~]# cd inotify-tools-3.13
[iyunv@rsync inotify-tools-3.13]# ./configure
[iyunv@rsync inotify-tools-3.13]# make && make install
|
2、在我们的三台服务器上,为rsync服务提供配置文件
rsync.365lsy.com
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
| [iyunv@rsync ~]# vim /etc/rsyncd.conf
#Global settings
uid = nobody
gid = nobody
user chroot = no
max connections = 10
strict modes = yes
pid file = /var/run/rsyncd.pid
logfile = /var/log/rsyncd.log
#Directory to be rsynced
[rsync]
path = /data #创建该目录
comment = server file
ignore errors = yes
read only = no
write only = no
hosts allow = 192.168.77.0/24
hosts deny = *
list = false
uid = root
gid = root
[iyunv@rsync ~]# chmod 600 /etc/rsyncd.conf
|
修改权限为600
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
| web1.365lsy.com
[iyunv@web1 ~]# vim /etc/rsyncd.conf
#Global settings
uid = nobody
gid = nobody
user chroot = no
max connections = 10
strict modes = yes
pid file = /var/run/rsyncd.pid
logfile = /var/log/rsyncd.log
#Directory to be rsynced
[web1]
path = /web1 #创建该目录
comment = web1 file
ignore errors = yes
read only = no
write only = no
hosts allow = 192.168.77.144
hosts deny = *
list = false
uid = root
gid = root
[iyunv@web1 ~]# chmod 600 /etc/rsyncd.conf
|
修改权限600
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
| web2.365lsy.com
[iyunv@web2 ~]# vim /etc/rsyncd.conf
[iyunv@web2 rsync]# vim /etc/rsyncd.conf
#Global settings
uid = nobody
gid = nobody
user chroot = no
max connections = 10
strict modes = yes
pid file = /var/run/rsyncd.pid
logfile = /var/log/rsyncd.log
#Directory to be rsynced
[web2]
path = /web2 #创建该目录
comment = web2 file
ignore errors = yes
read only = no
write only = no
hosts allow = 192.168.77.144
hosts deny = *
list = false
uid = root
gid = root
[iyunv@web2 ~]# chmod 600 /etc/rsyncd.conf
|
3、由于在CentOS6.5上,rsync是有超级进程xinetd管理的,我们要让rsync启动
更改配置文件如下,三台主机操作一致
1
| [iyunv@web2 rsync]# vim /etc/xinetd.d/rsync
|
安装xinetd服务,并启动服务
1
2
3
4
| [iyunv@web2 rsync]# yum install xinetd
[iyunv@web2 rsync]# service xinetd restart
Stopping xinetd: [FAILED]
Starting xinetd: [ OK ]
|
同时,我们可以使用ss -tnlp查看873端口是否监听
若是要让rsync开机就启动,我们可以将xinetd服务加入开机启动
1
2
3
| [iyunv@web2 ~]# chkconfig xinetd on
[iyunv@web1 ~]# chkconfig –list xinetd
xinetd 0:off 1:off 2:on 3:on 4:on 5:on 6:off
|
4、由于我上面并没有提供账号及密码,下面我将提供密钥的认证方式
首先,确保三台主机上,ssh的配置文件中,下面三个选项已经开启
1
2
3
| RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys
|
由于我们的密钥是基于RSA加密算法的
5、在rsync的主机上创建密钥
1
| [iyunv@rsync data]# ssh-keygen -t rsa -P ''
|
生成的密钥在当前的用户家目录下的.ssh目录中
并且,将公钥传到两台web服务器上
6、然后,在rsync服务上,同时登陆到两台web服务器上
1
2
3
4
5
6
7
| [iyunv@rsync data]# ssh 192.168.77.157
reverse mapping checking getaddrinfo for bogon [192.168.77.157] failed – POSSIBLE BREAK-IN ATTEMPT!
Last login: Mon Aug 18 14:42:13 2014 from 192.168.77.1
[iyunv@bogon ~]# ifconfig
eth1 Link encap:Ethernet HWaddr 00:0C:29:B6:29:F0
inet addr:192.168.77.157 Bcast:192.168.77.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:feb6:29f0/64 Scope:Link
|
测试都能够通过,这样就能验证了
7、在rsync的主机上,提供同步的脚本,如下所示
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| #!/bin/bash
web1=192.168.77.157
web2=192.168.77.155
src=/data/
dst1=web1
dst2=web2
/usr/local/bin/inotifywait -mrq –timefmt '%d/%m/%y %H:%M' –format '%T %w%f' -e modify,delete,create,attrib ${src} | while read file
do
rsync -vzrtopg –delete –progress ${src} root@${web1}::${dst1} &&
rsync -vzrtopg –delete –progress ${src} root@${web2}::${dst2} &&
echo "${file} was rsynced" >> /tmp/rsync.log 2>&1
echo "##############finish#######################"
done
|
参数解析:
inotifywait的相关参数
inotify是一个监控等待事件
-m:表示始终保持事件监听状态
-r:表示递归查询目录
-q:打印出监控事件
-e:指定要监控的事件
–timefmt:指的是时间的输出格式
–format:指变化文件的详细信息
-e,event,即指定要监听的事件,有modify,delete,create,attrib等
注意:这个脚本在执行的时候,是没有反应的,只有当你在/data目录里改变文件时,才会触发这个脚本,才会进行同步的过程。
8、然后把这个脚本加入到开启执行中
1
2
| [iyunv@rsync ~]# vim /etc/rc.d/rc.local
bash /root/rsync.sh &
|
注意:一定要放在后台,要不然开机的时候,会一直卡在这里,只能进单用户改了
9、测试:
回到我们的两台web服务器上,将httpd服务器的网站根目录改为web1和web2
1
2
3
| [iyunv@web1 web1]# vim /etc/httpd/conf/httpd.conf
DocumentRoot "/web1"
|
web2上也是做同样修改,然后重启httpd服务
在rsync服务器上的根下的data目录下,编辑index.html,如下内容
1
| This is the rsync server!!!
|
10、重要的步骤,为了安全起见,将/web{1,2}的属主和属组改为apache,要不然也是无法访问
1
2
| [iyunv@web1 /]# chown apache:apache web1/
[iyunv@web2 /]# chown apache:apache web2/
|
然后分别访问web1和web2,通过ip地址访问
见图
总结:这样实验的功能就初步完成了,但是,这其中的权限问题值得深思,开始时,我用的是普通用户,但是,遇到了问题,后来就用root完成了,下一步,想用普通用户去实验一下
|
|