乐于分享
好东西不私藏

在 Azure 容器化部署 OpenClaw:从零到生产环境实战指南

在 Azure 容器化部署 OpenClaw:从零到生产环境实战指南


面向技术爱好者的容器化部署实战教程

一、为什么选择 Azure 容器部署 OpenClaw?

OpenClaw 作为一个开源的 AI Agent 平台,支持多种部署方式。而 Azure 容器服务(Azure Container Apps / AKS)为我们提供了:

  • 弹性伸缩:根据负载自动调整实例数量
  • 高可用性:内置负载均衡和故障转移
  • DevOps 集成:与 GitHub Actions、Azure DevOps 无缝集成
  • 成本优化:按需付费,无需预置资源

二、部署架构设计

┌─────────────────────────────────────────┐
│           Azure Container Apps          │
│  ┌─────────────┐    ┌─────────────┐    │
│  │  OpenClaw   │◄──►│   Redis     │    │
│  │   Agent     │    │   Cache     │    │
│  └─────────────┘    └─────────────┘    │
│         │                               │
│         ▼                               │
│  ┌─────────────┐    ┌─────────────┐    │
│  │  Azure OpenAI│    │ Azure Blob  │    │
│  │   Service   │    │   Storage   │    │
│  └─────────────┘    └─────────────┘    │
└─────────────────────────────────────────┘

三、资源要求详解

3.1 基础配置(开发/测试环境)

组件 CPU 内存 存储 说明
OpenClaw Agent 0.5-1 vCPU 1-2 GiB 10 GB 单实例运行
Redis Cache 0.25 vCPU 0.5 GiB 5 GB 会话存储
总计 0.75-1.25 vCPU 1.5-2.5 GiB 15 GB 月费约 ¥200-400

3.2 生产环境配置

组件 CPU 内存 实例数 说明
OpenClaw Agent 1-2 vCPU 2-4 GiB 2-3 高可用部署
Redis Cache 0.5 vCPU 1 GiB 1 启用持久化
负载均衡 - - 1 内置免费
总计 2.5-4.5 vCPU 5-9 GiB 3-4 月费约 ¥800-1500

3.3 资源选择建议

轻量级场景(个人/小团队):

  • Azure Container Apps(Consumption Plan)
  • 按需付费,无请求时不产生费用

企业级场景(高并发/稳定性要求):

  • Azure Kubernetes Service (AKS)
  • 专用节点池,支持 GPU 加速

四、部署步骤详解

步骤 1:准备 Azure 环境

# 登录 Azure
az login

# 创建资源组
az group create \
  --name openclaw-rg \
  --location eastasia

# 创建 Container Registry
az acr create \
  --resource-group openclaw-rg \
  --name openclawregistry \
  --sku Basic

步骤 2:构建容器镜像

# Dockerfile
FROM node:20-alpine

WORKDIR /app

# 安装依赖
COPY package*.json ./
RUN npm ci --only=production

# 复制应用代码
COPY . .

# 设置环境变量
ENV NODE_ENV=production
ENV OPENCLAW_PORT=3000

EXPOSE 3000

CMD ["node""dist/index.js"]

构建并推送镜像:

# 构建镜像
docker build -t openclawregistry.azurecr.io/openclaw:v1.0 .

# 登录 ACR
az acr login --name openclawregistry

# 推送镜像
docker push openclawregistry.azurecr.io/openclaw:v1.0

步骤 3:部署到 Azure Container Apps

# 创建 Container Apps 环境
az containerapp env create \
  --name openclaw-env \
  --resource-group openclaw-rg \
  --location eastasia

# 部署应用
az containerapp create \
  --name openclaw-agent \
  --resource-group openclaw-rg \
  --environment openclaw-env \
  --image openclawregistry.azurecr.io/openclaw:v1.0 \
  --target-port 3000 \
  --ingress external \
  --min-replicas 1 \
  --max-replicas 3 \
  --cpu 1.0 \
  --memory 2.0Gi \
  --env-vars \
    "OPENAI_API_KEY=your-key" \
    "REDIS_URL=your-redis-url"

步骤 4:配置 Redis 缓存

# 创建 Azure Cache for Redis
az redis create \
  --name openclaw-redis \
  --resource-group openclaw-rg \
  --location eastasia \
  --sku Basic \
  --vm-size c0

步骤 5:配置 CI/CD(GitHub Actions)

# .github/workflows/deploy.yml
name: Deploy to Azure

on:
  push:
    branches: [main]

jobs:
  build-and-deploy:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      
      - name: Login to Azure
        uses: azure/login@v1
        with:
          creds: ${{ secrets.AZURE_CREDENTIALS }}
      
      - name: Build and push image
        run: |
          az acr build \
            --registry openclawregistry \
            --image openclaw:${{ github.sha }} .
      
      - name: Deploy to Container Apps
        run: |
          az containerapp update \
            --name openclaw-agent \
            --resource-group openclaw-rg \
            --image openclawregistry.azurecr.io/openclaw:${{ github.sha }}

五、性能优化技巧

5.1 冷启动优化

# 保持最小副本数,避免冷启动
minReplicas: 1
maxReplicas: 5

# 配置健康检查
healthCheck:
  httpGet:
    path: /health
    port: 3000
  initialDelaySeconds: 10
  periodSeconds: 30

5.2 成本优化

  • 使用 Consumption Plan:无请求时不计费
  • 配置自动缩放:基于 CPU/内存使用率
  • 启用 Spot 实例:利用 Azure 闲置资源,成本降低 90%

5.3 安全加固

# 启用 HTTPS 和自定义域名
ingress:
  external: true
  targetPort: 3000
  transport: auto
  allowInsecure: false

# 配置密钥管理
secrets:
  - name: openai-api-key
    value: <your-key>

六、监控与运维

6.1 启用 Azure Monitor

# 查看应用日志
az containerapp logs show \
  --name openclaw-agent \
  --resource-group openclaw-rg \
  --follow

# 查看指标
az monitor metrics list \
  --resource openclaw-agent \
  --metric "CpuUsage" "MemoryUsage"

6.2 配置告警

在 Azure Portal 中设置:

  • CPU 使用率 > 70% 时告警
  • 内存使用率 > 80% 时告警
  • 应用响应时间 > 2s 时告警

七、常见问题排查

问题 原因 解决方案
容器启动失败 端口冲突 检查 target-port 配置
连接 Redis 失败 网络隔离 配置 VNet 集成
内存不足 OOM 增加 memory 配置
冷启动慢 镜像过大 使用多阶段构建

八、总结

Azure Container Apps 为 OpenClaw 提供了:

简单:无需管理 Kubernetes 集群 ✅ 经济:按需付费,适合中小团队 ✅ 弹性:自动扩缩容,应对流量高峰 ✅ 集成:与 Azure AI 服务无缝连接

从开发环境到生产环境,Azure 容器服务都能满足 OpenClaw 的部署需求。


参考资源:

  • OpenClaw 官方文档:https://docs.openclaw.ai[1]
  • Azure Container Apps:https://azure.microsoft.com/services/container-apps[2]
  • 本文示例代码:https://github.com/openclaw/azure-deployment[3]

作者:小易 | 广州迅易科技有限公司 技术交流:关注公众号获取更多 AI Agent 部署实践

引用链接

[1]https://docs.openclaw.ai

[2]https://azure.microsoft.com/services/container-apps

[3]https://github.com/openclaw/azure-deployment