Kubernetes Scheduling in Pythonhtml
The topic of scheduling can be quite daunting. It is a full area of research in Computer Science.node
调度这个题目是让人生畏的,在计算机科学上是一个庞大的研究领域。python
At the end of the day scheduling requests is about looking at who is waiting and finding a plan to serve those requests. Generally you want to reduce the time to serve, and use the most appropriate resources to do so.git
调度一个请求就是去查看哪些在等待,而后寻找一个计划去知足这些请求。一般,须要考虑减小服务时间、最大化地利用资源。github
Kubernetes is no different. Kubernetes schedules Pods (groups of co-located containers) onto nodes of your cluster. That means that one of the core components of the Kubernetes head node is a scheduler.算法
Kubernetes没有什么不一样。Kubernetes调度Pods到集群中的节点上运行。这意味着Kubernetes的核心节点的核心组件是一个调度器。api
To demystify further the scheduling process, we can approximate it as Kubernetes needing to look at all the nodes that are in the cluster, filter out the ones that are not a good match to run Pods that are in Pending state, and then pick one.promise
为了揭开调度过程的神秘面纱,咱们假设Kubernetes须要在集群中的全部节点中进行查询,过滤掉哪些不是很好适合Pods的处于挂起状态的节点,而后选择节点来执行。app
In more complex terms, the scheduler will run a set of predicates and then run some some priority functions. The end result is a list of ranked nodes that can run the workload (i.e the Pod), the Pod ends up on the node with the highest rank.dom
在更复杂的状况下,调度器将运行预测器以运行一些优先功能。根据节点的评分来运行工做负载,Pod将运行与最高评分的节点之上。
This filtering and ranking is nothing new, and can be found in 30 years old batch processing systems.
这些过滤和评分机制不是什么新鲜的东西,在30年前的批处理系统上就已经在使用。
A Pod with a non-default scheduler
Let’s dig deeper and start a Pod that specifies a non-default scheduler. This can be done in a Pod manifest using schedulerName
in the Pod Spec. In the gist below we specify a scheduler named foobar
. Once you create this Pod, since Kubernetes does not know this scheduler, the Pod will remain in Pending state and never get scheduled until, we somehow assign a node to this Pod.
下面进一步深刻。启动一个Pod指定为非缺省调度器。在Pod的manifest文件指定,在Pod Spec中使用schedulerName
属性。在下面咱们指定了调度器foobar
。一旦你建立了这个Pod,由于Kubernetes不知道这个调度器,这个Pod将处于挂起状态而且永远不被调度,直到咱们赋给一个节点给这个Pod。
A Kubernetes scheduler in Python
While we can run some very advanced functions to determine the list of available nodes who can serve a Pod request, and then run some equally advanced functions to rank nodes that can serve said Pod request, we can also pick a node at random. Random selection is not as bad as it sounds and in some cases can be a very good compromise since it has very little computational cost.
咱们将运行一些更高级的算法函数去肯定可用的节点,可以对Pod请求提供服务。而后运行一些高级函数对能提供Pod请求服务的节点进行评分,也能够随机地选择一个节点。随机选择并无听起来那么糟糕,在某些状况下换时很是好的妥协,由于这种方式的计算成本很小。
To implement a scheduler, we run a watch
on the Pods endpoint. Every time there is a change in the set of Pods in the system, we get a notification. When a Pod is in Pending state and has specified our scheduler name, we call our random scheduler.
为了实现调度器,咱们在 Pods endpoint运行 watch
。系统中每一次Pods发生改变,咱们都能获得通知。当Pod处于挂起状态而且被指定咱们的调度器名称时,咱们调用随机调度器。
To do random scheduling, we just build a list of available nodes in our system and just pick one at random.
实现随机调度器,咱们只需建立一个系统中可用节点的列表,而且从中随机地选择一个。
To schedule the Pod on our random node, we create a Binding
object. This is basically a POST
that attaches an object which describes the target node.
为了调度Pod在随机的节点上,咱们建立一个 Binding
对象。将 POST
关联到描述目标节点的对象上。
Thanks to Ian Lewis for the tip and a more advanced blog.
感谢Ian Lewis 的建议,更多参阅:advanced blog.
The gist below use the Kubernetes Python client to implement this simple scheduling.
Kubernetes的Python Client实现参考: simple scheduling.
Now, stick this Python script in a container, run it in a Pod and you have a custom scheduler for Kubernetes.
如今Python脚本放到container中,在Pod中运行。恭喜!你如今有了一个自定义的Kubernetes调度器。