在互联网服务架构中,高可用性和负载均衡是两个永恒的话题。今天我要分享的是基于LVS-DR和Keepalived实现的双机热备高可用负载均衡方案,这个架构在我们电商平台的支付网关中已经稳定运行了三年多,处理峰值可达每秒5000+请求。
这个方案的核心价值在于:
code复制客户端 → VIP(192.168.10.100) → 主调度器(71) / 备调度器(70) → Real Server(72/73)
| 组件 | 作用 |
|---|---|
| LVS | 核心流量调度(DR模式),负责把VIP的请求分发到后端Real Server |
| Keepalived | 基于VRRP协议实现:VIP漂移、健康检查、自动同步LVS规则 |
| Real Server | 实际处理请求的应用服务器(本文示例为HTTP服务) |
DR模式是LVS三种工作模式中性能最优的一种,其核心特点是:
这种非对称路径设计使得调度器不会成为带宽瓶颈,特别适合高流量场景。实现的关键在于:
| 角色 | IP地址 | 主机名 | 备注 |
|---|---|---|---|
| Director Server(主) | 192.168.10.71 | lvs-master | 主调度器,初始持有VIP |
| Director Server(备) | 192.168.10.70 | lvs-backup | 备调度器,随时接管VIP |
| Real Server 1 | 192.168.10.72 | web-node1 | 运行HTTP服务 |
| Real Server 2 | 192.168.10.73 | web-node2 | 运行HTTP服务 |
| VIP | 192.168.10.100 | - | 客户端访问的统一入口 |
bash复制# 安装必要软件
yum install -y keepalived ipvsadm httpd net-tools sysstat
# 关闭防火墙和SELinux(生产环境建议精细配置)
systemctl stop firewalld && systemctl disable firewalld
setenforce 0 && sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
# 配置时间同步(关键!VRRP依赖时间同步)
yum install -y ntpdate
ntpdate ntp.aliyun.com
注意:生产环境中建议配置chronyd做持续时间同步,避免VRRP脑裂
bash复制# 临时绑定VIP(测试用)
ifconfig ens33:0 192.168.10.100 netmask 255.255.255.0 up
# 永久配置(写入网卡配置文件)
cat > /etc/sysconfig/network-scripts/ifcfg-ens33:0 << EOF
DEVICE=ens33:0
BOOTPROTO=static
IPADDR=192.168.10.100
NETMASK=255.255.255.0
ONBOOT=yes
EOF
# 应用配置
ifdown ens33:0 && ifup ens33:0
bash复制cat >> /etc/sysctl.conf << EOF
# 关闭ICMP重定向(DR模式必须)
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.ens33.send_redirects = 0
# 开启端口重用和快速回收
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
# 增大连接跟踪表大小
net.ipv4.vs.conn_tab_bits=20
EOF
sysctl -p
bash复制cat > /etc/keepalived/keepalived.conf << 'EOF'
! Configuration File for keepalived
global_defs {
router_id LVS_MASTER
enable_script_security
}
vrrp_script chk_httpd {
script "/usr/bin/curl -s -o /dev/null -w '%{http_code}' http://localhost | grep 200"
interval 2
weight -5
fall 2
rise 1
}
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.10.100/24 dev ens33 label ens33:0
}
track_script {
chk_httpd
}
}
virtual_server 192.168.10.100 80 {
delay_loop 6
lb_algo rr
lb_kind DR
protocol TCP
real_server 192.168.10.72 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.10.73 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
EOF
启动服务:
bash复制systemctl enable --now keepalived
备机配置与主机基本相同,只需修改三个参数:
bash复制cat > /etc/keepalived/keepalived.conf << 'EOF'
global_defs {
router_id LVS_BACKUP # 修改为BACKUP标识
}
vrrp_instance VI_1 {
state BACKUP # 修改为BACKUP状态
priority 90 # 优先级低于MASTER
# 其他配置与主节点保持一致
}
EOF
bash复制# 72节点
echo "<h1>Real Server 72 - 192.168.10.72</h1>" > /var/www/html/index.html
# 73节点
echo "<h1>Real Server 73 - 192.168.10.73</h1>" > /var/www/html/index.html
systemctl enable --now httpd
bash复制# 临时绑定
ifconfig lo:0 192.168.10.100 netmask 255.255.255.255 up
# 永久配置
echo "ifconfig lo:0 192.168.10.100 netmask 255.255.255.255 up" >> /etc/rc.local
chmod +x /etc/rc.local
bash复制cat >> /etc/sysctl.conf << EOF
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
EOF
sysctl -p
bash复制# 查看VIP绑定情况(主节点应有VIP)
ip addr show ens33 | grep 192.168.10.100
# 查看IPVS规则
ipvsadm -ln
预期输出:
code复制TCP 192.168.10.100:80 rr
-> 192.168.10.72:80 Route 1 0 0
-> 192.168.10.73:80 Route 1 0 0
bash复制# 在主节点停止keepalived
systemctl stop keepalived
# 在备节点检查VIP是否漂移
ip addr show ens33 | grep 192.168.10.100
# 恢复主节点
systemctl start keepalived
建议使用自定义脚本进行更精细的健康检查:
bash复制cat > /etc/keepalived/check_nginx.sh << 'EOF'
#!/bin/bash
if [ "$(curl -s -o /dev/null -w '%{http_code}' http://localhost/health)" != "200" ]; then
exit 1
fi
EOF
chmod +x /etc/keepalived/check_nginx.sh
bash复制authentication {
auth_type PASS
auth_pass 复杂密码应大于8位
}
bash复制iptables -A INPUT -i ens33 -d 224.0.0.0/8 -j ACCEPT
iptables -A INPUT -i ens33 -p vrrp -j ACCEPT
bash复制# 增大LVS连接哈希表
echo "net.ipv4.vs.conn_tab_bits=20" >> /etc/sysctl.conf
# 调整Keepalived检测间隔
vrrp_instance VI_1 {
advert_int 1 # 默认1秒,网络不稳定可适当增大
}
检查步骤:
journalctl -u keepalived可能原因:
排查方法:
bash复制# 在调度器上测试
curl -v http://192.168.10.72
# 检查IPVS规则
ipvsadm -ln
# 检查Real Server的ARP抑制是否生效
cat /proc/sys/net/ipv4/conf/all/arp_ignore
建议部署以下监控项:
示例Zabbix监控项:
bash复制# VIP状态检查
ip addr show ens33 | grep 192.168.10.100 | wc -l
# IPVS连接数统计
ipvsadm -ln | grep -c "192.168.10.100:80"
这套架构我们已经在大促期间承受了数亿级别的请求量,关键是要做好容量规划:
最后分享一个实用技巧:在升级维护时,可以临时降低主节点优先级实现无损切换:
bash复制# 在主节点执行
keepalived -p 50