keepalived实现高可用性

keepalived弥补了lvs缺少的后端服务器的健康检查功能,同时实现了网关的冗余,避免单点故障.

实验拓扑

实验准备

  • keepalived
  • ipvsadm
  • centos7.4

实验步骤

Master 192.168.30.200

yum install -y keepalived ipvsadm
vim /etc/keepalived/keepalived.conf
vrrp_instance VI_1 {
state MASTER //初始化配置为主角色
interface ens34 //绑定接口
virtual_router_id 51 //虚拟路由器ID
priority 100 //数字越大优先级越高,成为主角色
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.30.254 //VIP
}
}
systemctl start keepalived
iptables -F // 清除iptables规则,不然web1 ping不通 192.168.30.254

查看接口网卡信息多了一个VIP

Backup 192.168.30.201

vim /etc/keepalived/keepalived.conf
vrrp_instance VI_1 {
state BACKUP //设定初始为从角色
interface ens34
virtual_router_id 51
priority 99 //优先级比主要低
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.30.254 //vip
}
}
iptables -F

vrrp协议在组播地址224.0.0.18广播心跳信息,一旦从节点未收到信息,立即提升为主设备,用户无感知到切换

模拟Master接口故障

web1 ping 192.168.30.254 查看切换情况
[root@web1 www1]# ping 192.168.30.254
模拟master接口故障
[root@master keepalived]# ifconfig ens34 down
在backup从节点抓包观察主从节点切换情况

PING的结果没有丢包,表示整个故障failover情况web并没有感知到

修复主节点网络接口后,Master有更高的优先级自动切换为主

定义外围脚本,实现动态调整优先级切换主从

192.168.30.200

`//定义脚本,如果down文件存在则优先级-2,从而切换为从角色,反之为主角色
vrrp_script chk_maintainace {
script “/bin/bash -c ‘[[ -e /etc/keepalived/down ]]’ && exit 1 || exit 0”
interval 1
weight -2
}

vrrp_instance VI_1 {
state MASTER
interface ens34
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.30.254
}

track_script {
    chk_maintainace   //调用脚本
}

}`

Leave a Reply

Your email address will not be published. Required fields are marked *