nfs高可用实践

参考博客:https://blog.csdn.net/qq_39826987/article/details/126781943

环境准备:

  1. 操作系统:centos8.1 * 3台
  2. 服务器命名:master 、slave、client
  3. 网络环境:离线环境

安装包准备:

nfs-utils、rpcbind、keepalived
因为是离线环境,所以自己搭建了个相同操作系统的虚拟机用于拉取在线镜像,防止版本不同导致各种依赖问题。
注: 如果centos镜像下就有rpm,则直接使用镜像下的内容,如果没有,就需要从外网拉取

# 拉去安装环境所需要的包
yumdownloader --destdir=/opt/nfs --resolve nfs-utils rpcbind
yumdownloader --destdir=/opt/nfs --resolve keepalive
yumdownloader --destdir=/opt/nfs --resolve rsync inotify

操作步骤

参考博客:链接: NFS高可用方案

安装NFS

  1. master 和slave上创建共享目录
# master
[root@k8s-main1 nfs-k8s]# pwd
/data/nfs-k8s
# slave
[root@k8s-main2 nfs-k8s]# pwd
/data/nfs-k8s
  1. 关闭防火墙
  2. 安装NFS
# 将外网拉去的rpm包上传到服务器,并进行安装
rpm -Uvh *.rpm --nodeps --force
  1. 配置nfs共享目录
    master
echo '/data/nfs-k8s 10.25.13.0/24(rw,sync,no_root_squash)' >> /etc/exports
# 开启服务
 systemctl start rpcbind && systemctl start nfs-server.service
 # 设置开机自启
 systemctl enable rpcbind && systemctl enable nfs-server.service 

slave

echo '/data/nfs-k8s 192.168.100.0/24(rw,sync,no_root_squash)' >> /etc/exports
# 开启服务
 systemctl start rpcbind && systemctl start nfs-server.service
 # 设置开机自启
 systemctl enable rpcbind && systemctl enable nfs-server.service 

在client上测试挂在

# 测试 Master 
# 其中 ip 为 Master 的 ip,/data为 Master 共享的目录,/qiyuesuodata 为需要挂载至 Client 的目录
 mount -t nfs 192.168.100.246:/data/nfs-k8s /qiyuesuodata
# 检查 
df -Th 
# 出现  192.168.100.246:/data      nfs4       29G  7.6G   22G  27% /qiyuesuodata 即为成功
# 去除挂载
umount /qiyuesuodata

# 测试 Slave
# 其中 ip 为 Master 的 ip,/data为 Master 共享的目录,/qiyuesuodata 为需要挂载至 Client 的目录
 mount -t nfs 192.168.100.247:/data/nfs-k8s /qiyuesuodata
# 检查 
df -Th 
# 出现  192.168.100.247:/data/nfs-k8s      nfs4       29G  7.6G   22G  27% /qiyuesuodata 即为成功
# 去除挂载
umount /qiyuesuodata
  1. 配置同步文件(slave同步master)
# 修改 /etc/rsyncd.conf 如下,其中 hosts allow 填写 master ip
uid = nfsnobody
gid = nfsnobody
port = 873
pid file = /var/rsyncd.pid
log file = /var/log/rsyncd.log
use chroot = no
max connections = 200
read only = false
list = false
fake super = yes
ignore errors
[data]
path = /data/nfs-k8s
auth users = dataNode
secrets file = /etc/rsync_salve.pass
hosts allow = 192.168.100.246

# 生成认证文件
echo 'dataNode:supermap10I' > /etc/rsync_salve.pass
chmod 600 /etc/rsync_salve.pass
# 修改 文件夹权限
# 需要创建fsnobody用户和dataNode用户
chown -R nfsnobody:nfsnobody /data/
# 启动服务
 rsync --daemon --config=/etc/rsyncd.conf 

在 Master 上测试

chown -R nfsnobody:nfsnobody /data/
# supermap10I是slave的dataNode用户密码
echo "supermap10I" > /etc/rsync.pass
chmod 600 /etc/rsync.pass
#创建测试文件,测试推送
cd /data/nfs-k8s
echo "This is test file" > file.txt
rsync -arv /data/ dataNode@192.168.100.247::data --password-file=/etc/rsync.pass
#在 slave 上测试
ls /data/nfs-k8s
# 出现 file.txt 即可

在master上配置自动同步

 cd /usr/local/
 # 在 https://dl.qiyuesuo.com/private/nfs/sersync2.5.4_64bit_binary_stable_final.tar.gz链接下载sersync2.5.4_64bit_binary_stable_final.tar.gz并上传到服务器
 # 服务器解压
 tar xvf sersync2.5.4_64bit_binary_stable_final.tar.gz
 mv GNU-Linux-x86/ sersync
 cd sersync/
 # 修改配置文件
sed -ri 's#<delete start="true"/>#<delete start="false"/>#g' confxml.xml
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="192.168.100.247" 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="dataNode" 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
#启动Sersync
/usr/local/sersync/sersync2 -dro /usr/local/sersync/confxml.xml

测试同步

# 在 master 中的/data/nfs-k8s 目录创建文件
touch test
# 查看 salve 中的 /data 是否有该文件
  1. 配置同步文件(master同步slave)
# 修改 /etc/rsyncd.conf 如下,其中 hosts allow 填写 master ip
uid = nfsnobody
gid = nfsnobody
port = 873
pid file = /var/rsyncd.pid
log file = /var/log/rsyncd.log
use chroot = no
max connections = 200
read only = false
list = false
fake super = yes
ignore errors
[data]
path = /data/nfs-k8s
auth users = dataNode
secrets file = /etc/rsync_salve.pass
hosts allow = 192.168.100.247

# 生成认证文件
echo 'dataNode:supermap10I' > /etc/rsync_salve.pass
chmod 600 /etc/rsync_salve.pass
# 修改 文件夹权限
chown -R nfsnobody:nfsnobody /data/
# 启动服务
 rsync --daemon --config=/etc/rsyncd.conf 

在 Master 上测试

chown -R nfsnobody:nfsnobody /data/
# supermap10I是slave的dataNode用户密码
echo "supermap10I" > /etc/rsync.pass
chmod 600 /etc/rsync.pass
#创建测试文件,测试推送
cd /data/nfs-k8s
echo "This is test file" > file1.txt
rsync -arv /data/ dataNode@192.168.100.246::data --password-file=/etc/rsync.pass
#在 slave 上测试
ls /data/nfs-k8s
# 出现 file1.txt 即可

在master上配置自动同步

 cd /usr/local/
 # 在 https://dl.qiyuesuo.com/private/nfs/sersync2.5.4_64bit_binary_stable_final.tar.gz链接下载sersync2.5.4_64bit_binary_stable_final.tar.gz并上传到服务器
 # 服务器解压
 tar xvf sersync2.5.4_64bit_binary_stable_final.tar.gz
 mv GNU-Linux-x86/ sersync
 cd sersync/
 # 修改配置文件
sed -ri 's#<delete start="true"/>#<delete start="false"/>#g' confxml.xml
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="192.168.100.246" 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="dataNode" 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
#启动Sersync
/usr/local/sersync/sersync2 -dro /usr/local/sersync/confxml.xml

测试同步

# 在 slave中的/data/nfs-k8s 目录创建文件
touch test1
# 查看 salve 中的 /data 是否有该文件

安装keepalive

前面的安装包中已经安装了keepalive,但是启动的时候发现缺少so库,处理过程如下:

安装过程中异常处理

  1. 异常:/usr/sbin/keepalived: error while loading shared libraries: libnetsnmpmibs.so.35: cannot open shared object file: No such file or director
    解决方案:ceontos镜像包下搜索net-snmp,并拷贝到服务器安装
    在这里插入图片描述
 rpm -Uvh *.rpm --nodeps --force
 # 如果还报错,就将so库文件所在的文件夹夹路径追加到/etc/ld.so.conf中,然后执行ldconfig命令生效
ldconfig -v
  1. 异常:error while loading shared libraries: libsensors.so.4: cannot open shared object file: No such file or directory
    解决方案:安装sensors
    在这里插入图片描述
[root@k8s-main1 sensors]# rpm -Uvh *.rpm --nodeps --force
[root@k8s-main1 nfs]# ldconfig -v
# 重启keepalive服务
[root@k8s-main1 nfs]# systemctl restart keepalived.service

master配置keepalive

yum -y install keepalived.x86_64
# 修改 /etc/keepalived/keepalived.conf
# 其中 ens3 为绑定网卡名称,可以使用 ip addr 命令查看
# 其中 192.168.100.8  为虚拟 ip ,注意不要和其它 ip 冲突
! Configuration File for keepalived

global_defs {
   router_id NFS-Master
}

vrrp_instance VI_1 {
    state MASTER
    interface ens3 
    virtual_router_id 51
    priority 150
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass supermap10I
    }
    virtual_ipaddress {
         192.168.100.8
    }
}
# 启动服务
systemctl start  keepalived.service && systemctl enable keepalived.service

slave配置启动keepalive

yum -y install keepalived.x86_64
# 修改 /etc/keepalived/keepalived.conf
# 其中 enp0s3 为绑定网卡名称,可以使用 ip addr 命令查看
# 其中 192.168.100.8  为虚拟 ip ,注意不要和其它 ip 冲突
! Configuration File for keepalived

global_defs {
   router_id NFS-Slave
}

vrrp_instance VI_1 {
    state MASTER
    interface enp0s3
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass superamp10I
    }
    virtual_ipaddress {
        192.168.100.8  
    }
}
# 启动服务
systemctl start  keepalived.service && systemctl enable keepalived.service

查看虚拟ip是否存在

ip a | grep  192.168.100.8
# 出现
# inet 192.168.100.8/32 scope global enp0s3
# 即成功

vip挂载测试

在iclient上执行

mount -t nfs 192.168.100.8:/data/nfs-k8s /qiyuesuodata
# 如/qiyuesuodata目录中有共享目录中文件则说明挂载成功
umount /qiyuesuodata/

模拟测试宕机,测试ip是否会飘移

# 在 Master 上关闭 keepalived
systemctl stop keepalived.service
# 执行ip a | grep  192.168.100.8会无输出则关闭成功
# 在 Slave 上查看
ip a | grep  192.168.100.8
# 出现
# inet 192.168.100.8/32 scope global enp0s3
# 即成

设置keepalived脚本监测nfs宕机

cd /usr/local/sbin
# 生成文件check_nfs.sh
#!/bin/sh
# 每秒执行一次
step=1 #间隔的秒数,不能大于60 
for (( i = 0; i < 60; i=(i+step) )); do 
  ###检查nfs可用性:进程和是否能够挂载
  /sbin/service nfs status &>/dev/null
  if [ $? -ne 0 ];then
    ###如果服务状态不正常,先尝试重启服务
    /sbin/service nfs restart
    /sbin/service nfs status &>/dev/null
    if [ $? -ne 0 ];then
       # 如服务仍不正常,停止 keepalived
       systemctl stop keepalived.service
    fi
  fi
  sleep $step 
done 

添加定时任务

chmod 777 /usr/local/sbin/check_nfs.sh
crontab -e
# 输入定时任务
* * * * *  /usr/local/sbin/check_nfs.sh &> /dev/null

在 Client 添加定时任务,当 Master 宕机时进行重新挂载

cd /usr/local/sbin
# 生成文件check_mount.sh

#!/bin/sh
# 每秒执行一次
step=1 #间隔的秒数,不能大于60 
for (( i = 0; i < 60; i=(i+step) )); do 
  mount=`df -Th|grep qiyuesuodata`
  if [ $mount = "" ];then
     umount /qiyuesuodata
     mount mount -t nfs 192.168.50.143:/data /qiyuesuodata
  fi
  sleep $step 
done 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

会灭火的程序员

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值