使用Python调度Kubernetes

Kubernetes Scheduling in Python

使用Python调度Kubernetes

The topic of scheduling can be quite daunting. It is a full area of research in Computer Science.

调度这个题目是让人生畏的,在计算机科学上是一个庞大的研究领域。

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.

调度一个请求就是去查看哪些在等待,然后寻找一个计划去满足这些请求。通常,需要考虑减少服务时间、最大化地利用资源。

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的核心节点的核心组件是一个调度器。

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.

为了揭开调度过程的神秘面纱,我们假设Kubernetes需要在集群中的所有节点中进行查询,过滤掉哪些不是很好适合Pods的处于挂起状态的节点,然后选择节点来执行。

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.

在更复杂的情况下,调度器将运行预测器以运行一些优先功能。根据节点的评分来运行工作负载,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

带非缺省调度器的Pod

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

使用Python的Kubernetes调度器

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调度器。

 

转载于:https://my.oschina.net/u/2306127/blog/890855

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值