Keepalived + VRRP
- 实现虚拟IP(VIP)的自动切换
- 配置示例:
conf
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
}
}
Pacemaker + Corosync
- 企业级集群资源管理器
- 常用命令:
bash
crm configure property stonith-enabled=false
crm configure primitive VirtualIP ocf:heartbeat:IPaddr2 params ip=192.168.1.100 cidr_netmask=24 op monitor interval=30s
MySQL主从复制/MGR
-- 主库配置
CHANGE MASTER TO MASTER_HOST='slave1', MASTER_USER='repl', MASTER_PASSWORD='password', MASTER_AUTO_POSITION=1;
START SLAVE;
-- MGR配置
SET GLOBAL group_replication_bootstrap_group=ON;
START GROUP_REPLICATION;
SET GLOBAL group_replication_bootstrap_group=OFF;
Redis Sentinel
sentinel monitor mymaster 127.0.0.1 6379 2
sentinel down-after-milliseconds mymaster 5000
sentinel failover-timeout mymaster 10000
LVS (Linux Virtual Server)
- NAT模式配置:
bash
ipvsadm -A -t 192.168.1.100:80 -s rr
ipvsadm -a -t 192.168.1.100:80 -r 192.168.1.101:80 -m
ipvsadm -a -t 192.168.1.100:80 -r 192.168.1.102:80 -m
HAProxy TCP模式
frontend web_front
bind *:80
mode tcp
default_backend web_back
backend web_back
mode tcp
balance roundrobin
server web1 192.168.1.101:80 check
server web2 192.168.1.102:80 check
Nginx负载均衡
upstream backend {
least_conn;
server backend1.example.com weight=5;
server backend2.example.com;
server backup1.example.com backup;
}
server {
location / {
proxy_pass http://backend;
}
}
HAProxy HTTP模式
frontend http_front
bind *:80
mode http
default_backend http_back
backend http_back
mode http
balance leastconn
cookie SERVERID insert indirect nocache
server web1 192.168.1.101:80 cookie web1 check
server web2 192.168.1.102:80 cookie web2 check
自定义脚本示例
#!/bin/bash
if ! curl -I http://localhost/health-check >/dev/null 2>&1; then
systemctl restart nginx
if [ $? -ne 0 ]; then
pkill -9 nginx && systemctl start nginx
fi
fi
Prometheus + Grafana
- Prometheus配置示例:
yaml
scrape_configs:
- job_name: 'node'
static_configs:
- targets: ['192.168.1.101:9100', '192.168.1.102:9100']
Zabbix监控
- 自动恢复动作配置:
触发器: Web服务器不可用
动作: 执行远程命令重启服务
多层级冗余设计
渐进式故障转移策略
负载均衡算法选择
定期故障演练
通过以上方案的综合应用,可以构建一个高可用、高性能的Linux服务架构,有效应对各种故障场景和负载压力。