###实验环境:
nfs1 192.168.119.110 keepalived nfs drbd
nfs2 192.168.119.111 keepalived nfs drbd
vip 192.168.119.140
web 192.168.119.120 nginx
###安装软件:
在nfs1、nfs2上分别安装软件keepalived nfs drbd
drbd安装与配置 https://blog.csdn.net/harryxxxxx/article/details/80938951
keepalived安装与配置 https://blog.csdn.net/harryxxxxx/article/details/80803389
nfs安装与配置 https://blog.csdn.net/harryxxxxx/article/details/81078087
在web上安装nginx并配置 与nfs(做测试用)
###配置文件:
- keepalived
nfs1
! Configuration File for keepalived
global_defs { #全局定义部分
script_user root
enable_script_security
router_id NFS1 #id_唯一标识
}
vrrp_script chk_nfs {
script "/etc/keepalived/chk_nfs.sh"
interval 3
weight -20
fall 3
}
vrrp_instance VI_1 {
state MASTER
interface ens32
virtual_router_id 61
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.119.140
}
track_script {
chk_nfs
}
notify_master /etc/keepalived/notify_master.sh
}
nfs2
! Configuration File for keepalived
global_defs { #全局定义部分
script_user root
enable_script_security
router_id NFS2 #id_唯一标识
}
vrrp_instance VI_1 {
state BACKUP
interface ens32
virtual_router_id 61
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.119.140
}
notify_master /etc/keepalived/notify_master.sh
notify_backup /etc/keepalived/notify_backup.sh
}
drbd 与nfs两个节点配置相同,参考安装文档,
nginx不在本文范围内
###用到的脚本:
vim chk_nfs.sh
#!/bin/bash
/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 nfs
/usr/bin/umount -lf /data
/usr/sbin/drbdadm secondary web
/usr/bin/kill -15 `cat /var/run/keepalived.pid`
exit 1
fi
exit 0
fi
vim notify_master.sh
#!/bin/bash
sleep 2
/usr/sbin/drbdadm primary web
/usr/bin/mount /dev/drbd0 /data
/usr/bin/systemctl restart nfs
vim notify_backup.sh
#!/bin/bash
/usr/bin/umount -lf /data
/usr/sbin/drbdadm secondary web
/usr/bin/systemctl stop nfs
###测试:
nfs1上 将chk_nfs.sh中5 6 7 13 14行注释掉,然后停掉nfs服务,观察是否自动切换
由上两张图可知,vip已经漂移到nfs2上
查看drbd状态
同样可知 nfs2为主,nfs1为次
查看挂载情况
nfs1上 /data 已经被umount nfs2上挂载成功
测试机也能正常访问
####恢复nfs1上chk_nfs.sh脚本,然后启动keepalived,观察是否还原
同样先观察ip
vip 已经漂移回来
drbd状态
同样还原回nfs1为主 nfs2为次
挂载情况
同样还原回nfs1
测试链接同样正常