Kubernetes Scheduler Extender淺析

https://kubernetes.io/docs/concepts/extend-kubernetes/extend-cluster/#scheduler-extensions

Scheduler 組件可以視爲一種監視 watche 和將 Pod 分配 assign 到 Node 的特殊類型控制器 controller。在 Kubernetes 裏,默認的 Scheduler 完全可以被替代,又或者增加多個 Scheduler 來同時或者一起調度 Pod,這在 Kuberntes 裏稱爲 Mulitple Scheduler。

同時 Scheduler 也提供了 webhook 的機制,來讓用戶通過編寫自己定義的 Shedueler Exetension 來爲調度 Pod 去過濾 filter 和優化 prioritize 節點。

關於 Scheduler Extender 看下面官方文檔就夠了,這裏補充一些。

https://github.com/kubernetes/community/blob/master/contributors/design-proposals/scheduling/scheduler_extender.md

Scheduler Extender 實際上是一個額外的調度進程,用來 Filter 和 Prioritize 節點的。所以順理成章的,用戶需要實現自己的 Filter 和 Prioritize 方法。另外 Extender 也可以實現 Bind 方法,來實現將 Pod Bind 到 Node 上的操作。

// Holds the parameters used to communicate with the extender. If a verb is unspecified/empty,
// it is assumed that the extender chose not to provide that extension.
type ExtenderConfig struct {
	// URLPrefix at which the extender is available
	URLPrefix string `json:"urlPrefix"`
	// Verb for the filter call, empty if not supported. This verb is appended to the URLPrefix when issuing the filter call to extender.
	FilterVerb string `json:"filterVerb,omitempty"`
	// Verb for the prioritize call, empty if not supported. This verb is appended to the URLPrefix when issuing the prioritize call to extender.
	PrioritizeVerb string `json:"prioritizeVerb,omitempty"`
	// Verb for the bind call, empty if not supported. This verb is appended to the URLPrefix when issuing the bind call to extender.
	// If this method is implemented by the extender, it is the extender's responsibility to bind the pod to apiserver.
	BindVerb string `json:"bindVerb,omitempty"`
	// The numeric multiplier for the node scores that the prioritize call generates.
	// The weight should be a positive integer
	Weight int `json:"weight,omitempty"`
	// EnableHttps specifies whether https should be used to communicate with the extender
	EnableHttps bool `json:"enableHttps,omitempty"`
	// TLSConfig specifies the transport layer security config
	TLSConfig *client.TLSClientConfig `json:"tlsConfig,omitempty"`
	// HTTPTimeout specifies the timeout duration for a call to the extender. Filter timeout fails the scheduling of the pod. Prioritize
	// timeout is ignored, k8s/other extenders priorities are used to select the node.
	HTTPTimeout time.Duration `json:"httpTimeout,omitempty"`
}

可以見到,用戶需要提供一個 sheduler policy 的配置文件,其中包括了 Extender 的前綴,Filter 和 Priorize 和 Bind 等信息。

下面是一個配置文件的 json 例子。已知 predicatespriorities 兩個字段的值皆爲默認值,關注一下 extenders 字段的值,只有在這裏配置了,Kubernetes 在調度 Pod 的時候纔會過這裏這一層。

{
  "predicates": [
    {
      "name": "HostName"
    },
    {
      "name": "MatchNodeSelector"
    },
    {
      "name": "PodFitsResources"
    }
  ],
  "priorities": [
    {
      "name": "LeastRequestedPriority",
      "weight": 1
    }
  ],
  "extenders": [
    {
      "urlPrefix": "http://127.0.0.1:12345/api/scheduler",
      "filterVerb": "filter",
      "enableHttps": false
    }
  ]
}

我們熟知的阿里開源的 GPU 調度插件也是基於這個機制來實現 GPU 資源的調度 gpu-share-extender 的。 關於 Scheduler Extender 的實現,其實用什麼語言都能做。關鍵是要實現到 filter 和 prioritize 的功能。 這是一個完全用 Bash 腳本完成的 Extender rothgar/bashScheduler

關於如何編寫一個一個 Scheduler Exetender,我建議直接參考 IBM 博客的一篇文章 Create a custom Kubernetes scheduler

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章