服务器规划:
Master IP:192.168.250.193
Slave IP:192.168.250.194
VIP:192.168.250.195
服务器操作系统版本: centos 7
前期准备:
分别在master slave 创建目录
mkdir -p /mynfsdata
关闭防火墙 selinux 消除影响 (不符合安全规范)
systemctl stop firewalld && systemctl disable firewalld
setenforce 0
vim /etc/selinux/config
SELINUX=disabled
安装nfs服务,配置共享目录
yum -y install nfs-utils rpcbind
echo '/mynfsdata *(rw,sync,all_squash)'>> /etc/exports
systemctl start nfs && systemctl start rpcbind
配置文件同步-单向(master to slave)
#slave上操作
yum -y install rsync.x86_64
修改配置:
vim /etc/rsyncd.conf
uid = root
gid = root
port = 873
pid file = /var/run/rsyncd.pid
log file = /var/log/rsyncd.log
lock file = /var/run/rsyncd.lock
use chroot = no
max connections = 200
read only = false
list = false
fake super= yes
ignore errors
[data]
path = /mynfsdata
auth users = rsyncuser
secrets file = /etc/rsync_salve.pass
hosts allow = 192.168.250.193#填写master服务器的IP
#生成认证文件
echo 'rsyncuser:rsyncuser12#'> /etc/rsync_salve.pass
chmod 600 /etc/rsync_salve.pass
chown -R root:root /mynfsdata
#启动服务
rsync --daemon --config=/etc/rsyncd.conf
#master上测试
yum -y install rsync.x86_64
chown -R root:root /mynfsdata
echo "rsyncuser12#"> /etc/rsync.pass
chmod 600 /etc/rsync.pass
rsync -arv /mynfsdata/ rsyncuser@192.168.250.194::data --password-file=/etc/rsync.pass
#slave上验证检查
ls /mynfsdata
#master上配置自动同步
sersync2.5.4_64bit_binary_stable_final.tar.gz
tar -zxvf sersync2.5.4_64bit_binary_stable_final.tar.gz -C /usr/local/sersync
#修改配置文件
cd sersync/
sed -ri 's#<delete start="true"/>#<delete start="false"/>#g' confxml.xml
sed -ri '24s#<localpath watch="/opt/tongbu">#<localpath watch="/mynfsdata">#g' confxml.xml
sed -ri '25s#<remote ip="127.0.0.1" name="tongbu1"/>#<remote ip="192.168.250.194" name="data"/>#g' confxml.xml
sed -ri '30s#<commonParams params="-artuz"/>#<commonParams params="-az"/>#g' confxml.xml
sed -ri '31s#<auth start="false" users="root" passwordfile="/etc/rsync.pas"/>#<auth start="true" users="rsyncuser" passwordfile="/etc/rsync.pass"/>#g' confxml.xml
sed -ri '33s#<timeout start="false" time="100"/><!-- timeout=100 -->#<timeout start="true" time="100"/><!-- timeout=100 -->#g' confxml.xml
#启动服务
./sersync2 -dro ./confxml.xml
#测试自动同步
# 在 master 中的/mynfsdata 目录创建文件
[root@mynfs01 mynfsdata]# touch mytest2.txt
然后在slave中的/mynfsdata 是否有该文件
[root@mynfs02 mynfsdata]# ls
aaa.txt mytest2.txt
[root@mynfs02 mynfsdata]# cat mytest2.txt
111
以上就做完了 salve 同步 master 的文件,但是当 master 宕机后恢复,master 无法同步 salve 文件,所以要配置 master 同步 salve 文件 单向的
在master配置rsync服务,进行同步slave数据
#修改 /etc/rsyncd.conf
其中 hosts allow 填写 slave ip=192.168.250.194
vim /etc/rsyncd.conf
uid = root
gid = root
port = 873
pid file = /var/run/rsyncd.pid
log file = /var/log/rsyncd.log
lock file = /var/run/rsyncd.lock
use chroot = no
max connections = 200
read only = false
list = false
fake super= yes
ignore errors
[data]
path = /mynfsdata
auth users = rsyncuser
secrets file = /etc/rsync_master.pass
hosts allow = 192.168.250.194
#生成认证文件
echo 'rsyncuser:rsyncuser12#'> /etc/rsync_master.pass
chmod 600/etc/rsync_master.pass
chown -R root:root /mynfsdata
#启动服务
rsync --daemon --config=/etc/rsyncd.conf
#slave上测试
[root@mynfs02 ~]# echo "rsyncuser12#"> /etc/rsync.pass
[root@mynfs02 ~]# chmod 600/etc/rsync.pass
cd /mynfsdata
echo "hello world"> myfile.txt
rsync -arv /mynfsdata rsyncuser@192.168.250.193::data --password-file=/etc/rsync.pass
#master上查看
[root@mynfs01 mynfsdata]# cd mynfsdata/
[root@mynfs01 mynfsdata]# ls
aaa.txt myfile.txt mytest2.txt
[root@mynfs01 mynfsdata]# pwd
/mynfsdata/mynfsdata
[root@mynfs01 mynfsdata]# cat myfile.txt
hello world
#在slave上配置自动同步
#安装sersync 同上
#修改配置
[root@mynfs02 sersync]# sed -ri 's#<delete start="true"/>#<delete start="false"/>#g' confxml.xml
[root@mynfs02 sersync]# sed -ri '24s#<localpath watch="/opt/tongbu">#<localpath watch="/mynfsdata">#g' confxml.xml
[root@mynfs02 sersync]# sed -ri '25s#<remote ip="127.0.0.1" name="tongbu1"/>#<remote ip="192.168.250.193" name="data"/>#g' confxml.xml
[root@mynfs02 sersync]# sed -ri '30s#<commonParams params="-artuz"/>#<commonParams params="-az"/>#g' confxml.xml
[root@mynfs02 sersync]# sed -ri '31s#<auth start="false" users="root" passwordfile="/etc/rsync.pas"/>#<auth start="true" users="rsyncuser" passwordfile="/etc/rsync.pass"/>#g' confxml.xml
[root@mynfs02 sersync]# sed -ri '33s#<timeout start="false" time="100"/><!-- timeout=100 -->#<timeout start="true" time="100"/><!-- timeout=100 -->#g' confxml.xml
#启动服务
./sersync2 -dro ./confxml.xml
至此就做好了主从互相同步,接下来利用keepalive做心跳检测
#master上操作
yum -y install keepalived
cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.ori.bak.20220312
cat /etc/keepalived/keepalived.conf
! ConfigurationFilefor keepalived
global_defs {
router_id NFS-Master
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 150
advert_int 1
authentication {
auth_type PASS
auth_pass keepalived12#
}
virtual_ipaddress {
192.168.250.195
}
}
systemctl start keepalived.service && systemctl enable keepalived.service
#slave上操作
cat /etc/keepalived/keepalived.conf
! ConfigurationFilefor keepalived
global_defs {
router_id NFS-Slave
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass keepalived12#
}
virtual_ipaddress {
192.168.250.195
}
}
systemctl start keepalived.service && systemctl enable keepalived.service
#master上检查虚拟ip
[root@mynfs01 /]# ip a | grep 192.168.250.195
inet 192.168.250.195/32 scope global eth0
#使用虚拟ip测试挂载
mount -t nfs 192.168.250.195:/mynfsdata /mnt
#模拟主NFS故障
[root@mynfs01 /]# systemctl stop keepalived.service
[root@mynfs01 /]# ip a | grep 192.168.250.195
#从机上查看
[root@mynfs02 /]# ip a | grep 192.168.250.195
inet 192.168.250.195/32 scope global eth0
这里就说明keepalive心跳检测配置成功,接下来做nfs服务的检测
设置keepalive脚本来检测nfs存活
cat /root/check_nfs.sh
#!/bin/sh
/usr/bin/systemctl status keepalived &>/dev/null
if[ $? -ne 0]
then
echo "keepalived未启动,无需监测."
exit0
else
/usr/bin/systemctl status nfs &>/dev/null
if[ $? -ne 0]
then
/usr/bin/systemctl restart nfs
/usr/bin/systemctl status nfs &>/dev/null
if[ $? -ne 0]
then
/usr/bin/systemctl stop keepalived
fi
fi
fi
#定时任务
crontab -l
*/1 * * * * /bin/sh /root/check_nfs.sh &> /root/check_nfs.log