Google Cloud 部署
在 GCP 上部署 OpenClaw
概述
Google Cloud Platform (GCP) 提供多种部署 OpenClaw 的方式。 根据你的需求,可以选择无服务器的 Cloud Run 或传统的 Compute Engine。
全球网络
遍布全球的数据中心
弹性伸缩
自动扩缩容
企业安全
企业级安全保障
部署选项
| 选项 | 优点 | 缺点 | 适用场景 |
|---|---|---|---|
| Cloud Run | 无服务器,自动扩缩容 | 冷启动延迟 | 轻量使用 |
| Compute Engine | 完全控制,持续运行 | 需要管理 | 持续运行 |
| GKE | Kubernetes 生态 | 复杂度高 | 大规模部署 |
Cloud Run 部署
前提条件
# 安装 gcloud CLI
brew install google-cloud-sdk
# 登录
gcloud auth login
# 设置项目
gcloud config set project YOUR_PROJECT_ID创建 Dockerfile
FROM node:20-slim
WORKDIR /app
# 安装 OpenClaw
RUN npm install -g openclaw
# 复制配置
COPY config.yaml /root/.openclaw/config.yaml
# 暴露端口
EXPOSE 8080
# 启动命令
CMD ["openclaw", "gateway", "--port", "8080"]部署到 Cloud Run
# 构建并推送镜像
gcloud builds submit --tag gcr.io/YOUR_PROJECT/openclaw
# 部署到 Cloud Run
gcloud run deploy openclaw \
--image gcr.io/YOUR_PROJECT/openclaw \
--platform managed \
--region asia-east1 \
--allow-unauthenticated \
--set-env-vars ANTHROPIC_API_KEY=sk-... \
--min-instances 1 \
--max-instances 3配置持续运行
# 设置最小实例为 1,避免冷启动
gcloud run services update openclaw \
--min-instances 1Compute Engine 部署
创建 VM 实例
# 创建实例
gcloud compute instances create openclaw \
--zone=asia-east1-b \
--machine-type=e2-small \
--image-family=ubuntu-2204-lts \
--image-project=ubuntu-os-cloud \
--boot-disk-size=20GB连接并安装
# SSH 连接
gcloud compute ssh openclaw --zone=asia-east1-b
# 安装 Node.js
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
sudo apt install -y nodejs
# 安装 OpenClaw
sudo npm install -g openclaw
# 初始化
openclaw init配置系统服务
sudo cat > /etc/systemd/system/openclaw.service << 'EOF'
[Unit]
Description=OpenClaw Gateway
After=network.target
[Service]
Type=simple
User=root
ExecStart=/usr/bin/openclaw gateway
Restart=always
RestartSec=10
[Install]
WantedBy=multi-user.target
EOF
sudo systemctl daemon-reload
sudo systemctl enable openclaw
sudo systemctl start openclaw配置管理
使用 Secret Manager
# 创建 Secret
gcloud secrets create openclaw-api-key \
--data-file=- <<< "sk-your-api-key"
# 授权 Cloud Run 访问
gcloud secrets add-iam-policy-binding openclaw-api-key \
--member="serviceAccount:YOUR_SA@YOUR_PROJECT.iam.gserviceaccount.com" \
--role="roles/secretmanager.secretAccessor"
# 在 Cloud Run 中使用
gcloud run services update openclaw \
--set-secrets ANTHROPIC_API_KEY=openclaw-api-key:latest配置存储
# 创建 Cloud Storage 存储桶
gcloud storage buckets create gs://openclaw-data
# 挂载到 Cloud Run(使用 Cloud Storage FUSE)
gcloud run services update openclaw \
--add-volume name=data,type=cloud-storage,bucket=openclaw-data \
--add-volume-mount volume=data,mount-path=/data监控日志
查看日志
# Cloud Run 日志
gcloud logging read "resource.type=cloud_run_revision AND resource.labels.service_name=openclaw" --limit 50
# Compute Engine 日志
gcloud compute ssh openclaw --command "journalctl -u openclaw -f"设置告警
# 创建告警策略
gcloud alpha monitoring policies create \
--notification-channels=YOUR_CHANNEL \
--display-name="OpenClaw High Error Rate" \
--condition-display-name="Error rate > 1%" \
--condition-filter='resource.type="cloud_run_revision" AND metric.type="run.googleapis.com/request_count"'Cloud Monitoring 仪表板
在 GCP Console 中:
- 进入 Monitoring
- 创建自定义仪表板
- 添加 CPU、内存、请求数等图表