Get the FREE Ultimate OpenClaw Setup Guide →
npx machina-cli add skill chaterm/terminal-skills/deployment --openclaw
Files (1)
SKILL.md
6.3 KB

Deployment 管理

概述

Deployment 滚动更新、回滚、扩缩容等技能。

基础操作

查看 Deployment

# 列出 Deployment
kubectl get deployments
kubectl get deploy -o wide
kubectl get deploy -n namespace

# 详细信息
kubectl describe deploy deployment-name
kubectl get deploy deployment-name -o yaml

创建 Deployment

# deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.20
        ports:
        - containerPort: 80
        resources:
          requests:
            memory: "64Mi"
            cpu: "100m"
          limits:
            memory: "128Mi"
            cpu: "200m"
kubectl apply -f deployment.yaml
kubectl create deployment nginx --image=nginx:1.20 --replicas=3

删除 Deployment

kubectl delete deploy deployment-name
kubectl delete -f deployment.yaml

扩缩容

# 手动扩缩容
kubectl scale deploy deployment-name --replicas=5

# 自动扩缩容 (HPA)
kubectl autoscale deploy deployment-name --min=2 --max=10 --cpu-percent=80

# 查看 HPA
kubectl get hpa
kubectl describe hpa deployment-name

HPA 配置

apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: nginx-hpa
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: nginx-deployment
  minReplicas: 2
  maxReplicas: 10
  metrics:
  - type: Resource
    resource:
      name: cpu
      target:
        type: Utilization
        averageUtilization: 80
  - type: Resource
    resource:
      name: memory
      target:
        type: Utilization
        averageUtilization: 80

滚动更新

更新策略配置

spec:
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 25%           # 最多超出期望副本数
      maxUnavailable: 25%     # 最多不可用副本数

执行更新

# 更新镜像
kubectl set image deploy/deployment-name container-name=nginx:1.21

# 更新环境变量
kubectl set env deploy/deployment-name ENV_VAR=value

# 更新资源限制
kubectl set resources deploy/deployment-name -c container-name --limits=cpu=200m,memory=256Mi

# 应用配置文件更新
kubectl apply -f deployment.yaml

# 记录更新原因
kubectl set image deploy/deployment-name container-name=nginx:1.21 --record

查看更新状态

# 查看滚动更新状态
kubectl rollout status deploy/deployment-name

# 查看更新历史
kubectl rollout history deploy/deployment-name
kubectl rollout history deploy/deployment-name --revision=2

# 暂停/恢复更新
kubectl rollout pause deploy/deployment-name
kubectl rollout resume deploy/deployment-name

回滚

# 回滚到上一版本
kubectl rollout undo deploy/deployment-name

# 回滚到指定版本
kubectl rollout undo deploy/deployment-name --to-revision=2

# 查看回滚状态
kubectl rollout status deploy/deployment-name

高级配置

健康检查

spec:
  template:
    spec:
      containers:
      - name: app
        livenessProbe:
          httpGet:
            path: /healthz
            port: 8080
          initialDelaySeconds: 15
          periodSeconds: 10
          failureThreshold: 3
        readinessProbe:
          httpGet:
            path: /ready
            port: 8080
          initialDelaySeconds: 5
          periodSeconds: 5
        startupProbe:
          httpGet:
            path: /startup
            port: 8080
          failureThreshold: 30
          periodSeconds: 10

亲和性配置

spec:
  template:
    spec:
      affinity:
        # Pod 反亲和(分散部署)
        podAntiAffinity:
          preferredDuringSchedulingIgnoredDuringExecution:
          - weight: 100
            podAffinityTerm:
              labelSelector:
                matchLabels:
                  app: nginx
              topologyKey: kubernetes.io/hostname
        # 节点亲和
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
            - matchExpressions:
              - key: node-type
                operator: In
                values:
                - worker

容忍度

spec:
  template:
    spec:
      tolerations:
      - key: "node-role.kubernetes.io/master"
        operator: "Exists"
        effect: "NoSchedule"

常见场景

场景 1:蓝绿部署

# 创建新版本 Deployment
kubectl apply -f deployment-v2.yaml

# 切换 Service 到新版本
kubectl patch service my-service -p '{"spec":{"selector":{"version":"v2"}}}'

# 验证后删除旧版本
kubectl delete deploy deployment-v1

场景 2:金丝雀发布

# 创建金丝雀 Deployment(少量副本)
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-canary
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
      track: canary
  template:
    metadata:
      labels:
        app: nginx
        track: canary
    spec:
      containers:
      - name: nginx
        image: nginx:1.21

场景 3:批量重启 Pod

# 触发滚动重启
kubectl rollout restart deploy/deployment-name

# 或添加注解触发更新
kubectl patch deploy deployment-name -p '{"spec":{"template":{"metadata":{"annotations":{"date":"'$(date +%s)'"}}}}}'

场景 4:查看 Pod 分布

# 查看 Pod 所在节点
kubectl get pods -l app=nginx -o wide

# 按节点统计
kubectl get pods -l app=nginx -o jsonpath='{range .items[*]}{.spec.nodeName}{"\n"}{end}' | sort | uniq -c

故障排查

问题排查方法
更新卡住kubectl rollout status, 检查 Pod 状态
Pod 无法调度kubectl describe pod, 检查资源和亲和性
更新后服务异常检查健康检查配置、回滚
HPA 不生效检查 metrics-server、资源配置
# 查看 Deployment 事件
kubectl describe deploy deployment-name | grep -A 20 Events

# 查看 ReplicaSet
kubectl get rs -l app=nginx
kubectl describe rs rs-name

Source

git clone https://github.com/chaterm/terminal-skills/blob/main/kubernetes/deployment/SKILL.mdView on GitHub

Overview

Deployment 是 Kubernetes 提供的一种控制器,用于对应用的副本集进行版本化管理、滚动更新与回滚。通过合理配置资源、健康检查、扩缩容策略和场景化部署(蓝绿、金丝雀等),你可以实现零 다운타임 的发布、稳定的资源利用,以及快速的故障回滚。

How This Skill Works

Kubernetes 的 Deployment 控制器持续维护一个或多个 ReplicaSet,并确保实际运行的 Pod 数量与 Deployment 的 spec 一致。你通过修改 Deployment 的模板(镜像、环境变量、资源限制等)来触发滚动更新,Kubernetes 会按 rollingUpdate 策略逐步替换旧版本 Pod。若结合 HPA(HorizontalPodAutoscaler)并配置指标,系统会根据 CPU、内存等目标利用率自动调整副本数。为确保健康性,Deployment 还依赖就绪探针(readinessProbe)和活性探针(livenessProbe)等机制来决定流量路由和重启策略。

When to Use It

  • 需要无 downtime 的应用升级(滚动更新)
  • 需要按需扩缩容来应对流量波动
  • 需要快速回滚到稳定版本以应对新版本问题
  • 实现蓝绿发布或金丝雀发布以平滑变更
  • 希望基于资源利用率自动调整副本数(HPA)

Quick Start

  1. 1) 定义 deployment.yaml,设置 replicas、镜像、标签和模板。
  2. 2) kubectl apply -f deployment.yaml 部署应用。
  3. 3) 监控滚动更新状态:kubectl rollout status deploy/your-deployment。
  4. 4) 如需自动扩缩容,创建 HorizontalPodAutoscaler(HPA)并确保 metrics-server 可用。

Best Practices

  • 为 Deployment 指定明确的资源请求与上限(requests/limits)以便调度器稳定分配资源。
  • 对镜像使用固定标签(避免 latest),推荐使用版本化标签或镜像摘要以实现可回滚性。
  • 启用 readinessProbe 与 livenessProbe,确保 Pod 就绪才接收流量并在异常时自动重启。
  • 配置滚动更新策略中的 maxSurge 与 maxUnavailable,确保升级具有可控性与高可用性。
  • 在关键更改时记录更新原因(kubectl set image --record),并通过 rollback/history 跟踪历史版本。

Example Use Cases

  • 场景:蓝绿部署。创建新版本 Deployment,切换 Service 的 selector 指向新版本,验证后删除旧版本。
  • 场景:金丝雀发布。先部署少量副本的 Canary Deployment,与主 Deployment 共存,逐步增加 Canary 比例,降低风险。
  • 场景:滚动更新镜像版本。更新镜像为 nginx:1.21,并通过 kubectl rollout status 观察进度与健康状况。
  • 场景:基于 CPU/内存的自动扩缩容。设置 HPA,并确保 metrics-server 可用,以在高峰期自动扩到所需副本数。

Frequently Asked Questions

Add this skill to your agents

Related Skills

Sponsor this space

Reach thousands of developers