Linux 服务高可用架构设计与部署:Keepalived + HAProxy + 多实例最佳实践
一、高可用基础概念与设计原则
高可用核心指标:
- RTO(Recovery Time Objective):恢复时间目标(目标 < 30 秒)
- RPO(Recovery Point Objective):数据丢失目标(目标 = 0)
- 可用性:99.99%(四九)以上,即年停机时间 < 52 分钟
常见高可用模式:
- Active-Passive(主备):Keepalived 经典模式,一主一备
- Active-Active(双主/多活):HAProxy + 多后端实例
- 集群模式:Pacemaker + Corosync(更复杂场景)
设计原则:
- 快速切换:VRRP 心跳 < 1 秒,抢占延迟可控
- 数据同步:MySQL 主从、Redis Sentinel、共享存储
与 Systemd 结合:使用模板单元(@.service)实现多实例,所有服务通过 systemd 统一管理。
二、Keepalived 原理与 VRRP 配置详解
Keepalived 工作原理: Keepalived 使用 VRRP(Virtual Router Redundancy Protocol)协议实现 VIP(Virtual IP)漂移。主节点广播心跳,备节点监听;主节点故障后,备节点提升优先级并接管 VIP。
1. 安装与基础配置
yum install keepalived -y # Rocky/CentOSapt install keepalived -y # Ubuntusystemctl enable --now keepalived
2. 主节点 keepalived.conf(/etc/keepalived/keepalived.conf)
global_defs { router_id HA_WEB_01 vrrp_skip_check_adv_addr vrrp_garp_master_delay 1 vrrp_garp_master_repeat 5}vrrp_instance VI_1 { state MASTER interface eth0 # 实际网卡 virtual_router_id 51 # 同一组必须相同 priority 100 # 主节点更高 advert_int 1 # 心跳间隔 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.1.100/24 # VIP } track_interface { eth0 } # 健康检查脚本 track_script { chk_http_port }}vrrp_script chk_http_port { script "/etc/keepalived/check_http.sh" interval 2 weight -20 # 失败后降低优先级 fall 3 rise 2}
3. 备节点配置 仅修改 state BACKUP 和 priority 90。
4. 健康检查脚本示例(check_http.sh)
#!/bin/bashcurl -s --connect-timeout 2 --max-time 3 http://127.0.0.1:80/health > /dev/nullif [ $? -eq 0 ]; thenexit 0elseexit 1fi
5. 高级功能:
notify_master / notify_backup 脚本实现切换时执行动作(如重启服务、发通知)
生产建议:使用独立心跳网卡(低延迟、低丢包),避免脑裂(split-brain)。
三、HAProxy 负载均衡深度配置
HAProxy 是高性能 TCP/HTTP 负载均衡器,支持四层和七层模式。
1. 安装与 systemd 服务
yum install haproxy -y
2. 生产级 haproxy.cfg
global log /dev/log local0 chroot /var/lib/haproxy pidfile /var/run/haproxy.pid maxconn 50000 user haproxy group haproxy daemon stats socket /var/run/haproxy.sock mode 660 level admin nbproc 4 # 多进程 cpu-map auto:1/1-4 0-3defaults log global mode http option httplog option dontlognull timeout connect 5s timeout client 30s timeout server 30s timeout check 2sfrontend web_front bind 192.168.1.100:80 bind 192.168.1.100:443 ssl crt /etc/ssl/cert.pem acl health_check path /health http-request deny if health_check default_backend web_serversbackend web_servers balance roundrobin option httpchk GET /health http-check expect status 200 server web01 192.168.1.11:8080 check weight 100 maxconn 2000 server web02 192.168.1.12:8080 check weight 100 maxconn 2000 server web03 192.168.1.13:8080 check backup # 备用节点 # 动态权重 # server-template web 10 192.168.1.1:8080 check
3. 高级特性:
- 会话保持:
cookie SERVERID insert indirect nocache - 流量镜像:
option tcp-request content track-sc0 src table abuse
与 Systemd 集成:将 HAProxy 配置为 systemd 服务,启用 Restart=always 和资源限制。
四、多实例服务部署(模板单元 + 负载均衡)
使用 Systemd 模板单元实现水平扩展: 文件:/etc/systemd/system/app@.service
[Unit]Description=Spring Boot App Instance %iAfter=network-online.target[Service]Type=simpleUser=appuserWorkingDirectory=/opt/appExecStart=/usr/bin/java -jar -Dserver.port=%i app.jarEnvironment=SPRING_PROFILES_ACTIVE=prodRestart=alwaysRestartSec=3LimitNOFILE=65535CPUQuota=150%MemoryMax=2GSlice=app.slice[Install]WantedBy=multi-user.target
启动多个实例:
systemctl enable --now app@{8080..8085}.service
HAProxy 后端自动发现这些实例,实现真正的 Active-Active。
五、完整高可用 Web 架构实战案例
架构拓扑:
- 2 台 HAProxy + Keepalived(VIP 漂移)
- 4 台 Web 节点(Nginx + App 多实例)
- 2 台 MySQL(主从 + MHA/Orchestrator)
配置要点:
- Keepalived 管理 VIP → HAProxy
零停机部署流程:
六、生产环境高可用最佳实践
- Prometheus 监控 Keepalived VRRP 状态
- Alertmanager 规则:VIP 切换、后端 Down 比例过高
- HAProxy 启用 WAF 模块(lua 脚本)
七、常见高可用故障排查案例
案例1:脑裂导致双 VIP 症状:两个节点同时 Master。 解决:
案例2:HAProxy 后端健康检查失败 排查:
echo"show stat" | socat /var/run/haproxy.sock stdiohaproxy -c -f /etc/haproxy/haproxy.cfgjournalctl -u haproxy
案例3:VIP 漂移后 ARP 缓存未更新 解决:
- Keepalived 配置
vrrp_garp_master_repeat
案例4:多实例端口冲突 解决:严格使用模板单元 %i 参数化端口。
案例5:切换时连接中断 优化:HAProxy 配置 timeout client-fin + 后端 graceful shutdown。