Kubernetes 部署 OpenClaw 完整手册!高可用架构就靠它
Kubernetes 部署 OpenClaw 完整手册!高可用架构就靠它

大家好,我是正在实战各种 AI 项目的程序员晚枫。

😫 开篇:K8s 部署是生产环境的终极方案

"晚枫,我们用户量大,需要高可用,怎么部署?"

这种情况,Kubernetes(K8s)是终极方案。自动扩缩容、故障自愈、负载均衡,全都有

今天把 K8s 部署 OpenClaw 的完整手册整理出来,照着做,90 分钟搞定生产级高可用架构。

📋 部署前准备

1. K8s 集群要求

组件最低配置推荐配置
Master 节点2 核 4G4 核 8G × 3(高可用)
Worker 节点2 核 4G4 核 8G × N(按需)
网络千兆万兆
存储50G SSD200G 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
# 下载集群配置
# 阿里云控制台 → ACK → 集群信息 → 连接信息

# 配置 kubectl
mkdir -p ~/.kube
# 下载 kubeconfig 文件到 ~/.kube/config

# 验证连接
kubectl cluster-info
kubectl get nodes

# 应该看到:
# NAME STATUS ROLES AGE VERSION
# node-1 Ready <none> 10m v1.26.0
# node-2 Ready <none> 10m v1.26.0

第 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
# configmap.yaml
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
# secret.yaml
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
# postgres-statefulset.yaml
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
# redis-deployment.yaml
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
# openclaw-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: openclaw-app
namespace: openclaw
spec:
replicas: 3 # 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
# service.yaml
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
# ingress.yaml
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
# hpa.yaml
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
# 在 Deployment 中添加
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
# 创建 PVC
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
# 安装 Prometheus
helm install prometheus prometheus-community/kube-prometheus-stack

# 访问 Grafana
kubectl port-forward svc/prometheus-grafana 3000:80
# 浏览器访问 http://localhost:3000

📊 运维命令速查

查看状态

1
2
3
4
5
6
7
8
# 查看所有资源
kubectl get all -n openclaw

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

# 查看 Pod 详情
kubectl describe pod openclaw-app-xxx -n openclaw

查看日志

1
2
3
4
5
# 查看 Pod 日志
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 × 36000 元
负载均衡SLB200 元
存储100G SSD100 元
合计6800 元/月

自建 K8s 成本

项目配置月成本
服务器4 核 8G × 36000 元
运维人力0.2 人4000 元
合计10000 元/月

结论:云托管 K8s 更省心,综合成本更低。

🔧 常见问题排查

问题 1:Pod 无法启动

1
2
3
4
5
6
7
8
9
10
# 查看 Pod 状态
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
# 查看 Service
kubectl get svc -n openclaw

# 查看 Endpoints
kubectl get endpoints -n openclaw

# 检查 Pod 是否就绪
kubectl get pods -n openclaw

问题 3:Ingress 无法访问

1
2
3
4
5
6
7
8
# 查看 Ingress
kubectl get ingress -n openclaw

# 查看 Ingress Controller
kubectl get pods -n ingress-nginx

# 检查 DNS 解析
nslookup openclaw.your-domain.com

🚀 更多应用场景

  • 多环境部署
  • 蓝绿部署
  • 金丝雀发布
  • 多集群管理

💬 金句总结

K8s 不是银弹,但大规模场景下是必选项。

高可用架构,从 K8s 开始。

📚 相关阅读

🔗 联系方式

平台账号/链接
微信扫码加好友
微博@程序员晚枫
知乎@程序员晚枫
抖音@程序员晚枫
小红书@程序员晚枫
B 站Python 自动化办公社区

主营业务:AI 编程培训、企业内训、技术咨询


K8s 部署,生产级高可用架构!

🎓 AI 编程实战课程

想系统学习 AI 编程?程序员晚枫的 AI 编程实战课 帮你从零上手!