🚀 多服务器网站部署架构指南 | 负载均衡与高可用性全方案
以下为专业级多服务器协同运行配置方案,涵盖流量分发、故障转移及数据同步等核心环节:
一、基础架构设计
[Cloudflare DNS]
|
+----------+----------+
| | |
[Nginx LB] [HAProxy LB] [AWS ALB] ← 多级负载均衡
| | |
+---+----------+-------+---+
| |
[Web Server 1] [Web Server 2] ← 无状态应用集群
| |
[Redis Sentinel] ← 会话共享集群
|
[MySQL Group Replication] ← 数据库多主同步
|
[Ceph Storage Cluster] ← 分布式文件存储
二、核心组件配置
1. 负载均衡层(以Nginx为例)
upstream backend {
zone backend 64k;
server 10.0.1.10:80 weight=3 max_fails=2 fail_timeout=30s;
server 10.0.1.11:80 weight=2 backup;
keepalive 32;
}
server {
listen 80;
location / {
proxy_pass http://backend;
proxy_next_upstream error timeout http_500;
proxy_set_header X-Real-IP $remote_addr;
# 健康检查
health_check interval=5s uri=/health_check;
}
}
2. 高可用性保障(Keepalived + VIP)
# Master节点配置
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
virtual_ipaddress {
10.0.0.100/24 dev eth0 label eth0:0 # 虚拟IP
}
}
3. 应用层自动同步(Ansible Playbook)
- hosts: webservers
tasks:
- name: 同步代码
git:
repo: git@github.com:your/repo.git
dest: /var/www/html
version: master
force: yes
notify: reload app
handlers:
- name: reload app
systemd:
name: your_service
state: reloaded
三、数据层协同方案
1. 数据库多主复制(MySQL Group Replication)
-- 初始化集群
SET GLOBAL group_replication_bootstrap_group=ON;
START GROUP_REPLICATION;
SET GLOBAL group_replication_bootstrap_group=OFF;
-- 新节点加入
CHANGE MASTER TO
MASTER_USER='repl',
MASTER_PASSWORD='password'
FOR CHANNEL 'group_replication_recovery';
START GROUP_REPLICATION;
2. 分布式文件存储(CephFS)
# 创建存储池
ceph osd pool create web_data 128
ceph fs new web_fs web_data web_metadata
# 客户端挂载
mount -t ceph :/web_fs /mnt/ceph -o name=admin,secretfile=/etc/ceph/admin.secret
四、流量调度策略
1. 智能路由规则
策略类型 | 配置示例 | 适用场景 |
---|---|---|
加权轮询 | server 10.0.1.10 weight=5; |
异构服务器性能分配 |
最少连接数 | least_conn; |
长连接服务(WebSocket) |
IP哈希 | hash $remote_addr consistent; |
会话保持需求 |
地理路由 | geoip_country /etc/nginx/geoip; |
区域化服务部署 |
2. 自动扩缩容(Kubernetes示例)
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: web-app-autoscaler
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: web-app
minReplicas: 2
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 70
五、故障转移与监控
1. 多级健康检查
层级 | 检查方式 | 阈值 | 动作
-----|--------------------------|---------|---------------------
L4 | TCP端口探测 | 3次/5s | 标记节点不可用
L7 | HTTP 200检测 | 5次/10s | 触发服务重启
业务 | 自定义API健康端点 | 连续失败| 触发报警并隔离节点
2. Prometheus监控规则
- alert: HighErrorRate
expr: sum(rate(nginx_http_requests_total{status=~"5.."}[5m]))
/ sum(rate(nginx_http_requests_total[5m])) > 0.05
for: 10m
labels:
severity: critical
annotations:
summary: "高错误率告警 (实例 {{ $labels.instance }})"
六、成本优化建议
-
混合调度策略
- 高峰时段:启用全部云服务器+本地物理机
- 低峰时段:仅保留2台云服务器+本地冷备
-
分级存储方案
Hot层:NVMe云盘 → 存放动态页面 Warm层:本地SSD阵列 → 用户上传内容 Cold层:AWS S3 Glacier → 日志归档
🔧 部署验证命令
# 测试负载均衡
for i in {1..10}; do curl http://vip.domain.com; done
# 模拟节点故障
iptables -A INPUT -p tcp --dport 80 -j DROP # 在Web1执行
# 观察流量切换
tail -f /var/log/nginx/access.log | grep 'HTTP/1.1" 50[0-9]'
📌 架构升级路径
- 初期:DNS轮询 + Rsync同步
- 中期:Nginx负载均衡 + Ansible自动化
- 高级:Kubernetes集群 + 服务网格