nfs服务器 rip1:192.168.1.134
rip2:192.168.1.135
vip:192.168.1.137
客户端 ip1: 192.168.1.136
ip2: 192.168.1.234
用nfs来提供共享
用keepalive来实现对nfs的高可用
用inotify来检测文件的变化
用unison来同步主从nfs共享文件夹的数据
1.nfs服务器的搭建
yum install -y nfs-utils rpcbind
/etc/init.d/rpcbind start
/etc/init.d/nfs start
vi /etc/exports
/nfs_data/home/paixian192.168.1.0/24(rw,sync,no_wdelay)
/etc/init.d/nfs reload
chmod 755 /nfs_data
chown -R nfsnobody:nfsnobody /nfs_data
2.客户端测试
yum install -y nfs-utils rpcbind
/etc/init.d/rpcbind start
/etc/init.d/nfs start
[root@localhost paixian]# showmount -e192.168.1.134
Export list for 192.168.1.134:
/nfs_data/home/paixian 192.168.1.0/24
nfs服务器测试成功!下一步就是要做keepalive来实现高可用
3.keepalive实现nfs高可用
http://linuxnote.blog.51cto.com/9876511/1652612
keepalive安装可参考这个博客
http://linuxnote.blog.51cto.com/9876511/1652981
配置介绍请参考这个博客
主的keepalive配置
[root@min local]# cat/etc/keepalived/keepalived.conf|egrep -v "\#|^$"
! Configuration File for keepalived
global_defs {
notification_email {
}
notification_email_from [email protected]
smtp_server 192.168.1.135
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state MASTER
interface eth1
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.137/24
}
}
}
从的keepalive配置
[root@min ~]# cat /etc/keepalived/keepalived.conf|egrep -v"\#|^$"
! Configuration File for keepalived
global_defs {
notification_email {
}
notification_email_from [email protected]
smtp_server 192.168.1.135
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.137/24
}
}
}
客户机测试
[root@localhost paixian]#showmount -e192.168.1.137
Export list for 192.168.1.137:
/nfs_data/home/paixian 192.168.1.0/24
看来需求是可以解决了,当在客户机删除或者创建一个文件的时候就会同步到别的客户机和主nfs服务器上,那么问题来了,另外一台从的nfs服务器该如何同步呢?那么我们需要用到unison+inotify来实现主从那个共享文件的互相同步了
4.用inotify+unison实现共享文件互相同步
unison参考文档 http://yangsc.blog.51cto.com/7051889/1580846
http://wenku.baidu.com/link?url=PeWElTzzYAYnzBLLMbJRL5GV34QAVriXf6W_sSpBfP1OYNfFqESsaCeO6rpYGdIe6na344u9bwyqZUx6gM6PrUQX6DPoeBHtFImnrrRsnIS&qq-pf-to=pcqq.group
1.135上的操作
wgethttp://caml.inria.fr/pub/distrib/ocaml-3.12/ocaml-3.12.0.tar.gz ///下载2进制安装程序
tar xf ocaml-3.12.0.tar.gz
cd ocaml-3.12.0
./configure
make world opt
make install
wgethttp://www.seas.upenn.edu/~bcpierce/unison//download/releases/stable/unison-2.48.3.tar.gz
mv unison-2.48.3.tar.gz ../
cd ..
tar xf unison-2.48.3.tar.gz
cd unison-2.48.3
make UISTYLE=text
yum install emacs -y(报错etags安装emacs)
mkdir /root/bin/
cp unison /root/bin/(报错没有/root/bin这个文件夹)
make install
双机互信:
ssh-keygen
ssh-copy-id -i .ssh/[email protected](2台互相做)
scpunison [email protected]:/usr/local/bin/unison
134上创一个/root/bin目录然后把unison复制到/root/bin下面
创建文件[root@min paixian]# cat /root/.unison/default.prf
#Unison preferences file
batch= true
owner= true
group= true
perms= -1
fastcheck= true
rsync= false
sshargs= -C
xferbycopying= true
confirmbigdel= false
log= true
logfile= /home/unison/unison.log
然后复制到134上
scp/root/.unison/default.prf 192.168.1.134:/root/.unison/default.prf
1.135和1.134安装inotify
更多的inotify与rsync的同步请看http://10554846.blog.51cto.com/10544846/1693630
wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
mvinotify-tools-3.14.tar.gz /usr/local
cd /usr/local
tar xf inotify-tools-3.14.tar.gz
mv inotify-3.14 inotify
./configure
make&&make install
写脚本检测变同步
[root@minpaixian]# cat /usr/local/unison.sh
inotifywait-mrq --timefmt '%Y/%m/%d-%H:%M:%S' --format '%T %w %f %e' -emodify,attrib,move,create,delete /nfs_data/home/paixian|while read files
do
/root/bin/unison/nfs_data/home/paixian ssh://192.168.1.134//nfs_data/home/paixian &&echo "${files} was rsynced" >>/tmp/unison.log 2>&1
done
复制到134
scp/usr/local/unison.sh 192.168.1.134:/usr/local/unison.sh
然后修改里面的ip为192.168.1.135即可
开始同步可能得花很长一段时间
但后面就可以做到4台甚至N台(只要你多几台挂载就行)实时同步并且高可用了