kubernetes1.9源码阅读 kube-scheduler

之前写过kubernetes1.8 kube-scheduler源码阅读。基于上篇文章的基础,重新阅读了kubernetes1.9的kube-scheduler的代码。本篇文章是kubernetes1.9的kube-scheduler代码阅读笔记。不得不说,kubernetes工程师们,每经过短短三个月的版本发布,代码都会经过大大的重构。好的工程师是在不断重构代码中,认识得到升华,锤炼出来的。

kubernetes tag v.1.9.0

kube-scheduler的代码路径为:plugin/

入口程序:plugin/cmd/kube-scheduler/scheduler.go main()

kubernetes1.9源码阅读 kube-scheduler_第1张图片

1. kubernetes1.9对入口程序cmd部分进行了重构

2. 首先,调用app.NewSchedulerCommand(),  NewSchedulerCommand()方法在plugin/cmd/kube-scheduler/app/server.go中,NewSchedulerCommand()方法将调用opts.Run();

3. 创建NewSchedulerServer和启动调度服务;

4. componentconfig.KubeSchedulerConfiguration{}定义了kube-scheduler的参数信息;

kubernetes1.9源码阅读 kube-scheduler_第2张图片

构建调度服务器:plugin/cmd/kube-scheduler/app/server.go NewSchedulerServer()

1. 调用createClients(),创建apiserver客户端,通过REST方式访问APIserver提供的API服务,用来watch pod和node,并调用api server bind接口完成node和pod的Bind操作;

2. 调用record.NewBroadcaster(),创建eventBroadcaster对象,recorder记录config.SchedulerName的事件;

3. 如果设置了leaderElect,调用makeLeaderElectionConfig()进行选举;

4. 调用informers.NewSharedInformerFactory(),创建sharedInformerFactory对象

5. 调用factory.NewPodInformer(),创建PodInformer对象,PodInformer用于watch/list non-terminal pods并缓存;

kubernetes1.9源码阅读 kube-scheduler_第3张图片

启动程序:plugin/cmd/kube-scheduler/app/server.go Run()

1. 调用s.SchedulerConfig()方法,创建scheduler.Config对象;

2. 在s.SchedulerConfig()方法中,首先,通过factory.NewConfigFactory()创建ConfigFactory对象,ConfigFactory是scheduler.Configurator接口的实现;

3. ConfigFactory对象,包括:nodeInformer、podInformer、pvInformer、pvcInformer、rcInformer、rsInformer、statefulsetInformer、serviceInformer、pdbInformer、storageClassInformer,这些是通过s.InformerFactory创建;

4. 调用scheduler.NewFromConfig(schedulerConfig),创建Scheduler对象;

5. 调用s.Broadcaster.StartRecordingToSink(),接收EventBroadcaster发送的event,输出到EventSink;

6. 启动healthz server和metrics server,提供必要的健康检查和性能量度;

7. 调用informerFactory.start() ,开始运行Informer,进行缓存;

8. 调用sched.Run(),运行调度程序;

kubernetes1.9源码阅读 kube-scheduler_第4张图片

SharedInformers模式设计同时用在k8s的"Controller"中,下面是一段关于SharedInformers模式设计的英文介绍:(摘自https://github.com/kubernetes/community/blob/8decfe4/contributors/devel/controllers.md)

Use SharedInformers. SharedInformers provide hooks to receive notifications of adds, updates, and deletes for a particular resource. They also provide convenience functions for accessing shared caches and determining when a cache is primed.

SharedInformers提供勾子机制,接收到特定资源添加、更新、删除的通知。并提供函数更新缓存,启动执行。简而言之kube-scheduler的"informer"负责:watch/list non-terminal pods, 进行缓存,再从podQueue中获得NextPod,执行调度;

调度程序:plugin/pkg/scheduler/scheduler.go Run()

1. 等待缓存更新完成;

2. 调用sched.scheduleOne,运行调度流程;

kubernetes1.9源码阅读 kube-scheduler_第5张图片

调度流程:plugin/pkg/scheduler/scheduler.go scheduleOne()

1. 调用sched.config.NextPod(),从podQueue缓存中获得一个Pod;

2. 调用sched.schedule(pod),获得一个suggestedHost,此操作是同步操作;

3. 调用metrics.SchedulingAlgorithmLatency.Observe(metrics.SinceInMicroseconds(start)),记录算法延迟的量度;

4. 此Pod将标注为assumedPod;此时Pod并没有被成功调度;

5. 调用sched.bind(),绑定Pod到suggestedHost,此操作是异步操作;

6. 调用metrics.E2eSchedulingLatency.Observe(),记录调度延迟的量度;

kubernetes1.9源码阅读 kube-scheduler_第6张图片

调度逻辑:plugin/pkg/scheduler/scheduler.go Schedule()

1. 根据调度策略算法确定一个suggestedHost;

调度算法函数:

1. 调度算法函数支持plugin模式plugin/pkg/scheduler/algorithmprovider/plugins.go,scheduler的commandLine参数AlgorithmProvider可以指定调度算法函数;默认使用defaultProvider, defaultPredicates(), defaultPriorities() plugin/pkg/scheduler/algorithmprovider/defaults/default.go;同时,scheduler的commandLine参数PolicyConfigFile,可以加载自定义的调度策略文件。如:openshift中,/etc/origin/master/scheduler.json定义了调度策略文件。(参考信息:https://docs.openshift.com/container-platform/3.6/admin_guide/scheduling/scheduler.html)

2. FitPredicates:k8s.io/kubernetes/plugin/pkg/scheduler/algorithm/predicates

3. PrioritiesFunc:k8s.io/kubernetes/plugin/pkg/scheduler/algorithm/priorities

4. 调度流程plugin/pkg/scheduler/core/generic_scheduler.go schedule(),调度流程图如下:(摘自kubernetes调度详解:http://dockone.io/article/2885)

kubernetes1.9源码阅读 kube-scheduler_第7张图片

绑定逻辑:plugin/pkg/scheduler/scheduler.go bind()

1. 调用SchedulerCache.FinishBinding(),更新schedulerCache,将assumedPod的状态变为expired;  schedulerCache实现了schedulercache.cache接口;

2. schedulerCache定义在plugin/pkg/scheduler/schedulercache/cache.go;

3. schedulercache.cache定义在plugin/pkg/scheduler/schedulercache/interface.go;

4. 调用metrics.BindingLatency.Observe(),记录绑定延迟的量度;

5. 调用 sched.config.Recorder.Eventf(),记录绑定事件;

kubernetes1.9源码阅读 kube-scheduler_第8张图片

相关阅读:

1. http://licyhust.com/

你可能感兴趣的:(kubernetes1.9源码阅读 kube-scheduler)