Keepalived 是一个基于VRRP协议来实现的LVS服务高可用方案,可以解决静态路由出现的单点故障问题。
所以,keepalived 一方面具有配置管理 LVS 的功能,同时还具有对 LVS 下面节点进行健康检查的功能,另一方面也可实现系统网络服务的高可用。
Keepalived是一款专为LVS和HA设计的一款健康检查工具:
keepalived体系架构中主要有三个模块,分别是core、check和vrrp
Keepalived可以通过读取自身的配置文件,实现通过更底层的接口直接管理LVS的配置以及控制服务的启动,停止功能。
Keepalived可以通过在自身的Keepalived.conf文件里配置LVS的节点IP和相关参数实现对LVS的直接管理;除此之外,当LVS集群中的某一个甚至是几个节点服务器同时发生故障无法提供服务时,Keepalived服务会自动将失效的节点服务器从LVS的正常转发队列中清除出去,并将请求调度到别的正常节点服务器上,从而保证最终用户的访问不受影响;当故障的节点服务器被修复以后,Keepalived服务又会自动地把它们加入到正常转发队列中,对客户提供服务。
一般企业集群需要满足的三个特点:负载均衡、健康检查、故障切换,使用 LVS + Keepalived 完全可以满足需求。
keepalived 高可用服务对集群之间的故障切换转移,是通过 VRRP(虚拟路由器冗余协议)来实现的。
在 keepalived 服务正常工作时,主(Master)节点会不断地向备(Backup)节点发送(多播的方式)心跳消息,用以告诉备节点自己还活看,当主节点发生故障时,就无法发送心跳消息,备节点也就因此无法继续检测到来自主节点的心跳了,于是调用自身的接管程序,接管主节点的 IP 资源及服务。而当主节点恢复时,备节点又会释放主节点故障时自身接管的 IP 资源及服务,恢复到原来的备用角色。
在高可用(HA)系统中,当联系2个节点的“心跳线”断开时,本来为一整体、动作协调的HA系统,就分裂成为2个独立的个体。由于相互失去了联系,都以为是对方出了故障。两个节点上的HA软件像“裂脑人”一样,争抢“共享资源”、争起“应用服务”,就会发生严重后果——或者共享资源被瓜分、2边“服务”都起不来了;或者2边“服务”都起来了,但同时读写“共享存储”,导致数据损坏(常见如数据库轮询着的联机日志出错)。
检测思路:正常情况下keepalived的VIP地址是在主节点上的,如果在从节点发现了VIP,就设置报警信息。脚本(在从节点上)如下:
vim split-brainc_check.sh
#!/bin/bash
# 检查脑裂的脚本,在备节点上进行部署
LB01_VIP=192.168.1.229
LB01_IP=192.168.1.129
LB02_IP=192.168.1.130
while true
do
ping -c 2 -W 3 $LB01_VIP &>/dev/null
if [ $? -eq 0 -a `ip add|grep "$LB01_VIP"|wc -l` -eq 1 ];then
echo "ha is brain."
else
echo "ha is ok"
fi
sleep 5
done
环境
主DR 服务器:192.168.154.19 ipvsadm、keepalived(热备) 虚拟IP:192.168.154.188 网卡ens33:0
备DR 服务器:192.168.154.20 ipvsadm、keepalived(热备) 虚拟IP:192.168.154.188 网卡ens33:0
Web 服务器1:192.168.154.21
Web 服务器2:192.168.154.22
vip:192.168.154.188
客户端:192.168.154.100
systemctl stop firewalld
setenforce 0
yum install -y ipvsadm keepalived
modprobe ip_vs
cat /proc/net/ip_vs
[root@zyf-dr1 ~]# ipvsadm-save > /etc/sysconfig/ipvsadm
[root@zyf-dr1 ~]# systemctl start ipvsadm.service
[root@zyf-dr1 ~]# cd /etc/keepalived/
[root@zyf-dr1 /etc/keepalived]#cp keepalived.conf keepalived.conf.bak
[root@zyf-dr1 /etc/keepalived]# ls
keepalived.conf keepalived.conf.bak
[root@zyf-dr1 /etc/keepalived]# vim keepalived.conf
[root@zyf-dr1 /etc/keepalived]# sysctl -p
[root@zyf-dr1 /etc/keepalived]# vim /etc/sysctl.conf
[root@zyf-dr1 /etc/keepalived]# sysctl -p
net.ipv4.ip_forward = 1
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.ens33.send_redirects = 0
[root@zyf-dr1 /etc/keepalived]# vim keepalived.conf
! Configuration File for keepalived
global_defs {
smtp_server 127.0.0.1
router_id LVS_01
vrrp_skip_check_adv_addr
# vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.154.188
}
}
virtual_server 192.168.154.188 80 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 0
protocol TCP
real_server 192.168.154.21 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.154.22 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
[root@zyf-dr1 /etc/keepalived]# scp keepalived.conf [email protected]:/etc/keepalived/
[email protected]'s password:
keepalived.conf 100% 1308 2.1MB/s 00:00
[root@zyf-dr1 /etc/keepalived]#
[root@zyf-dr1 /etc/sysconfig/network-scripts]# vim ifcfg-ens33:0
DEVICE=ens33:0
ONBOOT=yes
IPADDR=192.168.154.188
NETMASK=255.255.255.255
[root@zyf-dr1 /etc/sysconfig/network-scripts]# systemctl start keepalived.service
[root@zyf-dr1 /etc/sysconfig/network-scripts]# systemctl status keepalived.service
[root@zyf-dr1 ~]#
[root@zyf-dr1 ~]# ipvsadm -C
[root@zyf-dr1 ~]# ipvsadm -A -t 192.168.154.188:80 -s rr
[root@zyf-dr1 ~]# ipvsadm -a -t 192.168.154.188:80 -r 192.168.154.21:80 -g
[root@zyf-dr1 ~]# ipvsadm -a -t 192.168.154.188:80 -r 192.168.154.22:80 -g
[root@zyf-dr1 ~]# ipvsadm
vim /etc/sysctl.conf
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.ens33.send_redirects = 0
sysctl -p
systemctl stop firewalld
setenforce 0
yum install -y ipvsadm keepalived
[root@localhost ~]# modprobe ip_vs
[root@localhost ~]# cat /proc/net/ip_vs
[root@localhost ~]# ipvsadm-save > /etc/sysconfig/ipvsadm
[root@localhost ~]# systemctl start ipvsadm.service
[root@localhost network-scripts]# vim /etc/keepalived/keepalived.conf
[root@zyf-dr2 keepalived]# vim keepalived.conf
[root@zyf-dr2 keepalived]# cat keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
[email protected]
[email protected]
[email protected]
}
notification_email_from [email protected]
smtp_server 127.0.0.1
smtp_connetc_timeout 30
router_id LVS_02
vrrp_skip_check_adv_addr
#vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 51
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.154.188
}
}
virtual_server 192.168.154.188 80 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 0
protocol TCP
real_server 192.168.154.21 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.154.22 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
[root@zyf-dr2 keepalived]# cd /etc/sysconfig/network-scripts/
[root@zyf-dr2 network-scripts]# ls
ifcfg-ens33 ifdown-Team ifup-post
ifcfg-lo ifdown-TeamPort ifup-ppp
ifdown ifdown-tunnel ifup-routes
ifdown-bnep ifup ifup-sit
ifdown-eth ifup-aliases ifup-Team
ifdown-ib ifup-bnep ifup-TeamPort
ifdown-ippp ifup-eth ifup-tunnel
ifdown-ipv6 ifup-ib ifup-wireless
ifdown-isdn ifup-ippp init.ipv6-global
ifdown-post ifup-ipv6 network-functions
ifdown-ppp ifup-isdn network-functions-ipv6
ifdown-routes ifup-plip
ifdown-sit ifup-plusb
[root@zyf-dr2 network-scripts]# vim ifcfg-ens33:0
[root@zyf-dr2 network-scripts]# systemctl restart network
[root@zyf-dr2 network-scripts]# ifconfig
[root@zyf-dr2 network-scripts]# systemctl start keepalived.service
[root@zyf-dr2 network-scripts]# systemctl status keepalived.service
vim /etc/sysctl.conf
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.ens33.send_redirects = 0
[root@zyf-dr2 ~]# sysctl -p
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.ens33.send_redirects = 0
[root@zyf-web1 ~]# systemctl stop firewalld
[root@zyf-web1 ~]# setenforce 0
[root@zyf-web1 ~]# yum -y install httpd
[root@zyf-web1 ~]# systemctl start httpd
[root@zyf-web1 ~]# echo 'hello user1' > /var/www/html/index.html
[root@zyf-web1 ~]# vim /etc/sysconfig/network-scripts/ifcfg-lo:0
systemctl restart network
vim /etc/sysctl.conf
sysctl -p
[root@zyf-web2 ~]# systemctl stop firewalld
[root@zyf-web2 ~]# setenforce 0
yum -y install httpd
systemctl start httpd
[root@zyf-web2 ~]# echo 'hello user2' > /var/www/html/index.html
[root@zyf-web2 network-scripts]# vim ifcfg-lo:0
DEVICE=lo:0
ONBOOT=yes
IPADDR=192.168.154.188
NETMASK=255.255.255.255
systemctl restart network
[root@zyf-web2 ~]# vim /etc/sysctl.conf
[root@zyf-web2 ~]# sysctl -p
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
systemctl start keepalived
ip addr
systemctl stop keepalived
本文主要叙述了
LVS+keepalived群集的工作原理
Keepalived体系主要模块及其作用
LVS+DR+keepalived的配置实验