Skip to content

小龙虾 OpenClaw Kubernetes 部署完全指南:高可用集群方案(2026 版)

2026年4月8日

OpenClaw Kubernetes (K8s) 部署实践教程:企业级容器编排指南

Docker 适合单机部署,Kubernetes 则适合大规模、高可用的企业级场景。这篇教程教你如何在 K8s 集群中部署 OpenClaw,实现自动扩缩容、滚动更新、故障自愈。

为什么用 K8s 部署?

对比项Docker 单机K8s 集群
高可用❌ 单点故障✅ 多副本
自动扩缩容❌ 手动✅ 自动
滚动更新❌ 停机更新✅ 零停机
故障自愈❌ 手动重启✅ 自动恢复
负载均衡⚠️ 手动配置✅ 内置
适用场景个人/测试企业生产

K8s 部署适合你,如果:

  • 需要高可用(99.9%+)
  • 流量波动大,需要自动扩缩容
  • 多环境管理(开发/测试/生产)
  • 有 K8s 运维团队

继续用 Docker,如果:

  • 个人项目或小团队
  • 没有专职运维
  • 流量稳定,不需要扩缩容

前置条件

需要的资源

  • Kubernetes 集群(v1.20+)
  • kubectl 命令行工具
  • 足够的集群资源(建议:2 CPU + 4 GB 内存)
  • 持久化存储(PV)

验证环境

bash
# 检查 kubectl 连接
kubectl cluster-info

# 检查节点状态
kubectl get nodes

# 检查存储类
kubectl get storageclass

部署方案概览

┌─────────────────────────────────────────────────────────────┐
│                    Kubernetes Cluster                        │
│  ┌──────────────┐  ┌──────────────┐  ┌──────────────┐     │
│  │   Pod 1      │  │   Pod 2      │  │   Pod 3      │     │
│  │  (OpenClaw)  │  │  (OpenClaw)  │  │  (OpenClaw)  │     │
│  └──────────────┘  └──────────────┘  └──────────────┘     │
│         │                 │                 │               │
│         └────────────────┬┴─────────────────┘               │
│                          ▼                                  │
│                   ┌──────────────┐                         │
│                   │   Service    │                         │
│                   │ (LoadBalance)│                         │
│                   └──────────────┘                         │
│                          │                                  │
│                          ▼                                  │
│                   ┌──────────────┐                         │
│                   │   Ingress    │                         │
│                   └──────────────┘                         │
└─────────────────────────────────────────────────────────────┘

Step 1:创建命名空间

bash
# 创建命名空间
kubectl create namespace openclaw

# 切换到该命名空间
kubectl config set-context --current --namespace=openclaw

Step 2:创建 Secret

存储敏感信息(API Key 等):

yaml
# secret.yaml
apiVersion: v1
kind: Secret
metadata:
  name: openclaw-secrets
  namespace: openclaw
type: Opaque
stringData:
  OPENAI_API_KEY: "sk-xxxxx"
  OPENCLAW_TOKEN: "your-secure-token"

应用:

bash
kubectl apply -f secret.yaml

Step 3:创建 ConfigMap

存储配置文件:

yaml
# configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: openclaw-config
  namespace: openclaw
data:
  OPENCLAW_VERBOSE: "0"
  TZ: "Asia/Shanghai"
  agents.defaults.model.primary: "openai/gpt-4o"

应用:

bash
kubectl apply -f configmap.yaml

Step 4:创建持久化存储

PVC(持久卷声明)

yaml
# pvc.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: openclaw-data
  namespace: openclaw
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 10Gi
  storageClassName: standard  # 根据集群配置调整

应用:

bash
kubectl apply -f pvc.yaml

Step 5:创建 Deployment

yaml
# deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: openclaw
  namespace: openclaw
  labels:
    app: openclaw
spec:
  replicas: 3
  selector:
    matchLabels:
      app: openclaw
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0
  template:
    metadata:
      labels:
        app: openclaw
    spec:
      containers:
      - name: openclaw
        image: openclaiai/openclaw:latest
        ports:
        - containerPort: 1878
        - containerPort: 18789
        envFrom:
        - configMapRef:
            name: openclaw-config
        - secretRef:
            name: openclaw-secrets
        volumeMounts:
        - name: data
          mountPath: /root/.openclaw
        resources:
          requests:
            cpu: "500m"
            memory: "1Gi"
          limits:
            cpu: "2000m"
            memory: "4Gi"
        livenessProbe:
          httpGet:
            path: /health
            port: 1878
          initialDelaySeconds: 30
          periodSeconds: 10
        readinessProbe:
          httpGet:
            path: /health
            port: 1878
          initialDelaySeconds: 10
          periodSeconds: 5
      volumes:
      - name: data
        persistentVolumeClaim:
          claimName: openclaw-data

应用:

bash
kubectl apply -f deployment.yaml

Step 6:创建 Service

yaml
# service.yaml
apiVersion: v1
kind: Service
metadata:
  name: openclaw
  namespace: openclaw
spec:
  type: LoadBalancer
  selector:
    app: openclaw
  ports:
  - name: http
    port: 1878
    targetPort: 1878
  - name: internal
    port: 18789
    targetPort: 18789

应用:

bash
kubectl apply -f service.yaml

Step 7:创建 Ingress(可选)

如果需要域名访问:

yaml
# ingress.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: openclaw-ingress
  namespace: openclaw
  annotations:
    nginx.ingress.kubernetes.io/ssl-redirect: "true"
spec:
  ingressClassName: nginx
  tls:
  - hosts:
    - openclaw.example.com
    secretName: openclaw-tls
  rules:
  - host: openclaw.example.com
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: openclaw
            port:
              number: 1878

应用:

bash
kubectl apply -f ingress.yaml

高级配置

自动扩缩容(HPA)

yaml
# hpa.yaml
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: openclaw-hpa
  namespace: openclaw
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: openclaw
  minReplicas: 3
  maxReplicas: 10
  metrics:
  - type: Resource
    resource:
      name: cpu
      target:
        type: Utilization
        averageUtilization: 70
  - type: Resource
    resource:
      name: memory
      target:
        type: Utilization
        averageUtilization: 80

应用:

bash
kubectl apply -f hpa.yaml

Pod 反亲和性

确保 Pod 分布在不同节点:

yaml
spec:
  affinity:
    podAntiAffinity:
      preferredDuringSchedulingIgnoredDuringExecution:
      - weight: 100
        podAffinityTerm:
          labelSelector:
            matchLabels:
              app: openclaw
          topologyKey: kubernetes.io/hostname

资源限制

yaml
resources:
  requests:
    cpu: "500m"
    memory: "1Gi"
  limits:
    cpu: "2000m"
    memory: "4Gi"

验证部署

检查 Pod 状态

bash
kubectl get pods -n openclaw

# 期望输出
NAME                        READY   STATUS    RESTARTS   AGE
openclaw-xxx-yyy            1/1     Running   0          1m
openclaw-xxx-zzz            1/1     Running   0          1m
openclaw-xxx-www            1/1     Running   0          1m

检查服务

bash
kubectl get svc -n openclaw

# 获取外部 IP
kubectl get svc openclaw -n openclaw

访问测试

bash
# 端口转发(本地测试)
kubectl port-forward svc/openclaw 1878:1878 -n openclaw

# 访问
curl http://localhost:1878/health

常用运维命令

查看日志

bash
# 查看某个 Pod 日志
kubectl logs -f openclaw-xxx-yyy -n openclaw

# 查看所有 Pod 日志
kubectl logs -f -l app=openclaw -n openclaw

进入容器

bash
kubectl exec -it openclaw-xxx-yyy -n openclaw -- /bin/bash

扩缩容

bash
# 手动扩容
kubectl scale deployment openclaw --replicas=5 -n openclaw

# 手动缩容
kubectl scale deployment openclaw --replicas=2 -n openclaw

滚动更新

bash
# 更新镜像
kubectl set image deployment/openclaw openclaw=openclaiai/openclaw:v2026.4.8 -n openclaw

# 查看更新状态
kubectl rollout status deployment/openclaw -n openclaw

# 回滚
kubectl rollout undo deployment/openclaw -n openclaw

常见问题

问题一:Pod 一直 Pending

原因: 资源不足或 PVC 未绑定

解决:

bash
# 检查事件
kubectl describe pod openclaw-xxx-yyy -n openclaw

# 检查 PVC
kubectl get pvc -n openclaw

问题二:健康检查失败

原因: 容器启动慢或探针配置不当

解决:

yaml
livenessProbe:
  initialDelaySeconds: 60  # 增加初始延迟

问题三:存储冲突

原因: 多副本写同一个 PVC

解决:

  • 使用 ReadWriteMany 存储类
  • 或使用 StatefulSet 替代 Deployment

一键部署脚本

将所有配置整合:

bash
#!/bin/bash
# deploy-openclaw-k8s.sh

# 创建命名空间
kubectl create namespace openclaw

# 依次应用配置
kubectl apply -f secret.yaml
kubectl apply -f configmap.yaml
kubectl apply -f pvc.yaml
kubectl apply -f deployment.yaml
kubectl apply -f service.yaml
kubectl apply -f ingress.yaml
kubectl apply -f hpa.yaml

# 等待就绪
kubectl rollout status deployment/openclaw -n openclaw

echo "OpenClaw 部署完成!"
kubectl get all -n openclaw

总结

组件作用
Deployment管理 Pod 副本
Service提供稳定访问入口
ConfigMap存储配置
Secret存储密钥
PVC持久化数据
Ingress域名访问
HPA自动扩缩容

K8s vs Docker:

场景推荐
个人项目Docker
测试环境Docker
企业生产K8s
高可用需求K8s
自动扩缩容K8s

K8s 部署让 OpenClaw 具备企业级能力:高可用、自动扩缩容、滚动更新、故障自愈。但配置复杂度也更高,适合有 K8s 运维经验的团队。先从简单的 Deployment + Service 开始,逐步添加 HPA、Ingress 等高级功能。

不要孤军奋战啦!

加入微信群一起学习交流 AI

与大神一起使用 OpenClaw、Hermes、Claude Code、Seedance 2.0、GPT-Image-2 等

微信公众号

扫码关注微信公众号
私信 "加群",将自动获取微信群二维码

探索 AI 世界,掌握智能未来