使用Python調度Kubernetes

Kubernetes Scheduling in Pythonhtml

使用Python調度Kubernetes

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

帶非缺省調度器的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調度器。

相關文章
相關標籤/搜索