

大家好,我是正在实战各种 AI 项目的程序员晚枫。
😫 开篇:K8s 部署是生产环境的终极方案
"晚枫,我们用户量大,需要高可用,怎么部署?"
这种情况,Kubernetes(K8s)是终极方案。自动扩缩容、故障自愈、负载均衡,全都有。
今天把 K8s 部署 OpenClaw 的完整手册整理出来,照着做,90 分钟搞定生产级高可用架构。
📋 部署前准备
1. K8s 集群要求
| 组件 | 最低配置 | 推荐配置 |
|---|
| Master 节点 | 2 核 4G | 4 核 8G × 3(高可用) |
| Worker 节点 | 2 核 4G | 4 核 8G × N(按需) |
| 网络 | 千兆 | 万兆 |
| 存储 | 50G SSD | 200G SSD × N |
2. K8s 发行版选择
1 2 3 4 5 6 7 8
| 推荐: ✓ K3s(轻量级,适合中小规模) ✓ KubeSphere(易用,带管理界面) ✓ 云厂商托管 K8s(最省心)
备选: ✓ 原生 Kubernetes(功能最全) ✓ Rancher(多集群管理)
|
3. 部署方式选择
1 2 3 4 5 6 7 8 9 10 11 12 13
| 方案 1:云厂商托管 K8s(推荐) - 阿里云 ACK - 腾讯云 TKE - 华为云 CCE - 火山引擎 VKE 优点:免运维,高可用 缺点:成本略高
方案 2:自建 K8s - 用 kubeadm 部署 - 用 K3s 部署 优点:成本低,可控 缺点:需要运维能力
|
🚀 部署步骤(云托管 K8s)
第 1 步:创建 K8s 集群
1 2 3 4 5 6 7 8 9 10 11 12
| 以阿里云 ACK 为例:
1. 登录阿里云控制台 2. 进入"容器服务 Kubernetes" 3. 点击"创建集群" 4. 选择配置: - 集群类型:托管版(免运维) - Worker 节点:2 台 4 核 8G - 网络:VPC 专有网络 - 存储:云盘 5. 确认配置,完成支付 6. 等待集群创建(约 10 分钟)
|
第 2 步:配置 kubectl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
mkdir -p ~/.kube
kubectl cluster-info kubectl get nodes
|
第 3 步:创建 Namespace
1 2 3 4 5
| kubectl create namespace openclaw
kubectl get namespaces | grep openclaw
|
第 4 步:创建 ConfigMap
1 2 3 4 5 6 7 8 9 10 11
| apiVersion: v1 kind: ConfigMap metadata: name: openclaw-config namespace: openclaw data: OPENCLAW_PORT: "8000" OPENCLAW_HOST: "0.0.0.0" DEBUG: "False" REDIS_URL: "redis://openclaw-redis:6379/0"
|
1
| kubectl apply -f configmap.yaml
|
第 5 步:创建 Secret
1 2 3 4 5 6 7 8 9 10
| apiVersion: v1 kind: Secret metadata: name: openclaw-secret namespace: openclaw type: Opaque stringData: DATABASE_URL: "postgresql://openclaw:secure_password@postgres:5432/openclaw" SECRET_KEY: "your-secret-key-here"
|
1
| kubectl apply -f secret.yaml
|
第 6 步:部署 PostgreSQL
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| apiVersion: apps/v1 kind: StatefulSet metadata: name: openclaw-postgres namespace: openclaw spec: serviceName: postgres replicas: 1 selector: matchLabels: app: postgres template: metadata: labels: app: postgres spec: containers: - name: postgres image: postgres:14-alpine ports: - containerPort: 5432 env: - name: POSTGRES_USER value: "openclaw" - name: POSTGRES_PASSWORD value: "secure_password" - name: POSTGRES_DB value: "openclaw" volumeMounts: - name: postgres-storage mountPath: /var/lib/postgresql/data volumeClaimTemplates: - metadata: name: postgres-storage spec: accessModes: ["ReadWriteOnce"] resources: requests: storage: 10Gi
|
1
| kubectl apply -f postgres-statefulset.yaml
|
第 7 步:部署 Redis
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| apiVersion: apps/v1 kind: Deployment metadata: name: openclaw-redis namespace: openclaw spec: replicas: 1 selector: matchLabels: app: redis template: metadata: labels: app: redis spec: containers: - name: redis image: redis:7-alpine ports: - containerPort: 6379 command: ["redis-server", "--appendonly", "yes"] volumeMounts: - name: redis-storage mountPath: /data volumes: - name: redis-storage emptyDir: {}
|
1
| kubectl apply -f redis-deployment.yaml
|
第 8 步:部署 OpenClaw 应用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| apiVersion: apps/v1 kind: Deployment metadata: name: openclaw-app namespace: openclaw spec: replicas: 3 selector: matchLabels: app: openclaw template: metadata: labels: app: openclaw spec: containers: - name: openclaw image: your-registry/openclaw:latest ports: - containerPort: 8000 envFrom: - configMapRef: name: openclaw-config - secretRef: name: openclaw-secret resources: requests: cpu: "500m" memory: "512Mi" limits: cpu: "1000m" memory: "1Gi" livenessProbe: httpGet: path: /health port: 8000 initialDelaySeconds: 30 periodSeconds: 10 readinessProbe: httpGet: path: /ready port: 8000 initialDelaySeconds: 5 periodSeconds: 5
|
1
| kubectl apply -f openclaw-deployment.yaml
|
第 9 步:创建 Service
1 2 3 4 5 6 7 8 9 10 11 12 13
| apiVersion: v1 kind: Service metadata: name: openclaw-service namespace: openclaw spec: selector: app: openclaw ports: - port: 80 targetPort: 8000 type: LoadBalancer
|
1
| kubectl apply -f service.yaml
|
第 10 步:配置 Ingress
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| apiVersion: networking.k8s.io/v1 kind: Ingress metadata: name: openclaw-ingress namespace: openclaw annotations: nginx.ingress.kubernetes.io/ssl-redirect: "true" cert-manager.io/cluster-issuer: "letsencrypt-prod" spec: rules: - host: openclaw.your-domain.com http: paths: - path: / pathType: Prefix backend: service: name: openclaw-service port: number: 80 tls: - hosts: - openclaw.your-domain.com secretName: openclaw-tls
|
1
| kubectl apply -f ingress.yaml
|
🔧 高级功能配置
1. 自动扩缩容(HPA)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| apiVersion: autoscaling/v2 kind: HorizontalPodAutoscaler metadata: name: openclaw-hpa namespace: openclaw spec: scaleTargetRef: apiVersion: apps/v1 kind: Deployment name: openclaw-app minReplicas: 3 maxReplicas: 10 metrics: - type: Resource resource: name: cpu target: type: Utilization averageUtilization: 70
|
1
| kubectl apply -f hpa.yaml
|
2. 健康检查
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| livenessProbe: httpGet: path: /health port: 8000 initialDelaySeconds: 30 periodSeconds: 10 failureThreshold: 3
readinessProbe: httpGet: path: /ready port: 8000 initialDelaySeconds: 5 periodSeconds: 5 failureThreshold: 3
|
3. 持久化存储
1 2 3 4 5 6 7 8 9 10 11 12 13
| apiVersion: v1 kind: PersistentVolumeClaim metadata: name: openclaw-data namespace: openclaw spec: accessModes: - ReadWriteMany resources: requests: storage: 20Gi storageClassName: nas
|
4. 监控告警
1 2 3 4 5 6
| helm install prometheus prometheus-community/kube-prometheus-stack
kubectl port-forward svc/prometheus-grafana 3000:80
|
📊 运维命令速查
查看状态
1 2 3 4 5 6 7 8
| kubectl get all -n openclaw
kubectl get pods -n openclaw
kubectl describe pod openclaw-app-xxx -n openclaw
|
查看日志
1 2 3 4 5
| kubectl logs openclaw-app-xxx -n openclaw
kubectl logs -f openclaw-app-xxx -n openclaw
|
进入容器
1
| kubectl exec -it openclaw-app-xxx -n openclaw -- bash
|
滚动更新
1 2 3 4 5 6 7 8
| kubectl set image deployment/openclaw-app openclaw=your-registry/openclaw:v2 -n openclaw
kubectl rollout status deployment/openclaw-app -n openclaw
kubectl rollout undo deployment/openclaw-app -n openclaw
|
扩缩容
1 2 3 4 5
| kubectl scale deployment openclaw-app --replicas=5 -n openclaw
kubectl get pods -n openclaw
|
💰 成本分析
云托管 K8s 成本
| 项目 | 配置 | 月成本 |
|---|
| K8s 管理费 | 托管版 | 500 元 |
| Worker 节点 | 4 核 8G × 3 | 6000 元 |
| 负载均衡 | SLB | 200 元 |
| 存储 | 100G SSD | 100 元 |
| 合计 | | 6800 元/月 |
自建 K8s 成本
| 项目 | 配置 | 月成本 |
|---|
| 服务器 | 4 核 8G × 3 | 6000 元 |
| 运维人力 | 0.2 人 | 4000 元 |
| 合计 | | 10000 元/月 |
结论:云托管 K8s 更省心,综合成本更低。
🔧 常见问题排查
问题 1:Pod 无法启动
1 2 3 4 5 6 7 8 9 10
| kubectl describe pod openclaw-app-xxx -n openclaw
kubectl logs openclaw-app-xxx -n openclaw
|
问题 2:Service 无法访问
1 2 3 4 5 6 7 8
| kubectl get svc -n openclaw
kubectl get endpoints -n openclaw
kubectl get pods -n openclaw
|
问题 3:Ingress 无法访问
1 2 3 4 5 6 7 8
| kubectl get ingress -n openclaw
kubectl get pods -n ingress-nginx
nslookup openclaw.your-domain.com
|
🚀 更多应用场景
💬 金句总结
K8s 不是银弹,但大规模场景下是必选项。
高可用架构,从 K8s 开始。
📚 相关阅读
🔗 联系方式
主营业务:AI 编程培训、企业内训、技术咨询
K8s 部署,生产级高可用架构!
🎓 AI 编程实战课程
想系统学习 AI 编程?程序员晚枫的 AI 编程实战课 帮你从零上手!