Kubernetes 集群部署

企业级部署 —— 自动扩缩容、高可用、滚动更新

什么时候该上 K8s

先说实话:大多数人用不着 Kubernetes。单机 Docker Compose 就能扛住不少流量了。但如果你的情况是这样的 —— 团队好几个人同时用、需要自动扩缩容、要求零停机更新 —— 那 K8s 确实是正解。

这篇手把手带你在 K8s 上部署 OpenClaw,从集群准备到 HPA 自动扩缩容,一步步来。

五步搞定 K8s 部署

1

集群准备

先把 K8s 集群搭好。可以用云厂商的托管 K8s(阿里云 ACK、腾讯 TKE、AWS EKS),也可以用 kubeadm 自建。建议至少 3 个节点,1 master + 2 worker。

2

创建 Deployment

写一个 Deployment YAML,定义 OpenClaw 的 Pod 模板、副本数、资源限制。副本数建议至少 2 个,保证一个挂了另一个能顶上。

3

创建 Service

Service 负责把流量分发到各个 Pod 上。用 ClusterIP 类型给集群内部用,或者 NodePort 直接对外暴露端口。

4

配置 Ingress

Ingress 是集群的入口网关,负责域名路由和 TLS 卸载。装一个 Nginx Ingress Controller,然后配 Ingress 规则就行。

5

开启 HPA 自动扩缩容

HPA(Horizontal Pod Autoscaler)根据 CPU 或内存使用率自动增减 Pod 数量。高峰期自动扩容,低谷期自动缩容,省钱又省心。

Deployment 配置

这是核心配置文件,定义了 OpenClaw 怎么跑:

deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: openclaw
  namespace: openclaw
  labels:
    app: openclaw
spec:
  replicas: 2
  selector:
    matchLabels:
      app: openclaw
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0
  template:
    metadata:
      labels:
        app: openclaw
    spec:
      containers:
      - name: openclaw
        image: openclaw/openclaw:latest
        ports:
        - containerPort: 3000
        resources:
          requests:
            cpu: "500m"
            memory: "512Mi"
          limits:
            cpu: "2000m"
            memory: "2Gi"
        env:
        - name: OPENCLAW_API_KEY
          valueFrom:
            secretKeyRef:
              name: openclaw-secrets
              key: api-key
        livenessProbe:
          httpGet:
            path: /health
            port: 3000
          initialDelaySeconds: 30
          periodSeconds: 10
        readinessProbe:
          httpGet:
            path: /health
            port: 3000
          initialDelaySeconds: 5
          periodSeconds: 5
        volumeMounts:
        - name: data
          mountPath: /app/data
      volumes:
      - name: data
        persistentVolumeClaim:
          claimName: openclaw-data

Service 配置

service.yaml
apiVersion: v1
kind: Service
metadata:
  name: openclaw
  namespace: openclaw
spec:
  selector:
    app: openclaw
  ports:
  - protocol: TCP
    port: 80
    targetPort: 3000
  type: ClusterIP

---
# Ingress 配置
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: openclaw
  namespace: openclaw
  annotations:
    nginx.ingress.kubernetes.io/proxy-body-size: "50m"
    cert-manager.io/cluster-issuer: "letsencrypt-prod"
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: 80

部署命令

YAML 写好了,几条命令就能部署上去:

kubectl 部署命令
# 创建命名空间
kubectl create namespace openclaw

# 创建 Secret(存 API Key)
kubectl create secret generic openclaw-secrets \
  --from-literal=api-key=your-api-key-here \
  -n openclaw

# 部署
kubectl apply -f deployment.yaml -n openclaw
kubectl apply -f service.yaml -n openclaw

# 查看 Pod 状态
kubectl get pods -n openclaw -w

# 查看日志
kubectl logs -f deployment/openclaw -n openclaw

# 扩缩容
kubectl scale deployment openclaw --replicas=3 -n openclaw

# 配置 HPA 自动扩缩容
kubectl autoscale deployment openclaw \
  --cpu-percent=70 --min=2 --max=10 -n openclaw
💡 别为了炫技上 K8s。个人用或者小团队(5 人以内),Docker Compose 完全够用,运维成本低得多。K8s 的学习曲线很陡,集群本身也需要维护。等你真的遇到单机扛不住、需要弹性伸缩的时候再考虑。

滚动更新与回滚

K8s 的滚动更新是自带的,但你得知道怎么回滚:

更新与回滚
# 更新镜像版本
kubectl set image deployment/openclaw \
  openclaw=openclaw/openclaw:v2.0 -n openclaw

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

# 更新有问题?一键回滚
kubectl rollout undo deployment/openclaw -n openclaw

# 查看历史版本
kubectl rollout history deployment/openclaw -n openclaw
这篇教程对你有帮助吗?