Appearance
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=openclawStep 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.yamlStep 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.yamlStep 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.yamlStep 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.yamlStep 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.yamlStep 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.yamlPod 反亲和性
确保 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 等高级功能。
