add-license-1
yinwenqin 6 years ago
parent a6be1a670e
commit 0908b1ec33

BIN
.DS_Store vendored

Binary file not shown.

BIN
scheduler/.DS_Store vendored

Binary file not shown.

@ -0,0 +1,63 @@
调度器框架
## 前言
在上一篇文档中我们找到了sheduler调度功能主逻辑的入口:
[**P1-调度器入口篇**](https://github.com/yinwenqin/kubeSourceCodeNote/blob/master/scheduler/P1-%E8%B0%83%E5%BA%A6%E5%99%A8%E5%85%A5%E5%8F%A3%E7%AF%87.md)
那么在本篇,我们基于找到的入口,来进入调度器框架内部,看一看整体的逻辑流程,本篇先跳过调度的算法(Predicates断言选择、Priority优先级排序),只关注`pkg/scheduler`目录内的scheduler框架相关的逻辑流向摸清scheduler框架本身的代码结构调度算法留在后面的文章再谈
## 框架流程
让回到我们上一篇篇末找到的调度逻辑入口位置,`pkg/scheduler/scheduler.go:435`, `scheduleOne()`函数内部,定位在`pkg/scheduler/scheduler.go:457`位置,是通过这个`sched.schedule(pod)`方法来获取与pod匹配的node的我们直接跳转2次,来到了这里`pkg/scheduler/core/generic_scheduler.go:107`
[image](https://github.com/yinwenqin/kubeSourceCodeNote/blob/master/scheduler/image/p2/schedule.jpg)
[image](https://github.com/yinwenqin/kubeSourceCodeNote/blob/master/scheduler/image/p2/AlgSchedule.jpg)
[image](https://github.com/yinwenqin/kubeSourceCodeNote/blob/master/scheduler/image/p2/scheduleStruct.jpg)
通过注释可以知道ScheduleAlgorithm interface中的Schedule方法就是用来为pod筛选node的但这是个接口方法并不是实际调用的我们稍微往下,在`pkg/scheduler/core/generic_scheduler.go:162`这个位置就可以找到实际调用的Schedule方法:
[image](https://github.com/yinwenqin/kubeSourceCodeNote/blob/master/scheduler/image/p2/genericSchedule.jpg)
本篇我们不看Schedule方法内的具体调度算法细节先来逆向回溯代码结构找到哪里创建了scheduler调度器的默认初始化配置默认的调度算法来源等等框架相关的东西。`Schedule()`方法属于`genericScheduler`结构体,先查看`genericScheduler`结构体再选中结构体名称crtl + b组合键查看它在哪些地方被引用找出创建结构体的位置:
[image](https://github.com/yinwenqin/kubeSourceCodeNote/blob/master/scheduler/image/p2/createGenSche.jpg)
通过缩略代码框排除test相关的测试文件很容易找出创建结构体的地方位于`pkg/scheduler/core/generic_scheduler.go:1189`,点击图中红框圈中位置,跳转过去,果然找到了`NewGenericScheduler()`方法,这个方法是用来创建一个`genericScheduler`对象的那么我们再次crtl + b组合键查看`NewGenericScheduler`再什么地方被调用:
[image](https://github.com/yinwenqin/kubeSourceCodeNote/blob/master/scheduler/image/p2/newGenericScheduler.jpg)
找出了在`pkg/scheduler/factory/factory.go:441`这个位置上找到了调用入口,这里位于`CreateFromKeys()`方法中继续crtl + b查看它的引用,跳转到`pkg/scheduler/factory/factory.go:336`这个位置:
[image](https://github.com/yinwenqin/kubeSourceCodeNote/blob/master/scheduler/image/p2/createFromKeys.jpg)
[image](https://github.com/yinwenqin/kubeSourceCodeNote/blob/master/scheduler/image/p2/createFromProvider.jpg)
[image](https://github.com/yinwenqin/kubeSourceCodeNote/blob/master/scheduler/image/p2/getAlgorithmProvider.jpg)
这里找到了`algorithmProviderMap`这个变量,顾名思义,这个变量里面包含的应该就是调度算法的来源,点击进去查看,跳转到了`pkg/scheduler/factory/plugins.go:86`这个位置,组合键查看引用一眼就可以看出哪个引用为这个map添加了元素
[image](https://github.com/yinwenqin/kubeSourceCodeNote/blob/master/scheduler/image/p2/addMapEle.jpg)
跳转过去,来到了`pkg/scheduler/factory/plugins.go:391`这个位置这个函数的作用是为scheduler的配置指定调度算法即`FitPredicate、Priority`这两个算法需要用到的metric或者方法,再次请出组合键,查找哪个地方调用了这个方法:
[image](https://github.com/yinwenqin/kubeSourceCodeNote/blob/master/scheduler/image/p2/registerAlgorithmProvider.jpg)
来到了`pkg/scheduler/algorithmprovider/defaults/defaults.go:99`,继续组合键向上查找引用,这次引用只有一个,没有弹窗直接跳转过去了`pkg/scheduler/algorithmprovider/defaults/defaults.go:36`:
[image](https://github.com/yinwenqin/kubeSourceCodeNote/blob/master/scheduler/image/p2/registerAlgorithmProvider1.jpg)
[image](https://github.com/yinwenqin/kubeSourceCodeNote/blob/master/scheduler/image/p2/init.jpg)
我们来看看`defaultPredicates(), defaultPriorities()`这两个函数具体的内容:
[image](https://github.com/yinwenqin/kubeSourceCodeNote/blob/master/scheduler/image/p2/default.jpg)
我们随便点击进去一个`predicates`选项查看其内容:
[image](https://github.com/yinwenqin/kubeSourceCodeNote/blob/master/scheduler/image/p2/memPressure.jpg)
`CheckNodeMemoryPressure`这个词相应熟悉kubernetes pod的朋友一定不会陌生在node内存压力大无法调度的pod时`kubectl describe pod xxx`就会在状态信息里面看到这个关键词。同时,该变量的定义位于`pkg/scheduler/algorithm/predicates/predicates.go:102``pkg/scheduler/algorithm/`这个目录在调度器的总览中已经讲过是调度算法相关的目录,`defaultPredicates(), defaultPriorities()`里面的内容都是位于这个目录中,具体调度器的断言过滤、优先级排序算法详情将在下篇中详解,本篇调度器框架结构梳理到此结束!

@ -2,7 +2,7 @@
## 调度器源码分段阅读目录
- [调度器入口](https://github.com/yinwenqin/kubeSourceCodeNote/blob/master/scheduler/P1-%E8%B0%83%E5%BA%A6%E5%99%A8%E5%85%A5%E5%8F%A3%E7%AF%87.md)
- 待补充
- [调度器框架](https://github.com/yinwenqin/kubeSourceCodeNote/blob/master/scheduler/P1-%E8%B0%83%E5%BA%A6%E5%99%A8%E5%85%A5%E5%8F%A3%E7%AF%87.md)
## 概览
首先列出官方md链接讲解颇为生动

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 178 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 282 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 558 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 169 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 457 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 280 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 289 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 144 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 40 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 190 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 528 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 282 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 178 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 43 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 262 KiB

Loading…
Cancel
Save