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