Files
Deploy-Laboratory/docs/04-09-nodejs-调度与亲和.md
2026-03-27 16:58:41 +08:00

71 lines
2.9 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-09-nodejs-调度与亲和
> 控制 `nodejs-demo` **落在哪些节点**`nodeSelector`、`affinity`、`tolerations`。常用于与 Traefik、存储或合规区域对齐。
## TL;DR
- **自动化验收**`./scripts/verify.sh run 04-09`
- **关键前置**:按本文「前置条件」准备环境变量/Secret/入口 IP
- **成功判据**:达到本文「预期」且 playbook 断言通过
- **排障**:见本文「排障」
## 前置条件
- 已部署 `nodejs-demo``04-01`);集群至少一个节点带可区分 **label**(例如 `kubectl get nodes --show-labels`)。
## 清单路径(唯一真源)
| 本篇完整清单 | [`ansible/files/04-01/04-09-nodejs-demo.yaml`](../ansible/files/04-01/04-09-nodejs-demo.yaml) |
| 应用 | `kubectl apply -f ansible/files/04-01/04-09-nodejs-demo.yaml` |
清单中默认 `nodeSelector: kubernetes.io/hostname: ylc62`,请改为本集群节点名。
## 场景说明(白话)
- **想让 Pod 只跑在某几台机器上**:给节点打标签,在 Pod 里写 **`nodeSelector`**,最简单。
- **规则更复杂**(尽量分散、尽量和某类 Pod 同机架等):用 **affinity亲和**
- **节点有「污点」**像「专属机器」Pod 必须配置 **容忍污点tolerations** 才能调度上去。
### 相对 `04-08` 的变更(原文 → 新文)
| 位置 | 原文(`04-08` | 新文(`04-09` |
|------|-----------------|-----------------|
| `template.spec.nodeSelector` | (无) | `kubernetes.io/hostname: ylc62`(请按环境修改) |
仅当节点具备该标签键值时 Pod 才可调度;否则 **Pending**
## 亲和性(示意,未写入默认累积清单)
**说明**:与 `nodeSelector` **不要硬混用冲突条件**`affinity` / `tolerations` 语法可参考 Kubernetes 文档,在本地改清单或 `kubectl patch` 实验。(示例:`node-role.kubernetes.io/worker` + `Exists`。)
## 容忍污点 tolerations
若目标节点有 `taints`,需在 Pod 上配置对应 `tolerations`,否则无法调度。
## 部署与验证
```bash
kubectl apply -f ansible/files/04-01/04-09-nodejs-demo.yaml
kubectl get pod -l app=nodejs-demo -n default -o wide
```
确认 **NODE** 列符合预期。
## 失败排查
- **Pending**`kubectl describe pod` 看 Events`0/X nodes are available`);检查 selector/affinity/taint。
- 与 Traefik DaemonSet 同节点时注意主机端口与防火墙见工作节点、OpenWrt HAProxy 文档)。
## 相关文档
- `01-02-k3s-工作节点.md`
- `02-00-nginx-系列说明.md`(调度思路通用)
- `06-01-k3s-networkpolicy-故障排查.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`