K8S系列之6.2:调度进阶(污点、容忍、亲和性与自定义调度器)
2026/6/25 21:52:24 网站建设 项目流程

Kubernetes调度器被称为集群的"大脑",负责将Pod分配到合适的节点。本章将从基础调度深入到高级调度策略,让你从"能调度"进阶到"调度好",实现资源的最优利用和业务的最佳性能。

引言:调度器的进化之路

从简单的随机调度到智能的感知调度,Kubernetes调度器经历了显著的演进:

随机调度
节点选择器
亲和性/反亲和性
污点与容忍
自定义调度器
多调度器协同

一、污点(Taints)与容忍(Tolerations):节点隔离的艺术

1.1 污点与容忍的核心概念

污点是节点的属性,用于排斥Pod;容忍是Pod的属性,允许Pod被调度到有特定污点的节点上。

Pod容忍
节点污点
匹配
不匹配
匹配
不匹配
容忍所有
关键服务
容忍部分
普通服务
容忍特定
特殊服务
NoSchedule
专用节点
NoExecute
问题节点
PreferNoSchedule
优选节点
Pod无法调度
Pod被驱逐

1.2 污点的三种效果详解

NoSchedule:硬性排斥
# 为GPU节点添加污点kubectl taint nodes gpu-node-1hardware=gpu:NoSchedule# 为生产环境节点添加污点kubectl taint nodes prod-node-1environment=production:NoSchedule
PreferNoSchedule:软性排斥
# 为测试节点添加软污点kubectl taint nodes test-node-1environment=test:PreferNoSchedule
NoExecute:驱逐排斥
# 当节点出现问题时,Kubernetes会自动添加NoExecute污点kubectl taint nodes unhealthy-node-1 node.kubernetes.io/unreachable:NoExecute# 手动添加维护污点kubectl taint nodes node-1maintenance=true:NoExecute

1.3 容忍的配置模式

完全容忍模式
# 容忍任意值的特定污点tolerations:-key:"node.kubernetes.io/unreachable"operator:"Exists"# 只要存在该键就容忍effect:"NoExecute"tolerationSeconds:6000# 6000秒后才驱逐
精确容忍模式
# 只容忍特定值的污点tolerations:-key:"hardware"operator:"Equal"# 必须等于指定值value:"gpu"effect:"NoSchedule"
多污点容忍模式
# 容忍多个污点tolerations:-key:"environment"operator:"Equal"value:"production"effect:"NoSchedule"-key:"dedicated"operator:"Exists"effect:"NoSchedule"-key:"node.kubernetes.io/not-ready"operator:"Exists"effect:"NoExecute"tolerationSeconds:300# 5分钟后驱逐

1.4 实战:构建多环境集群

集群节点规划
# 生产节点kubectl taint nodes prod-node-{1..3}environment=production:NoSchedule kubectl label nodes prod-node-{1..3}environment=productiontier=frontend# 预发节点kubectl taint nodes staging-node-{1..2}environment=staging:PreferNoSchedule kubectl label nodes staging-node-{1..2}environment=staging# GPU节点kubectl taint nodes gpu-node-{1..2}hardware=gpu:NoSchedule kubectl label nodes gpu-node-{1..2}hardware=gpuaccelerator=nvidia-tesla-v100# 测试节点kubectl label nodes test-node-{1..3}environment=test
应用容忍配置
# 生产环境应用apiVersion:apps/v1kind:Deploymentmetadata:name:production-appspec:template:s

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询