🏢 OpenClaw多租户部署实战:隔离、配额、计费

凌晨2点11分,我看着第17个客户要求"独立环境"的需求单,突然悟了:"多租户部署,就像合租房——既要让大家住得舒服,又要防止有人偷用别人的WiFi。"

如果你也想把OpenClaw做成SaaS平台,或者给公司内部多个团队用,那这篇指南就是你的"租房合同"。🏠

┌─────────────────────────────────────────────┐
│           OpenClaw Gateway                  │
│  ┌──────────┐  ┌──────────┐  ┌──────────┐ │
│  │ Tenant A │  │ Tenant B │  │ Tenant C │ │
│  │ Namespace│  │ Namespace│  │ Namespace│ │
│  │ · Skills │  │ · Skills │  │ · Skills │ │
│  │ · Models │  │ · Models │  │ · Models │ │
│  │ · Data   │  │ · Data   │  │ · Data   │ │
│  └──────────┘  └──────────┘  └──────────┘ │
│        隔离边界 (Namespace)                  │
└─────────────────────────────────────────────┘
        

🎯 多租户架构选择

方案 优点 缺点 适用场景
命名空间隔离 轻量级、资源共享 隔离性较弱 SaaS平台、企业内部
容器隔离 强隔离、独立扩展 资源开销大 高安全需求、大客户
独立实例 完全隔离 运维成本高 VIP客户、合规要求

🔧 方案一:命名空间隔离(推荐)

1. 创建租户命名空间

# 创建租户A的命名空间 openclaw namespace create tenant-a \ --display-name="客户A" \ --quota-cpu=4 \ --quota-memory=8Gi \ --quota-skills=50 # 创建租户B的命名空间 openclaw namespace create tenant-b \ --display-name="客户B" \ --quota-cpu=2 \ --quota-memory=4Gi \ --quota-skills=20 # 查看所有命名空间 openclaw namespace list

2. 配置租户隔离策略

# 为租户A配置独立模型路由 openclaw config set namespaces.tenant-a.models.primary "gpt-4o" openclaw config set namespaces.tenant-a.models.fallback "claude-3.5-sonnet" # 为租户A配置独立Skills目录 openclaw config set namespaces.tenant-a.skills.path "/var/lib/openclaw/tenants/tenant-a/skills" openclaw config set namespaces.tenant-a.skills.isolation "strict" # 启用跨租户访问控制 openclaw config set security.crossNamespaceAccess false

3. 租户认证与授权

# 为租户创建API Key openclaw apikey create \ --namespace=tenant-a \ --name="tenant-a-production" \ --rate-limit=1000/hour \ --expires-in=365d # 输出示例: # API Key: sk-tenant-a-xxxxxxxxxxxxxxxx # 请妥善保管,此密钥只显示一次! # 为租户配置OAuth2(可选) openclaw auth configure \ --namespace=tenant-a \ --provider=oauth2 \ --client-id="${OAUTH_CLIENT_ID}" \ --client-secret="${OAUTH_CLIENT_SECRET}" \ --callback-url="https://tenant-a.app.com/callback"

📊 资源配额与计量

1. 设置资源配额

# 为租户设置详细的资源配额 cat > tenant-a-quota.yaml << 'EOF' namespace: tenant-a quotas: compute: cpu: 4 # 4核CPU memory: 8Gi # 8GB内存 storage: skills: 1Gi # Skills存储 data: 10Gi # 数据持久化 api: requestsPerHour: 10000 tokensPerDay: 1000000 skills: maxInstalled: 50 maxConcurrent: 10 models: maxCallsPerDay: gpt-4o: 1000 claude-3.5-sonnet: 500 EOF # 应用配额配置 openclaw namespace apply-quota tenant-a --from=tenant-a-quota.yaml # 查看配额使用情况 openclaw namespace quota-status tenant-a

2. 计量与计费

# 启用计量功能 openclaw config set metering.enabled true openclaw config set metering.backend "prometheus" openclaw config set metering.retention "90d" # 查看租户用量报告 openclaw metering report \ --namespace=tenant-a \ --period=2026-05 \ --format=json # 输出示例: # { # "namespace": "tenant-a", # "period": "2026-05", # "usage": { # "api_requests": 123456, # "tokens_used": 8901234, # "skills_executed": 5678, # "storage_used": "2.3Gi" # }, # "cost": { # "api_calls": 123.45, # "tokens": 89.01, # "storage": 23.00, # "total": 235.46 # } # } # 导出计费数据到CSV openclaw metering export \ --namespace=tenant-a \ --start=2026-05-01 \ --end=2026-05-31 \ --format=csv > tenant-a-billing.csv

🚀 方案二:容器隔离(高安全场景)

💡 适用场景:金融、医疗等需要强隔离的行业客户,或者你不想让客户A的代码bug影响到客户B。
# 使用Kubernetes部署多租户 cat > openclaw-tenant-a-deployment.yaml << 'EOF' apiVersion: apps/v1 kind: Deployment metadata: name: openclaw-tenant-a namespace: tenant-a spec: replicas: 2 selector: matchLabels: app: openclaw tenant: tenant-a template: metadata: labels: app: openclaw tenant: tenant-a spec: containers: - name: openclaw image: openclaw/gateway:latest env: - name: NAMESPACE value: "tenant-a" - name: TENANT_ID value: "tenant-a" resources: requests: cpu: "2" memory: "4Gi" limits: cpu: "4" memory: "8Gi" volumeMounts: - name: tenant-data mountPath: /var/lib/openclaw volumes: - name: tenant-data persistentVolumeClaim: claimName: tenant-a-pvc --- apiVersion: v1 kind: Service metadata: name: openclaw-tenant-a namespace: tenant-a spec: selector: app: openclaw tenant: tenant-a ports: - port: 8080 targetPort: 8080 EOF # 部署到Kubernetes kubectl apply -f openclaw-tenant-a-deployment.yaml # 查看部署状态 kubectl get pods -n tenant-a

🔐 安全与合规

1. 数据隔离

# 配置租户数据加密 openclaw config set namespaces.tenant-a.encryption.enabled true openclaw config set namespaces.tenant-a.encryption.algorithm "AES-256-GCM" openclaw config set namespaces.tenant-a.encryption.keySource "vault" # 配置数据留存策略 openclaw config set namespaces.tenant-a.dataRetention.conversations "90d" openclaw config set namespaces.tenant-a.dataRetention.logs "30d" openclaw config set namespaces.tenant-a.dataRetention.metrics "180d" # 定期清理过期数据 openclaw namespace cleanup tenant-a --older-than=90d --dry-run=false

2. 审计日志

# 启用租户级审计日志 openclaw config set namespaces.tenant-a.audit.enabled true openclaw config set namespaces.tenant-a.audit.backend "elasticsearch" openclaw config set namespaces.tenant-a.audit.events "all" # 查询审计日志 openclaw audit query \ --namespace=tenant-a \ --start="2026-05-27T00:00:00Z" \ --end="2026-05-28T00:00:00Z" \ --event-type="api_call" # 导出审计日志(合规需求) openclaw audit export \ --namespace=tenant-a \ --period=2026-Q2 \ --format=pdf > tenant-a-audit-q2-2026.pdf

💰 计费系统集成

# 集成Stripe计费 openclaw billing configure \ --provider=stripe \ --api-key="${STRIPE_SECRET_KEY}" \ --webhook-secret="${STRIPE_WEBHOOK_SECRET}" # 创建计费计划 openclaw billing plan create \ --name="basic" \ --price=99.00 \ --currency=USD \ --interval=month \ --quotas=@tenant-b-quota.yaml openclaw billing plan create \ --name="pro" \ --price=299.00 \ --currency=USD \ --interval=month \ --quotas=@tenant-a-quota.yaml # 为租户订阅计划 openclaw billing subscribe tenant-a --plan=pro --payment-method="pm_xxxxx" # 查看账单 openclaw billing invoices tenant-a --limit=12

⚠️ 常见坑与解决方案

坑1:资源泄漏

症状:某个租户跑完后,资源没释放

解决:配置资源回收策略,定期检查

# 启用自动资源回收 openclaw config set namespaces.*.autoCleanup.enabled true openclaw config set namespaces.*.autoCleanup.after="1h"
坑2:冷启动慢

症状:新租户首次请求很慢

解决:预热机制,或者保持最小实例数

# 为VIP租户保持热实例 openclaw config set namespaces.tenant-a.minInstances 1 openclaw config set namespaces.tenant-a.warmupOnCreate true
✅ 多租户部署检查清单:

📚 相关资源

「凌晨3点08分,第17个客户终于满意地走了。我看着监控系统里17个命名空间安安静静地跑着,突然明白——原来多租户部署最大的挑战,不是技术,而是忍受客户半夜提需求。」——妙趣AI