• 继续上一篇博客阅读 Kubernetes 源码,参照《k8s 源码阅读》首先学习 Kubernetes 的一些核心组件,首先是 kube-scheduler
  • 本文严重参考原文:《k8s 源码阅读》之 2.2 章节:scheduler,加入部分自己阅读的体会作为自己的阅读笔记
  • 感谢《k8s 源码阅读》的作者们辛苦编写教材,在此郑重表示感谢,望大家多多支持!~

1. 整体设计

1.1 概述

  • 官网描述:
    • The Kubernetes scheduler runs as a process alongside the other master components such as the API server.
    • Its interface to the API server is to watch for Pods with an empty PodSpec.NodeName, and for each Pod,
    • it posts a binding indicating where the Pod should be scheduled.
  • Scheduler 是相对独立的一个组件,主动访问 API server,寻找等待调度的 Pod(PodSpec.NodeName 为空)
  • 然后通过一系列调度算法寻找哪个 Node 适合跑这个 Pod
  • 然后将这个 Pod 和 Node 的绑定关系发给 API server,即整个调度流程

1.2 源码层次

  • cmd/kube-scheduler/scheduler.go:main() 函数入口位置,在 Scheduler 过程开始前的一系列初始化工作
  • pkg/scheduler/scheduler.go:调度框架整体逻辑,抽象调用各个算法的 Interface
  • pkg/scheduler/core/generic_scheduler.go:计算哪些 Node适合跑哪些 Pod 的具体算法

1.3 调度流程

  • 通过一系列的 Predicates(预选) 过滤掉不能运行 Pod 的 Node
    • 比如一个 Pod 需要 500M 的内存,有些节点剩余内存只有 100M 了,就会被剔除
  • 通过一系列的 Priorities(优选)给剩下的 Node 排一个分数,寻找能够运行 Pod 的 Node 中最合适的一个
  • 得分最高的一个 Node 胜出,获得了运行 Pod 的资格
  • 若是上述预选过程中没有找到任何一个合适的 Node,则进入抢占模式,移除部分低优先级的 Pod,再选择 Node

1.4 Predicates 和 Priorities 策略

  • Predicates 是用于过滤不合适 Node的策略
  • Priorities 是用于计算 Node 分数的策略(作用在通过 Predicates 过滤的 Node上)
  • Kubernetes 默认内建了一些 Predicates 和 Priorities 策略,代码分别在:
    • pkg/scheduler/algorithm/predicates/predicates.go
    • pkg/scheduler/algorithm/priorities/

1.5 调度策略的修改

  • 默认调度策略是通过 defaultPredicates() 和 defaultPriorities() 函数定义的
  • 代码在 pkg/scheduler/algorithmprovider/defaults/defaults.go
  • 可以通过命令行 flag --policy-config-file 来覆盖默认行为
    • 可以修改 pkg/scheduler/algorithm/predicates/predicates.go/pkg/scheduler/algorithm/priorities/,然后注册到 defaultPredicates() 和 defaultPriorities() 来实现
    • 或者通过配置文件:
{
"kind" : "Policy",
"apiVersion" : "v1",
"predicates" : [
    {"name" : "PodFitsHostPorts"},
    {"name" : "PodFitsResources"},
    {"name" : "NoDiskConflict"},
    {"name" : "NoVolumeZoneConflict"},
    {"name" : "MatchNodeSelector"},
    {"name" : "HostName"}
    ],
"priorities" : [
    {"name" : "LeastRequestedPriority", "weight" : 1},
    {"name" : "BalancedResourceAllocation", "weight" : 1},
    {"name" : "ServiceSpreadingPriority", "weight" : 1},
    {"name" : "EqualPriority", "weight" : 1}
    ],
"hardPodAffinitySymmetricWeight" : 10,
"alwaysCheckAllPredicates" : false
}
View Code

相关文章:

  • 2022-01-19
  • 2021-06-12
  • 2021-04-27
  • 2022-02-03
  • 2021-06-21
  • 2021-06-25
  • 2021-12-14
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-09-10
  • 2021-11-08
  • 2021-07-12
  • 2021-11-15
  • 2021-12-20
  • 2022-12-23
相关资源
相似解决方案