Keepalived+NFS+SHELL脚本实现NFS-HA高可用

Posted 奋斗的寒霜

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Keepalived+NFS+SHELL脚本实现NFS-HA高可用相关的知识,希望对你有一定的参考价值。

本来想做DRBD+HEARTBEAT,但是领导说再加硬盘浪费资源,没有必要,而且在已有硬盘上做风险较大,所以就只能用rsync来实现数据同步了,实验中发现很多的坑,都用脚本和计划任务给填上了,打算把这套直接用在生产环境中,到时候如果还遇到什么问题,再进行修正和补全,下面是项目细节:

主机配置:

web:192.168.6.10 Centos 6.4

nfs1:192.168.6.1 Centos 6.4

nfs2:192.168.6.2 Centos 6.4

keepalived 1.2.13 VIP:192.168.6.105

1.安装NFS和keepalived

这部分比较简单,直接用yum -y install nfs keepalived

2.配置NFS(两台同样配置)

mkdir /home/shares

vim /etc/exports

/home/shares 192.168.6.0/24(rw,sync,no_root_squash)

cd /home/shares

echo aaa > /home/shares/a.txt #用作后面脚本判断

touch files{1..10}

service nfs start

3.配置keepalived

vim /etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {
router_id LVS_DEVEL
}

vrrp_instance VI_1 {
state MASTER  #辅改成BACKUP
interface eth0
virtual_router_id 51
priority 100      #辅改成50
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.6.105
}
}

service keepalived start

web:mount -t nfs 192.168.6.1:/home/shares /shares

4.ssh免密登录

ssh-keygen

ssh-copy-id

5.shell脚本

cd /etc/keepalived

vim notify_master.sh   #判断NFS端口是否存在,不存在就停止keepalived服务

#!/bin/bash

port=`netstat -anp|grep 2049`
if [ "$port" == "" ];then
/sbin/service keepalived stop
fi

vim change.sh(两台NFS都需要)  

#!/bin/bash
ip=`/sbin/ip a|grep "eth0"|grep "105"`
web="192.168.6.10"
if [ "$ip" != "" ];then  #判断VIP是否存在,如果存在就表示现在这台是master
/usr/bin/ssh $web "cat /shares/a.txt"  #在web上运行命令,查看/shares目录是否可用,执行cat a.txt消耗内存cpu较小
if [ "$?" != 0 ];then  #如果不可用$?返回值不为0,则卸载原来的/shares目录,再重新挂载这个目录,这样做的原因是,VIP切换的时候,必须重新挂载/shares,否则不能使用这个目录
/usr/bin/ssh $web "umount /shares&&mount -t nfs 192.168.6.105:/home/shares /shares"
fi
fi

chmod +x change.sh

chmod +x notify_master.sh

6.计划任务crontab

crontab -e

* * * * * /etc/keepalived/notify_master.sh  #每分钟检查一次NFS服务是否正常
* * * * * /etc/keepalived/change.sh  #每分钟检查一次keepalived状态

0 00 * * * rsync /home/shares 192.168.6.2:/home/shares  #每天夜间同步数据




























以上是关于Keepalived+NFS+SHELL脚本实现NFS-HA高可用的主要内容,如果未能解决你的问题,请参考以下文章

nfs主主模式

keepalived用脚本实现切换

activitmq+keepalived+nfs 非zk的高可用集群构建

测试NFS+rsync+sersync+SMTP+shell脚本+cron

keepalived实战

keepalived实战