调度器、亲和性与污点容忍
"Pod 跑在哪个节点"这件事,K8s 交给 kube-scheduler 决定。默认行为是"哪儿有空位去哪儿",但生产需要精细控制:数据库要单独跑、同服务的多副本要打散、GPU 节点只跑 AI 任务、维护中的节点不能接新 Pod。
这篇讲清楚调度的两个阶段,以及 nodeSelector、亲和性、污点容忍、优先级抢占四种控制手段的适用场景。
一、调度器怎么工作
调度分两步,发生在 Pod 创建后、容器启动前:
1. 过滤(Filter):选出所有"能跑"的节点
- 资源够不够(requests ≤ allocatable)
- 端口冲突吗、亲和性满足吗、污点容忍吗
2. 打分(Score):给候选节点排名,选最优
- 资源均衡(CPU/内存使用率低的加分)
- 镜像本地性(节点已有镜像的加分,省拉取时间)
- 亲和性权重、拓扑分布约束绑定后 kubelet 接手拉镜像起容器。Pod 一直 Pending,99% 是第一步过滤后候选集为空——describe 看事件,scheduler 会告诉你为什么每个节点被拒:
kubectl describe pod <pod> | grep -A10 Events
# Warning FailedScheduling ... 0/5 nodes are available:
# 1 node(s) had untolerated taint {key: value}. 2 Insufficient cpu. 2 node(s) were unschedulable.调度是异步的:Pod 从创建到绑定通常几十毫秒,但大集群排队时会看到 Pending。kubectl get pods -o wide 的 NODE 列空着就是还没调度。
二、nodeSelector 与 nodeAffinity:把 Pod 放到指定节点
2.1 nodeSelector:精确匹配
最简单的方式,等值匹配节点标签:
spec:
nodeSelector:
disktype: ssd
kubernetes.io/os: linux节点打标签:
kubectl label nodes node1 disktype=ssd
kubectl label nodes node1 disktype- # 删除标签(减号)常用内置标签:kubernetes.io/hostname、kubernetes.io/os、kubernetes.io/arch、topology.kubernetes.io/zone(可用区)、node.role.kubernetes.io/control-plane。注意 1.24 起 master 污点的 key 从 node-role.kubernetes.io/master 改成了 node-role.kubernetes.io/control-plane,老教程的 key 在新集群不生效。
2.2 nodeAffinity:表达式匹配
需要"非等值"逻辑时用 nodeAffinity,分两种强度:
spec:
affinity:
nodeAffinity:
# required:硬性要求,不满足不调度
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: disktype
operator: In
values: [ssd, nvme]
- key: topology.kubernetes.io/zone
operator: NotIn
values: [zone-c] # 排除故障可用区
# preferred:软性偏好,满足加分,不满足也调度
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 80
preference:
matchExpressions:
- key: gpu-type
operator: In
values: [a100]
- weight: 20
preference:
matchExpressions:
- key: memory
operator: Gt
values: ["128"]operator 支持:In、NotIn、Exists、DoesNotExist、Gt、Lt(Gt/Lt 只支持单值,用于数值型标签)。
字段名的含义:requiredDuringScheduling(调度时必须满足)+ IgnoredDuringExecution(调度后节点标签变了不驱逐 Pod)。没有 RequiredDuringExecution 这个选项——K8s 不会因为你改了标签就杀 Pod,所以"标签变更 + 驱逐"要手动做(cordon/drain 换标签)。
2.3 podAffinity:按 Pod 关系调度
nodeAffinity 按"节点标签"选机器,podAffinity 按"节点上已有什么 Pod"选机器:
affinity:
podAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchLabels:
app: web
topologyKey: kubernetes.io/hostname # 和 web 在同一台机器affinity:
podAntiAffinity: # 反亲和:不要和 web 挤一起
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchLabels:
app: web
topologyKey: topology.kubernetes.io/zone # 同可用区不要有同名 PodtopologyKey 是拓扑域的标识:kubernetes.io/hostname(节点级)、topology.kubernetes.io/zone(可用区级)。写错 key 直接报错 couldn't parse key,或者调度不出来 Pod。
典型误用:反亲和用 requiredDuringScheduling 强制"同服务的 Pod 必须在不同节点",副本数超过节点数时永远调度失败。副本 10 个、节点 5 台的集群,应该用 preferredDuringScheduling(软约束)+ PodTopologySpread。软硬之分是亲和性设计的第一决策:除非有强正确性要求(如主从必须分开),一律用 preferred。
2.4 PodTopologySpreadConstraints:均匀打散
1.18 GA 的官方打散机制,比反亲和更直观:
spec:
topologySpreadConstraints:
- maxSkew: 1 # 各域间 Pod 数差不超过 1
topologyKey: kubernetes.io/hostname
whenUnsatisfiable: ScheduleAnyway # DoNotSchedule 是硬约束
labelSelector:
matchLabels:
app: webmaxSkew: 1 + 5 节点 10 副本 = 每节点 2 个,扩容时严格均匀。这是生产多副本服务的推荐做法,替代手写反亲和——它天然处理了"域数量不够时怎么办"(whenUnsatisfiable 选择硬失败还是降级)。
三、污点与容忍:节点说了算
亲和性是"Pod 挑节点",污点是"节点挑 Pod"。污点打在节点上,只有容忍该污点的 Pod 才能调度上来。
3.1 语法与效果
# 打污点
kubectl taint nodes node1 dedicated=gpu:NoSchedule
kubectl taint nodes node1 dedicated=gpu:NoSchedule- # 去掉
# YAML 方式spec:
tolerations:
- key: dedicated
operator: Equal
value: gpu
effect: NoScheduleeffect 三种:
| effect | 含义 |
|---|---|
| NoSchedule | 不容忍就不调度上来(已运行的不管) |
| PreferNoSchedule | 尽量别来,没地方了也来 |
| NoExecute | 已运行的 Pod 也被驱逐(一段时间宽限后) |
operator 用 Exists 时不需要 value,容忍该 key 的所有污点——宽容配写起来省事,但注意别把危险污点也忍了。
3.2 内置污点(必须认识)
| 污点 | 触发条件 | 影响 |
|---|---|---|
| node.kubernetes.io/not-ready | 节点 NotReady | 默认容忍,等 5 分钟开始重调度 |
| node.kubernetes.io/unreachable | 节点失联 | 同上 |
| node.kubernetes.io/memory-pressure | 节点内存紧张 | 不容忍的 Pod 被驱逐 |
| node.kubernetes.io/disk-pressure | 磁盘紧张 | 同上 |
| node.kubernetes.io/pid-pressure | PID 耗尽 | 同上 |
| node.kubernetes.io/unschedulable | cordon 过 | 不容忍不调度 |
| node.kubernetes.io/network-unavailable | 节点网络没配好 | 不容忍不调度 |
| node-role.kubernetes.io/control-plane:NoSchedule | 控制平面默认污点 | 业务 Pod 不上 master |
系统组件(kube-proxy、CNI、监控 agent)都内置容忍这些污点,所以节点 NotReady 时它们还在。自己写的 DaemonSet 忘了加 tolerations,NotReady 节点就丢监控——这是最常见的疏漏。
3.3 经典组合:专用节点
# 1. 给一批节点打污点,只许特定负载上来
kubectl taint nodes gpu-node1 dedicated=gpu:NoSchedule
# 2. 业务 Pod 加 tolerations + nodeSelectorspec:
nodeSelector:
gpu-type: a100
tolerations:
- key: dedicated
operator: Equal
value: gpu
effect: NoSchedule维护节点的完整流程:kubectl cordon node1(加 unschedulable 污点,新 Pod 不上来)→ kubectl drain node1 --ignore-daemonsets --delete-emptydir-data(老 Pod 撤走)→ 维护 → kubectl uncordon node1。
四、Pod 优先级与抢占
资源紧张时"谁让谁"是真实问题:集群打满,来了个核心业务的 Pod,跑还是不跑?PriorityClass 定义优先级,调度器按优先级抢占。
apiVersion: scheduling.k8s.io/v1
kind: PriorityClass
metadata:
name: high-priority
value: 1000000 # 越大越优先,保留段:
preemptionPolicy: Preempt # Preempt(抢占)/ Never(只排队不抢)
globalDefault: false # 整个集群无优先级 Pod 的默认值,只能有一个 true
description: "核心支付服务"
---
# Pod 引用
spec:
priorityClassName: high-priority内置优先级(别乱占这些值):
| 类 | 值 | 用途 |
|---|---|---|
| system-cluster-critical | 2000000000 | kube-apiserver 等 |
| system-node-critical | 2000001000 | kubelet、CNI 等节点关键组件 |
抢占的工作方式:高优先级 Pod 调度失败时,scheduler 模拟"杀掉某些低优先级 Pod 能不能腾出地方",选牺牲最少的节点,把受害者标记驱逐,然后调度高优 Pod。受害者有 terminationGracePeriod 的宽限时间,优雅退出。
生产提醒:
- 优先级是双刃剑:全员 high-priority 等于没有优先级。给业务分级(核心 > 一般 > 离线批处理),离线任务用负优先级(如 -1),资源紧张时第一个被牺牲。
- 抢占不保证不误杀:被抢的可能是运行中的有状态服务。核心服务自己要有 PDB(PodDisruptionBudget)保护自愿驱逐,但抢占是另一条路径——所以核心业务的副本数要能在"损失一个"时继续服务。
- 配合 requests 才有效:Pod 没写 requests 时 Burstable/BestEffort,调度按 0 计算,抢不抢占都算不准。没有 requests 的集群,优先级体系形同虚设。
五、调度相关排错速查
# Pod 为什么 Pending
kubectl describe pod <pod> | grep -A10 Events
# 节点为什么不可调度
kubectl describe node <node> | grep -i taint
kubectl get nodes -o custom-columns=NAME:.metadata.name,TAINTS:.spec.taints[*].key
# Pod 最终落在哪、为什么
kubectl get pod <pod> -o wide
# 调度决策过程要看 scheduler 日志(-v=4)
kubectl -n kube-system logs kube-scheduler-<node> | grep <pod-name>
# 资源是否真的够
kubectl describe node <node> | grep -A10 Allocated| Pending 原因(事件原文) | 解法 |
|---|---|
0/N nodes are available: N Insufficient cpu | 加节点、降 requests、配 HPA/VPA |
had untolerated taint | 加 tolerations 或去掉污点 |
node(s) didn't match Pod's node affinity | 改 nodeSelector/affinity 或给节点打标签 |
node(s) didn't match pod anti-affinity rules | 反亲和太严,改 preferred 或扩节点 |
no nodes available to schedule pods | 全 cordon 了或全 NotReady |
小结
调度控制手段按侵入性排序:nodeSelector(简单精确)→ nodeAffinity(表达式)→ podAffinity(Pod 关系)→ topologySpread(均匀分布)→ 污点容忍(节点侧准入)→ 优先级抢占(资源仲裁)。生产最佳实践一句话:默认均匀打散(topologySpread + preferred 反亲和),特殊节点用污点隔离,核心业务分级用 PriorityClass。Pod 有了资源限制和健康检查,加上调度策略,才是一份能上生产的工作负载清单——下一篇讲这两件事。
