您的位置:首页 > 运维架构 > 网站架构

部署nfs高可用rsync+inotify

2019-11-15 19:42 1661 查看

项目环境:

三台主机(centos7):

nfs-server主:172.16.1.20
nfs-server从:172.16.1.30
client(客户机):172.16.1.40

项目操作:

1, 首先搭建nfs服务器

主从nfs-server都需搭建,相同的操作。

[root@nfs-master ~]# yum -y install nfs-utils  #安装nfs服务
[root@nfs-master ~]# yum -y install rpcbind #安装远程传输控制协议
[root@nfs-master ~]# vim /etc/exports  #编写nfs文件
/nfs-share  172.16.1.*(rw,sync,no_root_squash)

参数解释:
172.16.1.*:表示允许该网段,也可以自定义ip地址
rw:可读可写
sync:同步数据到磁盘
no_root_squash:加上这个选项后,root用户就会对共享的目录拥有至高的权限控制,就像是对本机的目录操作一样。

[root@nfs-master ~]# mkdir /nfs-share       #创建共享目录
[root@nfs-master ~]# systemctl start rpcbind    #先启动该服务
[root@nfs-master ~]# systemctl start nfs

2, 在从nfs-server上搭建rsync:

//安装rsync:
[root@nfs-slave ~]# yum -y install rsync
//修改rsync配置文件:
[root@nfs-slave ~]# vim /etc/rsyncd.conf

修改内容如下:

//为授权账户创建数据文件:
[root@nfs-slave ~]# vim /etc/rsyncd_users.db
#文件名可以自定义,但是必须与上面配置文件中数据文件名相同

注意:用户名得和rsync配置文件中同一个用户。

//授予权限
[root@nfs-slave ~]# chmod 600 /etc/rsyncd_users.db
//启动rsync服务:
[root@nfs-slave ~]# rsync  --daemon

3, 在主nfs-server上安装inotify工具:

下载并上传inotify-tools-3.14.tar.gz安装包

[root@nfs-master ~]# tar zxf inotify-tools-3.14.tar.gz
[root@nfs-master ~]# cd inotify-tools-3.14/
[root@nfs-master inotify-tools-3.14]# ./configure && make && make install

##编写触发式同步脚本:

[root@nfs-master ~]# vim inotify.sh


#!/bin/bash
INOTIFY_CMD="inotifywait -mrq -e modify,create,move,delete /nfs-share"
RSYNC_CMD="rsync -azH --delete --password-file=/etc/server.pass /nfs-share sunqiuming@172.16.1.30::rsync"
$INOTIFY_CMD | while read DIRECTORY EVENT FILE

do
$RSYNC_CMD
done

注意:该脚本用于监控本地的共享目录/nfs-share,只要监控到对该目录有任何操作,就会同步数据到从nfs-server服务器上的/nfs-share目录下(需要权限)。

//创建脚本中的密码文件(为了在同步的过程中不用在输入密码)

[root@nfs-master ~]# echo "123456" >  /etc/server.pass
##文件名自定义,只要确保与上述脚本中相同

[root@nfs-master ~]# chmod 600 /etc/server.pass    #授予权限

//对从nfs-server服务上的共享目录授予权限

[root@nfs-slave ~]# chmod 777 /nfs-share/
[root@nfs-slave ~]# ls -ld /nfs-share/
drwxrwxrwx 2 root root 6 Nov 14 23:14 /nfs-share/

//将脚本文件加入开机自启:

[root@nfs-master ~]# echo '/root/inotify.sh' >> /etc/rc.local

//执行该触发脚本:

[root@nfs-master ~]# sh inotify.sh  &  #后台运行

4, client客户机进行挂载:

[root@client ~]# mkdir /test    #创建测试挂载目录
[root@client ~]# mount -t nfs -o soft,timeo=5 172.16.1.20:/nfs-share  /test
#注意使用软挂载,默认是硬挂载,使用软挂载,当服务端宕机,不会一直阻塞
##在测试目录下编写测试文件:
[root@client test]# echo "hello" > index.html
[root@client test]# echo "ha ha ha " > index.php

//在主nfs-server上进行查看:

[root@nfs-master ~]# cd /nfs-share/
[root@nfs-master nfs-share]# cat index.html
hello
[root@nfs-master nfs-share]# cat index.php
ha ha ha

//再次到从nfs-server上进行查看(是否已经触发脚本,并同步数据)

[root@nfs-slave ~]# cd /nfs-share/
[root@nfs-slave nfs-share]# ls
nfs-share
[root@nfs-slave nfs-share]# cd nfs-share/
[root@nfs-slave nfs-share]# cat index.html
hello
[root@nfs-slave nfs-share]# cat index.php
ha ha ha

数据同步成功。。。。。。。。。。

5, 模拟nfs-server故障:

模拟故障前首先在客户机上编写检测脚本:

[root@client ~]# vim nfs.sh
#!/bin/bash
while  true;
do
ping 172.16.1.20 -c 4  &> /dev/null
if [ $? -ne 0  ];
then
umount -l /test  && mount -t nfs -o soft,timeo=5 172.16.1.30:/nfs-share /test
fi
sleep 1
done

注意:这个脚本会每秒检测一次,只要当主nfs-server故障后,就会重新挂载到从nfs-server上的共享目录下。

//执行该脚本:

[root@client ~]# sh nfs.sh &  #让其后台运行

//接下来将主nfs-server主机进行关机或者停止服务(模拟宕机)。

//最后验证客户机是否检测到,并且将目录挂载到从nfs-server服务器上。

[root@client ~]# cd /test
[root@client test]# ls
nfs-share
[root@client test]# ls nfs-share/
123.txt  index.html  index.php

---------------------------------可以看到当主nfs-server故障后,会执行脚本重新挂载到备用nfs-server上,以实现数据不会丢失,并且不会中断正在运行的服务---------------------

———————— 本文至此结束,感谢阅读 ————————

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: