1.keepalive
- Keepalived 是一款轻量级HA集群应用,它的设计初衷是为了做LVS集群的HA,即探测LVS健康情况,从而进行主备切换,不仅如此,还能够探测LVS代理的后端主机的健康状况,动态修改LVS转发规则。
- 当LVS进行主备切换的时候,对外提供服务的IP是如何做到切换的呢?这就依赖于keepalived 所应用的vrrp协议,即Virtual Reduntant Routing Protocol,虚拟冗余路由协议。简单来讲,此协议是将IP设置在虚拟接口之上,根据一定的规则实现IP在物理主机上流动,即哪台主机可以占有该IP。
- 而且keepalived具有脚本调用接口,可通过脚本完成拓展功能。
1.1 安装keepalived
[root@haproxy_v1 ~]# yum install -y keepalived
#安装完成,真没啥说的。
#keepalived开启防火墙
[root@haproxy_v1 ~]# firewall-cmd --direct --permanent --add-rule ipv4 filter INPUT 0 --in-interface eth0 --destination 224.0.0.18 --protocol vrrp -j ACCEPT
#红色标记eth0是指的服务器的网卡名称,INPUT代表接收224.0.0.18的报文,使用 ip a 命令查看.主备都要操作
1.2 修改keepalived的配置文件
[root@haproxy_v1 ~]# cat /etc/keepalived/keepalived.conf
global_defs {
router_id LVS_01
}
vrrp_script check_haproxy {
script "/server/scripts/check_haproxy.sh"
interval 2
weight 2
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 150
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.77/24 dev eth0 label eth0:1
}
track_script {
check_haproxy
}
}
1.3 编写切换脚本
[root@haproxy_v1 ~]# cat /server/scripts/check_haproxy.sh
#!/bin/bash
A=`ps -C haproxy --no-header |wc -l`
if [ $A -eq 0 ];then
systemctl stop keepalived
fi
1.4 节点2 的配置文件
[root@haproxy_v2 scripts]# cat /etc/keepalived/keepalived.conf
global_defs {
router_id LVS_02
}
vrrp_script check_haproxy {
script "/server/scripts/check_haproxy.sh"
interval 2
weight 2
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.77/24 dev eth0 label eth0:1
}
track_script {
check_haproxy
}
}