使用污点和容忍度实现 GPU 节点专属 DaemonSet
解读
在国内云原生面试中,这道题常被用来区分“会用 DaemonSet”与“懂调度原理”的候选人。
面试官真正想听的是:
- 你能否把“GPU 节点只能跑 GPU 业务”这一硬性需求,翻译成 Kubernetes 的调度语言;
- 你是否理解污点和容忍度的协作方式,以及它们在 DaemonSet 生命周期里的特殊之处;
- 你能否在生产环境里闭环:从节点标记、Pod 模板、更新策略到监控告警,给出可落地的方案。
回答时切忌只背命令,而要体现“节点异构资源隔离”这一核心思想。
知识点
- 污点(Taint)三要素:key、value、effect(NoSchedule / PreferNoSchedule / NoExecute)。
- 容忍度(Toleration)三要素:key、operator、effect,以及可选的 tolerationSeconds。
- DaemonSet 的调度逻辑:
- 1.18+ 默认通过 ScheduleDaemonSetPods 控制器,由 kube-scheduler 统一调度;
- 老版本由 DaemonSet Controller 直接绑定,忽略未容忍的节点。
- 节点标签与污点区别:标签用于“优选”,污点用于“硬拒”,二者互补。
- 国内常见 GPU 机型:
- 阿里云 ebmgn6v、腾讯云 GN7、华为云 G5,均自带 nvidia.com/gpu 资源名;
- 裸金属场景需手动给节点打污点 gpu=true:NoSchedule,防止普通业务误调度。
- 生产闭环:
- 节点上线时通过 Ansible / kubelet 启动参数自动打污点;
- DaemonSet 用 onDelete 或 RollingUpdate(maxUnavailable=1)保证驱动 Pod 不中断;
- Prometheus 通过 kube_node_spec_taint 指标监控污点一致性;
- 故障演练:临时去掉污点,观察非 GPU Pod 是否被禁止调度。
答案
步骤一:给 GPU 节点打污点
kubectl taint node gpu-node{1..3} gpu=exclusive:NoSchedule
说明:effect 用 NoSchedule 即可,NoExecute 会驱逐已运行 Pod,可能误伤系统组件。
步骤二:编写 DaemonSet 模板,在 spec.template.spec 里声明容忍度
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: nvidia-device-plugin-ds
namespace: kube-system
spec:
selector:
matchLabels:
name: nvidia-device-plugin
template:
metadata:
labels:
name: nvidia-device-plugin
spec:
tolerations:
- key: "gpu"
operator: "Equal"
value: "exclusive"
effect: "NoSchedule"
containers:
- image: registry.cn-hangzhou.aliyuncs.com/acs/nvidia-device-plugin:v0.14.0
name: nvidia-device-plugin
resources:
requests:
cpu: 50m
memory: 50Mi
limits:
cpu: 50m
memory: 50Mi
volumeMounts:
- name: device-plugin
mountPath: /var/lib/kubelet/device-plugins
volumes:
- name: device-plugin
hostPath:
path: /var/lib/kubelet/device-plugins
updateStrategy:
type: RollingUpdate
rollingUpdate:
maxUnavailable: 1
关键点:
- 容忍度必须与污点完全匹配,operator 用 Equal 最直观;
- 镜像源改用国内阿里云镜像仓库,避免 Docker Hub 拉取超时;
- 更新策略设为 RollingUpdate 且 maxUnavailable=1,保证驱动 Pod 滚动时 GPU 节点始终有设备插件可用。
步骤三:验证
kubectl get ds -n kube-system nvidia-device-plugin-ds -o wide
# 期望 READY 等于 GPU 节点数,且只分布在 gpu=exclusive 的节点
kubectl describe node gpu-node1 | grep -i taint
# 确认污点存在
kubectl get pod -n kube-system -l name=nvidia-device-plugin -o wide
# 确认 Pod 全部落在 GPU 节点
步骤四:加固
- 在集群初始化脚本里用 kubeadm init --config 配合 NodeRegistrationOptions.taints,确保新 GPU 节点自动带污点;
- 用 OPA Gatekeeper 约束模板“禁止没有 gpu=exclusive:NoSchedule 污点的节点出现 nvidia.com/gpu 资源”,防止人工误操作;
- 对 DaemonSet 加 PodDisruptionBudget,预算为 0,避免集群自动缩容时把 GPU 插件一起下线。
拓展思考
- 多 GPU 厂商混部:
若节点同时存在 NVIDIA 与华为昇腾,需要两套污点:gpu=nvidia:NoSchedule 与 npu=ascend:NoSchedule,对应两个 DaemonSet,分别容忍自己的污点。 - 动态污点:
利用 Node Feature Discovery(NFD)检测 GPU 设备热插拔,自动增删污点,实现“GPU 节点即插即用”。 - 成本优化:
对 GPU 节点加第二个污点 gpu=preemptible:NoExecute,并给离线训练任务加 tolerationSeconds=3600,实现“白天在线业务,夜间离线抢占”,提升国内昂贵的 GPU 裸金属利用率。 - 安全隔离:
结合 seccomp、apparmor 与容器运行时 nvidia-container-toolkit,确保 GPU 插件容器即使被入侵也无法访问宿主机 /dev 之外设备;污点机制与 RBAC 配合,限制只有 gpu-admin 组才能提交带对应容忍度的 Pod,防止普通用户“蹭卡”。