准备工作检查selinux 防火墙
#关闭 selinux
sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config
#关闭防火墙
systemctl stop firewalld;systemctl disable firewalld
1.安装nfs相关包
# 所有节点安装nfs相关包
yum install nfs-utils -y
systemctl enable nfs-utils --now
2.主节点启动nfs服务
systemctl enable nfs-server.service --now
3.主节点创建目录并共享
# 创建nfs共享目录
mkdir /data
echo "/data *(rw,sync,insecure,no_subtree_check,no_root_squash)" /etc/exports
# 重启nfs服务
systemctl restart nfs-server.service
# 在其他节点showmount测试
showmount -e 172.25.47.18
Export list for xxx.xxx.xxx.xxx:
/data *
#其他节点测试挂载
mkdir /haha
mount -t nfs 主节点IP地址:/data /haha
umount /haha
4.从节点创建目录
mkdir /data
5.配置 rsync
此文档使用以下IP和目录举例,使用时请根据实际情况进行更换
主节点:172.16.164.14
从节点:172.16.164.15
同步目录 /data
yum install rsync -y
vi /etc/rsyncd.conf(从节点执行)
uid = root
gid = root
use chroot = yes
max connections = 200
log file=/var/log/rsyncd.log
pid file = /var/run/rsyncd.pid
lock file=/var/run/rsyncd.lock
exclude = lost+found/
transfer logging = yes
timeout = 600
ignore nonreadable = yes
dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2
[data]
path = /data
auth users = rsync
secrets file = /etc/rsync_salve.pass
hosts allow = 172.16.164.14
read only = no
6.配置认证文件
从节点执行
格式为:用户:用户密码
echo 'rsync:password' > /etc/rsync_salve.pass
chmod 600 /etc/rsync_salve.pass
主节点执行
这里写入从节点配置的密码
echo "password" > /etc/rsync.pass
chmod 600 /etc/rsync.pass
7.目录授权
主节点和从节点执行
chown -R root:root /data
8.启动服务
从节点执行
systemctl enable rsyncd.service
systemctl start rsyncd.service
9.同步测试
主节点执行
rsync -arv /data/ rsync@172.16.164.15::data --password-file=/etc/rsync.pass
#如果想在目标目录中删除那些在源目录中不存在的文件可使用
#rsync -avz --progress --delete /data/ rsync@172.16.164.15::data --password-file=/etc/rsync.pass
10.使用 sersync2 配置自动同步(主节点执行)
10.1 安装 sersync2
#下载
wget https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/sersync/sersync2.5.4_64bit_binary_stable_final.tar.gz
#解压
tar -zxvf sersync*_64bit_binary_stable_final.tar.gz && mv GNU-Linux-x86/ /usr/local/sersync && cd /usr/local/sersync
10.2 修改配置文件
!!注意更换下面的IP地址哦(改为从节点的)!!
sed -ri '24s#<localpath watch="/opt/tongbu">#<localpath watch="/data">#g' confxml.xml
sed -ri '25s#<remote ip="127.0.0.1" name="tongbu1"/>#<remote ip="172.16.164.15" name="data"/>#g' confxml.xml
sed -ri '31s#<auth start="false" users="root" passwordfile="/etc/rsync.pas"/>#<auth start="true" users="rsync" 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
10.3 启动
将 sersync2 纳入 system管理
cat > /etc/systemd/system/sersync2.service << EOF
[Unit]
Description=Sersync2 Service
After=network.target
[Service]
RemainAfterExit=yes
ExecStart=/usr/local/sersync/sersync2 -dro /usr/local/sersync/confxml.xml
ExecStop=/usr/bin/pkill sersync2
ExecReload=/usr/bin/pkill sersync2 && /usr/local/sersync/sersync2 -dro /usr/local/sersync/confxml.xml
Restart=on-abnormal
[Install]
WantedBy=multi-user.target
EOF
启动 sersync2
systemctl daemon-reload
systemctl enable sersync2
systemctl start sersync2
11.测试主从是否同步
#主节点执行
echo 123 >/data/test.tt
#从节点执行
cat /data/test.txt
#如果看到结果是123即表示成功搭建了NFS主从