SuperAPI AI Directory

deployment

Deployment & operations

View repository

Updated 10-15 17:33

About this skill

Deployment Management

Overview

Skills for Deployment rolling updates, rollbacks, scaling, and more.

Basic Operations

View 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

Create 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

Delete Deployment

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

Scaling

# 手动扩缩容
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 Configuration

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

Rolling Update

Update Strategy Configuration

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

Perform Update

# 更新镜像
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

View Update Status

# 查看滚动更新状态
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

Rollback

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

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

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

Advanced Configuration

Health Checks

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

Affinity Configuration

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

Tolerations

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

Common Scenarios

Scenario 1: Blue-Green Deployment

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

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

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

Scenario 2: Canary Release

# 创建金丝雀 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

Scenario 3: Batch Restart Pods

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

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

Scenario 4: View Pod Distribution

# 查看 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

Troubleshooting

Problem Troubleshooting Method
Update Stuck kubectl rollout status, check Pod status
Pod Cannot Be Scheduled kubectl describe pod, check resources and affinity
Service Abnormal After Update Check health check configuration, roll back
HPA Not Working Check metrics-server, resource configuration
# 查看 Deployment 事件
kubectl describe deploy deployment-name | grep -A 20 Events

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