Kubernetes YAML 配置复杂、排错费时。 Claude Code 能生成生产级 K8s 配置、 分析 kubectl 报错,让 K8s 部署不再痛苦。
Claude Code 在 K8s 运维中的价值
擅长处理的 K8s 场景:
生成 Deployment/Service/Ingress/ConfigMap YAML
设计 HPA(水平自动扩缩容)策略
分析 Pod 报错(CrashLoopBackOff/OOMKilled 等)
设计 Helm Chart 结构
多环境(dev/staging/prod)配置管理
安全加固(RBAC/NetworkPolicy/PodSecurityContext)
场景一:从零生成完整部署配置
Prompt:
帮我写一个 Node.js API 服务的完整 K8s 部署配置:
- 3 副本,滚动更新(最多 1 个不可用)
- 环境变量从 ConfigMap 和 Secret 注入
- 健康检查(liveness/readiness,HTTP /health)
- 资源限制(limit: 500m CPU / 512Mi 内存)
- HPA:CPU > 70% 时自动扩容,最多 10 副本
- Service(ClusterIP)+ Ingress(带 TLS)
- 命名空间:production
Claude Code 生成的 Deployment:
yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: api-server
namespace: production
labels:
app: api-server
spec:
replicas: 3
strategy:
type: RollingUpdate
rollingUpdate:
maxUnavailable: 1
maxSurge: 1
selector:
matchLabels:
app: api-server
template:
metadata:
labels:
app: api-server
spec:
containers:
- name: api-server
image: your-registry/api-server:latest
ports:
- containerPort: 3000
envFrom:
- configMapRef:
name: api-server-config
- secretRef:
name: api-server-secrets
resources:
requests:
cpu: "200m"
memory: "256Mi"
limits:
cpu: "500m"
memory: "512Mi"
livenessProbe:
httpGet:
path: /health
port: 3000
initialDelaySeconds: 30
periodSeconds: 10
failureThreshold: 3
readinessProbe:
httpGet:
path: /health/ready
port: 3000
initialDelaySeconds: 10
periodSeconds: 5HPA 配置:
yaml
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: api-server-hpa
namespace: production
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: api-server
minReplicas: 3
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 70场景二:分析 K8s 报错
Prompt 模板(将报错粘贴给 Claude Code):
kubectl describe pod api-server-xxx-yyy 输出如下,
帮我分析 CrashLoopBackOff 的根本原因:
[粘贴 describe 输出]
kubectl logs api-server-xxx-yyy 输出:
[粘贴日志]
常见问题 Claude Code 能直接诊断:
CrashLoopBackOff:容器启动失败,分析日志找原因OOMKilled:内存超限,建议调整 memory limitImagePullBackOff:镜像拉取失败,检查仓库权限Pending:资源不足或 NodeSelector 不匹配
场景三:多环境配置管理
Prompt:
帮我设计一个 K8s 多环境配置方案(dev/staging/prod),
要求:
- 镜像 tag 不同(dev: latest, prod: 具体版本号)
- 副本数不同(dev: 1, prod: 3)
- 环境变量不同(数据库地址等)
- 不要复制粘贴三份相同的 YAML
推荐使用 Kustomize 或 Helm,给出完整目录结构
Claude Code 生成的 Kustomize 结构:
k8s/
base/
deployment.yaml
service.yaml
kustomization.yaml
overlays/
dev/
kustomization.yaml # replicas=1, image=latest
staging/
kustomization.yaml
prod/
kustomization.yaml # replicas=3, image=v1.2.3
场景四:RBAC 安全配置
Prompt:
帮我配置 K8s RBAC:
- 开发团队:只能读取 dev 命名空间的 Pod/Logs
- 运维团队:可以操作 staging/prod 的 Deployment(但不能删除)
- CI/CD 服务账号:只能更新 Deployment 的镜像
场景五:GitHub Actions CI/CD
Prompt:
写一个 GitHub Actions 工作流:
- 构建 Docker 镜像,推送到 GitHub Container Registry
- 用 kubectl 滚动更新 K8s Deployment
- 部署后等待 rollout 成功,失败则自动回滚
- 只在 main 分支 push 时触发
高价值排错 Prompt
# Pod 一直 Pending
kubectl get events -n production 输出如下:
[粘贴输出]
为什么 Pod 一直处于 Pending 状态?
# 服务无法访问
Service 配置如下:[粘贴]
Pod 正在运行,但 curl Service ClusterIP 超时,
帮我排查网络问题。
来源:Anthropic Claude Code 官方文档 - docs.anthropic.com/en/docs/claude-code