为什么需要负载均衡?
凌晨4点11分,我看着监控面板:Agent-1 CPU 100%,Agent-2 CPU 5%,Agent-3 CPU 3%。
这就是没有负载均衡的后果——有的Agent累成狗,有的Agent在摸鱼。
没有负载均衡:
请求1 → Agent-1 (CPU 100% 🔥)
请求2 → Agent-1 (CPU 100% 🔥)
请求3 → Agent-1 (崩溃了 💥)
Agent-2 (CPU 5% 😴) Agent-3 (CPU 3% 😴)
有负载均衡:
请求1 → Agent-1 (CPU 40%)
请求2 → Agent-2 (CPU 40%)
请求3 → Agent-3 (CPU 40%)
😌 均匀、稳定、高可用
请求1 → Agent-1 (CPU 100% 🔥)
请求2 → Agent-1 (CPU 100% 🔥)
请求3 → Agent-1 (崩溃了 💥)
Agent-2 (CPU 5% 😴) Agent-3 (CPU 3% 😴)
有负载均衡:
请求1 → Agent-1 (CPU 40%)
请求2 → Agent-2 (CPU 40%)
请求3 → Agent-3 (CPU 40%)
😌 均匀、稳定、高可用
🎯 负载均衡四大价值:
- 高可用:一个Agent挂了,其他顶上
- 水平扩展:流量大了,加Agent实例就行
- 性能优化:充分利用所有Agent的计算能力
- 故障隔离:坏实例自动摘除,不影响服务
OpenClaw 负载均衡配置
基础负载均衡配置
# openclaw-load-balancer.yaml
name: "openclaw-agent-lb"
version: "1.0"
# 负载均衡器监听
lb:
listen: "0.0.0.0:9090"
health_check:
enabled: true
interval: "10s"
timeout: "3s"
healthy_threshold: 2
unhealthy_threshold: 3
# Agent实例池
backend_pool:
name: "agent-pool-1"
algorithm: "round_robin" # 轮询算法
instances:
- id: "agent-1"
host: "agent1.internal:5000"
weight: 1
max_connections: 100
- id: "agent-2"
host: "agent2.internal:5000"
weight: 1
max_connections: 100
- id: "agent-3"
host: "agent3.internal:5000"
weight: 1
max_connections: 100
# 健康检查端点
health_endpoint: "/health"
health_expected_status: 200
# 故障转移
failover:
enabled: true
max_retries: 3
retry_interval: "1s"
fallback_response:
status: 503
body: '{"error": "所有Agent实例不可用", "code": "SERVICE_UNAVAILABLE"}'
启动负载均衡器
# 安装负载均衡Skill
openclaw skill install agent-load-balancer
# 启动负载均衡器
openclaw lb start --config openclaw-load-balancer.yaml
# 查看实例状态
openclaw lb status
# 输出示例:
# Agent实例状态:
# agent-1: ✅ 健康 (CPU 45%, MEM 60%, 活跃连接: 23)
# agent-2: ✅ 健康 (CPU 38%, MEM 55%, 活跃连接: 19)
# agent-3: ⚠️ 降级 (CPU 85%, MEM 80%, 活跃连接: 45)
#
# 负载均衡算法:round_robin
# 总请求:15234,成功率:99.2%
# 动态调整权重(agent-3压力太大,降低权重)
openclaw lb set-weight agent-3 0.5
四种负载均衡算法
🔄 1. 轮询(Round Robin)
请求依次分配给每个Agent,简单公平。
algorithm: "round_robin"
# 请求1→agent-1, 请求2→agent-2, 请求3→agent-3, 请求4→agent-1...
适用场景:Agent实例配置相同,无状态服务。
⚖️ 2. 加权轮询(Weighted Round Robin)
配置高的Agent多分点流量。
instances:
- id: "agent-strong" # 高配机器
weight: 3 # 拿3倍流量
- id: "agent-weak" # 低配机器
weight: 1 # 拿1倍流量
适用场景:Agent实例配置不同。
📊 3. 最少连接(Least Connections)
谁闲给谁,动态负载均衡。
algorithm: "least_connections"
# Agent-1有5个活跃连接,Agent-2有2个 → 新请求给Agent-2
适用场景:请求处理时间差异大,长连接场景。
🧠 4. 智能路由(Smart Routing)
根据请求特征(用户ID、请求类型)路由到特定Agent。
algorithm: "smart_routing"
routing_rules:
- match: {user_tier: "vip"}
target: "agent-premium" # VIP用户走高端Agent
- match: {request_type: "code_generation"}
target: "agent-code" # 代码生成走专用Agent
- default: "agent-pool-general"
适用场景:需要会话保持、专用Agent的场景。
健康检查与自动摘除
负载均衡器会定期检查每个Agent实例的健康状态,不健康的自动摘除。
# 健康检查配置
health_check:
enabled: true
endpoint: "/health"
interval: "5s"
timeout: "2s"
# 健康判定
healthy_threshold: 2 # 连续2次成功才算健康
unhealthy_threshold: 3 # 连续3次失败才算不健康
# 检查内容
expected_status: 200
expected_body_contains: '"status":"healthy"'
# Agent自定义健康检查逻辑
agent_health_script: |
#!/bin/bash
# 检查Agent进程
pgrep -f "openclaw-agent" > /dev/null || exit 1
# 检查内存使用(超过90%认为不健康)
mem_usage=$(free | grep Mem | awk '{print $3/$2 * 100.0}')
if (( $(echo "$mem_usage > 90" | bc -l) )); then
exit 1
fi
exit 0
# 查看健康检查日志
tail -f /var/log/openclaw/lb-health-check.log
会话保持(Session Affinity)
某些场景需要同一个用户始终路由到同一个Agent(比如有本地缓存)。
# 会话保持配置
session_affinity:
enabled: true
method: "cookie" # 或 "ip_hash"
cookie_name: "OPENCLAW_SESSION"
ttl: "1h"
# 用户第一次请求,负载均衡器设置cookie
# 后续请求根据cookie路由到同一Agent
# IP Hash模式(根据用户IP哈希)
session_affinity:
enabled: true
method: "ip_hash"
# 同一IP总是路由到同一Agent
性能对比与调优
| 算法 | 吞吐量(req/s) | 延迟P99 | 适用场景 |
|---|---|---|---|
| 轮询 | 1200 | 45ms | 无状态、实例同配 |
| 加权轮询 | 1350 | 42ms | 实例配置不同 |
| 最少连接 | 1500 | 38ms | 长连接、处理时间不均 |
| 智能路由 | 1800 | 35ms | 多租户、专用Agent |
监控指标
# 负载均衡器暴露的Prometheus指标
openclaw_lb_requests_total{instance="agent-1",status="200"} 4532
openclaw_lb_requests_total{instance="agent-2",status="200"} 4489
openclaw_lb_requests_total{instance="agent-3",status="500"} 23
openclaw_lb_active_connections{instance="agent-1"} 23
openclaw_lb_active_connections{instance="agent-2"} 19
openclaw_lb_health_status{instance="agent-1"} 1 # 1=健康, 0=不健康
openclaw_lb_response_time_ms{instance="agent-1",p="p99"} 45.2
# Grafana Dashboard关键Panel:
# 1. 请求QPS(按实例分组)
# 2. 错误率(按实例分组)
# 3. P95/P99延迟
# 4. 活跃连接数
# 5. 实例健康状态(红/绿)
⚠️ 避坑指南:
- 健康检查间隔别太短(<5s),会增加Agent负担
- 会话保持会增加故障影响面(Agent挂了,相应用户全掉线)
- 权重调整要渐进,别一下子从1改到10
- 监控要跟上,不然不知道哪个Agent在摸鱼
🎯 妙趣金句:
"负载均衡就像公司分任务——能力强(高配)的多分点,能力弱(低配)的少分点,谁闲着就给谁。别让一个人累死,其他人看戏。"
实战:多租户负载均衡
# multi-tenant-lb.yaml
lb:
listen: "0.0.0.0:9090"
# 按租户分池
pools:
- name: "pool-vip"
instances: ["agent-vip-1:5000", "agent-vip-2:5000"]
algorithm: "least_connections"
max_qps: 500
- name: "pool-standard"
instances: ["agent-std-1:5000", "agent-std-2:5000", "agent-std-3:5000"]
algorithm: "round_robin"
max_qps: 200
- name: "pool-free"
instances: ["agent-free-1:5000"]
algorithm: "round_robin"
max_qps: 50
# 路由规则
routing:
- match: {user_tier: "vip"}
pool: "pool-vip"
- match: {user_tier: "standard"}
pool: "pool-standard"
- default: "pool-free"