$npx -y skills add chaterm/terminal-skills --skill deploymentKubernetes Deployment 管理
| 1 | # Deployment 管理 |
| 2 | |
| 3 | ## 概述 |
| 4 | Deployment 滚动更新、回滚、扩缩容等技能。 |
| 5 | |
| 6 | ## 基础操作 |
| 7 | |
| 8 | ### 查看 Deployment |
| 9 | ```bash |
| 10 | # 列出 Deployment |
| 11 | kubectl get deployments |
| 12 | kubectl get deploy -o wide |
| 13 | kubectl get deploy -n namespace |
| 14 | |
| 15 | # 详细信息 |
| 16 | kubectl describe deploy deployment-name |
| 17 | kubectl get deploy deployment-name -o yaml |
| 18 | ``` |
| 19 | |
| 20 | ### 创建 Deployment |
| 21 | ```yaml |
| 22 | # deployment.yaml |
| 23 | apiVersion: apps/v1 |
| 24 | kind: Deployment |
| 25 | metadata: |
| 26 | name: nginx-deployment |
| 27 | labels: |
| 28 | app: nginx |
| 29 | spec: |
| 30 | replicas: 3 |
| 31 | selector: |
| 32 | matchLabels: |
| 33 | app: nginx |
| 34 | template: |
| 35 | metadata: |
| 36 | labels: |
| 37 | app: nginx |
| 38 | spec: |
| 39 | containers: |
| 40 | - name: nginx |
| 41 | image: nginx:1.20 |
| 42 | ports: |
| 43 | - containerPort: 80 |
| 44 | resources: |
| 45 | requests: |
| 46 | memory: "64Mi" |
| 47 | cpu: "100m" |
| 48 | limits: |
| 49 | memory: "128Mi" |
| 50 | cpu: "200m" |
| 51 | ``` |
| 52 | |
| 53 | ```bash |
| 54 | kubectl apply -f deployment.yaml |
| 55 | kubectl create deployment nginx --image=nginx:1.20 --replicas=3 |
| 56 | ``` |
| 57 | |
| 58 | ### 删除 Deployment |
| 59 | ```bash |
| 60 | kubectl delete deploy deployment-name |
| 61 | kubectl delete -f deployment.yaml |
| 62 | ``` |
| 63 | |
| 64 | ## 扩缩容 |
| 65 | |
| 66 | ```bash |
| 67 | # 手动扩缩容 |
| 68 | kubectl scale deploy deployment-name --replicas=5 |
| 69 | |
| 70 | # 自动扩缩容 (HPA) |
| 71 | kubectl autoscale deploy deployment-name --min=2 --max=10 --cpu-percent=80 |
| 72 | |
| 73 | # 查看 HPA |
| 74 | kubectl get hpa |
| 75 | kubectl describe hpa deployment-name |
| 76 | ``` |
| 77 | |
| 78 | ### HPA 配置 |
| 79 | ```yaml |
| 80 | apiVersion: autoscaling/v2 |
| 81 | kind: HorizontalPodAutoscaler |
| 82 | metadata: |
| 83 | name: nginx-hpa |
| 84 | spec: |
| 85 | scaleTargetRef: |
| 86 | apiVersion: apps/v1 |
| 87 | kind: Deployment |
| 88 | name: nginx-deployment |
| 89 | minReplicas: 2 |
| 90 | maxReplicas: 10 |
| 91 | metrics: |
| 92 | - type: Resource |
| 93 | resource: |
| 94 | name: cpu |
| 95 | target: |
| 96 | type: Utilization |
| 97 | averageUtilization: 80 |
| 98 | - type: Resource |
| 99 | resource: |
| 100 | name: memory |
| 101 | target: |
| 102 | type: Utilization |
| 103 | averageUtilization: 80 |
| 104 | ``` |
| 105 | |
| 106 | ## 滚动更新 |
| 107 | |
| 108 | ### 更新策略配置 |
| 109 | ```yaml |
| 110 | spec: |
| 111 | strategy: |
| 112 | type: RollingUpdate |
| 113 | rollingUpdate: |
| 114 | maxSurge: 25% # 最多超出期望副本数 |
| 115 | maxUnavailable: 25% # 最多不可用副本数 |
| 116 | ``` |
| 117 | |
| 118 | ### 执行更新 |
| 119 | ```bash |
| 120 | # 更新镜像 |
| 121 | kubectl set image deploy/deployment-name container-name=nginx:1.21 |
| 122 | |
| 123 | # 更新环境变量 |
| 124 | kubectl set env deploy/deployment-name ENV_VAR=value |
| 125 | |
| 126 | # 更新资源限制 |
| 127 | kubectl set resources deploy/deployment-name -c container-name --limits=cpu=200m,memory=256Mi |
| 128 | |
| 129 | # 应用配置文件更新 |
| 130 | kubectl apply -f deployment.yaml |
| 131 | |
| 132 | # 记录更新原因 |
| 133 | kubectl set image deploy/deployment-name container-name=nginx:1.21 --record |
| 134 | ``` |
| 135 | |
| 136 | ### 查看更新状态 |
| 137 | ```bash |
| 138 | # 查看滚动更新状态 |
| 139 | kubectl rollout status deploy/deployment-name |
| 140 | |
| 141 | # 查看更新历史 |
| 142 | kubectl rollout history deploy/deployment-name |
| 143 | kubectl rollout history deploy/deployment-name --revision=2 |
| 144 | |
| 145 | # 暂停/恢复更新 |
| 146 | kubectl rollout pause deploy/deployment-name |
| 147 | kubectl rollout resume deploy/deployment-name |
| 148 | ``` |
| 149 | |
| 150 | ## 回滚 |
| 151 | |
| 152 | ```bash |
| 153 | # 回滚到上一版本 |
| 154 | kubectl rollout undo deploy/deployment-name |
| 155 | |
| 156 | # 回滚到指定版本 |
| 157 | kubectl rollout undo deploy/deployment-name --to-revision=2 |
| 158 | |
| 159 | # 查看回滚状态 |
| 160 | kubectl rollout status deploy/deployment-name |
| 161 | ``` |
| 162 | |
| 163 | ## 高级配置 |
| 164 | |
| 165 | ### 健康检查 |
| 166 | ```yaml |
| 167 | spec: |
| 168 | template: |
| 169 | spec: |
| 170 | containers: |
| 171 | - name: app |
| 172 | livenessProbe: |
| 173 | httpGet: |
| 174 | path: /healthz |
| 175 | port: 8080 |
| 176 | initialDelaySeconds: 15 |
| 177 | periodSeconds: 10 |
| 178 | failureThreshold: 3 |
| 179 | readinessProbe: |
| 180 | httpGet: |
| 181 | path: /ready |
| 182 | port: 8080 |
| 183 | initialDelaySeconds: 5 |
| 184 | periodSeconds: 5 |
| 185 | startupProbe: |
| 186 | httpGet: |
| 187 | path: /startup |
| 188 | port: 8080 |
| 189 | failureThreshold: 30 |
| 190 | periodSeconds: 10 |
| 191 | ``` |
| 192 | |
| 193 | ### 亲和性配置 |
| 194 | ```yaml |
| 195 | spec: |
| 196 | template: |
| 197 | spec: |
| 198 | affinity: |
| 199 | # Pod 反亲和(分散部署) |
| 200 | podAntiAffinity: |
| 201 | preferredDuringSchedulingIgnoredDuringExecution: |
| 202 | - weight: 100 |
| 203 | podAffinityTerm: |
| 204 | labelSelector: |
| 205 | matchLabels: |
| 206 | app: nginx |
| 207 | topologyKey: kubernetes.io/hostname |
| 208 | # 节点亲和 |
| 209 | nodeAffinity: |
| 210 | requiredDuringSchedulingIgnoredDuringExecution: |
| 211 | nodeSelectorTerms: |
| 212 | - matchExpressions: |
| 213 | - key: node-type |
| 214 | operator: In |
| 215 | values: |
| 216 | - worker |
| 217 | ``` |
| 218 | |
| 219 | ### 容忍度 |
| 220 | ```yaml |
| 221 | spec: |
| 222 | template: |
| 223 | spec: |
| 224 | tolerations: |
| 225 | - key: "node-role.kubernetes.io/master" |
| 226 | operator: "Exists" |
| 227 | effect: "NoSchedule" |
| 228 | ``` |
| 229 | |
| 230 | ## 常见场景 |
| 231 | |
| 232 | ### 场景 1:蓝绿部署 |
| 233 | ```bash |
| 234 | # 创建新版本 Deployment |
| 235 | kubectl apply -f deployment-v2.yaml |
| 236 | |
| 237 | # 切换 Service 到新版本 |
| 238 | kubectl patch service my-service -p '{"spec":{"selector":{"version":"v2"}}}' |
| 239 | |
| 240 | # 验证后删除旧版本 |
| 241 | kubectl delete deploy deployment-v1 |
| 242 | ``` |
| 243 | |
| 244 | ### 场景 2:金丝雀发布 |
| 245 | ```yaml |
| 246 | # 创建金丝雀 Deployment(少量副本) |
| 247 | apiVersion: apps/v1 |
| 248 | kind: Deployment |
| 249 | metadata: |
| 250 | name: nginx-canary |
| 251 | spec: |
| 252 | replicas: 1 |
| 253 | selector: |
| 254 | matchLabels: |
| 255 | app: nginx |
| 256 | track: canary |
| 257 | template: |
| 258 | metadata: |
| 259 | labels: |
| 260 | app: nginx |
| 261 | track: canary |
| 262 | spec: |
| 263 | containers: |
| 264 | - name: nginx |
| 265 | image: nginx:1.21 |
| 266 | ``` |
| 267 | |
| 268 | ### 场景 3:批量重启 Pod |
| 269 | ```bas |