Files
Deploy-Laboratory/docs/04-06-nodejs-副本与滚动发布.md
2026-03-27 16:58:41 +08:00

85 lines
3.0 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 04-06-nodejs-副本与滚动发布
> 调整 `nodejs-demo` 的 **副本数** 与 **滚动更新策略**,实现多实例与可控发布。
## TL;DR
- **自动化验收**`./scripts/verify.sh run 04-06`
- **关键前置**:按本文「前置条件」准备环境变量/Secret/入口 IP
- **成功判据**:达到本文「预期」且 playbook 断言通过
- **排障**:见本文「排障」
## 前置条件
- 已部署 `nodejs-demo``04-01`)。
- 多副本时应用须 **无状态** 或可共享会话;否则需粘性会话/外部会话存储(本文不展开)。
## 清单路径(唯一真源)
| 本篇完整清单 | [`ansible/files/04-01/04-06-nodejs-demo.yaml`](../ansible/files/04-01/04-06-nodejs-demo.yaml) |
| 应用 | `kubectl apply -f ansible/files/04-01/04-06-nodejs-demo.yaml` |
`replicas``strategy`**Deployment.spec** 下,与 `selector` / `template` 同级。
## 场景说明(白话)
- **多副本**:同样应用跑多份,一台挂了别的还能接客;配合 Service 做负载均衡。
- **滚动发布**:换新版本时**一个一个 Pod 换**,而不是全停再起(可通过 `maxSurge` / `maxUnavailable` 调「多激进」)。
### 相对 `04-05` 的变更(原文 → 新文)
| 位置 | 原文(`04-05` | 新文(`04-06` |
|------|-----------------|-----------------|
| `spec.replicas` | `1` | `3` |
| `spec.strategy` | (默认 RollingUpdate | 显式 `RollingUpdate``maxSurge: 1``maxUnavailable: 0` |
验证:
```bash
kubectl get deploy nodejs-demo -n default
kubectl get pod -l app=nodejs-demo -n default -o wide
```
- **`maxUnavailable: 0`**:发布时先起新 Pod 再摘旧 Pod适合要求 **不中断** 的场景(需足够资源 surge
- 资源紧张时可适当允许 `maxUnavailable: 1`
## 发布新版本
```bash
kubectl set image deployment/nodejs-demo nodejs-demo=node:20-alpine -n default
kubectl rollout status deployment/nodejs-demo -n default
```
## 回滚
```bash
kubectl rollout undo deployment/nodejs-demo -n default
kubectl rollout history deployment/nodejs-demo -n default
```
## 验证
```bash
curl -s --max-time 3 -H "Host: app.example.local" "http://<节点IP>/api/"
```
多次请求应看到多 Pod 分担(若 Service 为 ClusterIP + Ingress由 kube-proxy/Traefik 负载)。
## 失败排查
- **一直滚动中**:新 Pod 未 Ready探针、镜像拉取`kubectl describe deploy` / `kubectl get rs`
- **会话漂移**:多副本下登录态不一致为应用架构问题。
- `06-01-k3s-networkpolicy-故障排查.md`
## 相关文档
- [`04-05-nodejs-探针与健康检查.md`](04-05-nodejs-探针与健康检查.md)
- [`04-13-nodejs-HPA.md`](04-13-nodejs-HPA.md)
## 排障
- **先看 playbook 输出**:失败时先定位是 deploy/wait/http_check 哪一步。
- **集群侧总览**`kubectl get nodes -o wide``kubectl -n kube-system get pods -o wide`
- **事件与日志**`kubectl -n <ns> describe ...``kubectl -n <ns> logs ... --tail=200`