污点和容忍度
taints-and-tolerations 调度隔离。
30 分钟
污点和容忍度
默认 Scheduler 把 Pod 放到 资源够、.selector 匹配 的任意节点。污点(Taint) 与 容忍度(Toleration) 让节点 拒绝 不含对应容忍的 Pod——实现专用节点池、GPU 隔离、控制平面保护。本课对应路线图 taints-and-tolerations。
学习目标
- 解释 Taint 的
key=value:Effect语法与三种 Effect - 为专用节点池设计 Taint + Toleration 配对
- 区分 Taint/Toleration 与 Node Affinity 的分工
基本概念
Taint 打在 Node 上:「除非你能容忍,否则别来」。
Toleration 写在 Pod 上:「我可以忍受这种污点」。
# Node 示例
taints:
- key: dedicated
value: gpu
effect: NoSchedule
# Pod 示例
tolerations:
- key: dedicated
operator: Equal
value: gpu
effect: NoSchedule
与 k8s-pods 的 nodeSelector 相比,Taint 是 推(节点拒 Pod),Affinity 是 拉(Pod 选节点),常 组合使用。
Effect 三种
| Effect | 行为 |
|---|---|
| NoSchedule | 新 Pod 不调度上来(已在跑的不受影响) |
| PreferNoSchedule | 软拒绝,实在没节点才可能上来 |
| NoExecute | 不调度 + 驱逐 已运行且无 toleration 的 Pod |
NoExecute 配合 tolerationSeconds 可做过渡驱逐,与 k8a-resources 的节点压力驱逐不同——这是 策略性 隔离。
典型场景
| 场景 | Taint 示例 |
|---|---|
| 控制平面节点 | node-role.kubernetes.io/control-plane:NoSchedule(内置) |
| GPU 节点池 | nvidia.com/gpu=present:NoSchedule |
| 批处理 Spot | workload=batch:NoSchedule + 容忍的 Job Pod |
| 新节点预热 | 临时 taint,测完再去掉 |
| 单租户硬隔离 | 专用 taint + namespace 级准入(进阶) |
托管集群节点池创建时可 启动时带 taint;Helm values 里为特定 Deployment 加 tolerations,见 helm-templates。