3. 调度器参数详解:kube-scheduler 配置文件结构、常用参数解析

kube-scheduler 是 Kubernetes 控制平面的核心组件之一,负责将待调度的 Pod 绑定到最合适的 Node 上。从 Kubernetes v1.23 开始,调度器全面采用 KubeSchedulerConfiguration API 作为主要配置方式,摒弃了传统的命令行标志。本章将深入解析配置文件的层级结构、核心参数及其对调度行为的影响。

3.1 配置文件结构总览

KubeSchedulerConfiguration 是一个 YAML 或 JSON 格式的 API 对象,其顶层结构包含以下关键字段:

字段 类型 说明
apiVersion string 配置 API 版本,当前稳定版为 kubescheduler.config.k8s.io/v1
kind string 固定为 KubeSchedulerConfiguration
profiles []SchedulerProfile 调度器配置文件列表,支持多 profile 多调度器实例
extenders []Extender 调度器扩展器配置,用于对接外部调度逻辑
leaderElection LeaderElectionConfiguration 高可用场景下的领导者选举配置
clientConnection ClientConnectionConfiguration 与 API Server 的连接参数(QPS、Burst 等)
percentageOfNodesToScore int32 调度时参与评分的节点百分比,默认 50%
podInitialBackoffSeconds int64 Pod 调度失败后的初始退避时间(秒)
podMaxBackoffSeconds int64 Pod 调度失败后的最大退避时间(秒)
关键理解: profiles 字段是配置的核心,每个 profile 可以定义独立的调度插件集合、插件权重和扩展点行为。这意味着你可以在同一个调度器进程中运行多个不同策略的调度实例。

3.2 Profiles 与调度插件体系

每个 SchedulerProfile 包含以下子结构:

  • schedulerName:调度器名称,Pod 通过 spec.schedulerName 选择对应的 profile。
  • plugins:插件配置,包含多个扩展点(QueueSort、PreFilter、Filter、PostFilter、PreScore、Score、Reserve、Permit、PreBind、Bind、PostBind、MultiPoint)。
  • pluginConfig:插件的自定义参数,每个插件可以独立配置。

调度器插件体系采用“扩展点”架构,Pod 的调度生命周期依次经过以下阶段:

调度器扩展点执行流程 QueueSort PreFilter Filter PostFilter PreScore Score Reserve Permit PreBind Bind 说明:每个扩展点可以启用或禁用多个插件。Filter 扩展点执行“硬约束”检查,Score 扩展点执行“软约束”打分。 PostFilter 仅在 Filter 阶段所有节点均被排除时触发,用于执行抢占逻辑。

3.3 常用参数深度解析

3.3.1 节点评分相关参数

参数 默认值 作用域 说明
percentageOfNodesToScore 50 全局 控制参与 Filter 和 Score 的节点比例。集群规模 > 100 节点时生效,用于提升调度性能。
scoringStrategy LeastAllocated Profile 节点资源打分策略,可选 LeastAllocated、MostAllocated、RequestedToCapacityRatio。
resources cpu, memory Profile 指定参与打分的资源类型及其权重。
实战建议: 在大型集群(>1000 节点)中,将 percentageOfNodesToScore 降低到 10-20 可以显著减少调度延迟,但可能降低最优节点选择的精确度。建议结合业务容忍度进行调优。

3.3.2 调度失败重试与退避

当 Pod 调度失败时,调度器会将其重新入队并等待一段时间后重试。退避策略由以下参数控制:

  • podInitialBackoffSeconds:首次失败后的等待时间,默认 1 秒。
  • podMaxBackoffSeconds:最大等待时间,默认 10 秒。
  • 退避算法:实际等待时间 = min(初始退避 × 2^(失败次数), 最大退避)。

3.3.3 插件级参数配置示例

以下是一个完整的 KubeSchedulerConfiguration 示例,展示了如何配置自定义插件参数:

apiVersion: kubescheduler.config.k8s.io/v1
kind: KubeSchedulerConfiguration
leaderElection:
  leaderElect: true
  resourceNamespace: kube-system
  resourceName: kube-scheduler
clientConnection:
  qps: 50
  burst: 100
percentageOfNodesToScore: 30
podInitialBackoffSeconds: 2
podMaxBackoffSeconds: 20
profiles:
  - schedulerName: default-scheduler
    plugins:
      score:
        disabled:
          - name: NodeResourcesBalancedAllocation
        enabled:
          - name: NodeResourcesLeastAllocated
            weight: 2
          - name: NodeAffinity
            weight: 1
    pluginConfig:
      - name: NodeResourcesLeastAllocated
        args:
          scoringStrategy:
            type: LeastAllocated
            resources:
              - name: cpu
                weight: 1
              - name: memory
                weight: 1
      - name: InterPodAffinity
        args:
          hardPodAffinityWeight: 2
  - schedulerName: high-priority-scheduler
    plugins:
      filter:
        enabled:
          - name: NodeUnschedulable
          - name: NodeName
          - name: TaintToleration
          - name: NodeAffinity
      score:
        enabled:
          - name: NodeResourcesMostAllocated
            weight: 3
          - name: ImageLocality
            weight: 1

3.4 调度器参数与 OPA Gatekeeper 的协同点

在 OPA Gatekeeper 策略与调度器参数协同配置的上下文中,以下参数直接影响策略设计:

调度器参数 OPA 策略协同点 典型场景
scoringStrategy 约束 Pod 的资源请求比例,确保打分策略与资源配额一致 强制所有 Pod 设置 CPU/Memory 的 request 和 limit
plugins.filter 通过 Gatekeeper 验证 Pod 的 NodeSelector 或 Tolerations 是否合规 禁止 Pod 调度到特定污点节点
percentageOfNodesToScore 配合策略限制大规模集群中的 Pod 分布范围 确保关键业务 Pod 只调度到指定拓扑域
podMaxBackoffSeconds 防止因策略拒绝导致 Pod 无限重试 设置合理的退避上限,避免调度队列积压
注意: 调度器参数与 OPA 策略的协同不是简单的叠加。例如,如果 Gatekeeper 强制所有 Pod 使用 NodeResourcesMostAllocated 打分策略,但调度器 profile 中配置的是 LeastAllocated,则策略与调度行为会产生冲突。建议在配置前先通过 kube-scheduler --config 验证参数合法性。

3.5 参数验证与调试

在将配置文件应用到生产环境前,建议执行以下验证步骤:

  1. 语法验证:使用 kube-scheduler --config /path/to/config.yaml --version 检查配置是否可被解析。
  2. 插件可用性检查:确保启用的插件在调度器编译时已包含(可通过 kube-scheduler --plugins 查看)。
  3. 调度模拟:使用 kubectl create --dry-run=client 配合自定义调度器名称,观察调度决策是否符合预期。
  4. 日志监控:设置 --v=4 或更高日志级别,查看调度器在每个扩展点的详细决策日志。

通过深入理解 kube-scheduler 的配置文件结构和参数含义,你可以精确控制 Pod 的调度行为,并为后续与 OPA Gatekeeper 策略的协同配置打下坚实基础。