k8s的發展越來越像是一個框架,然后把各種擴展的能力留給開發者。開發者可以基于這些接口結合自己的業務場景,實現自己的場景化需求。其中kube scheduler 就是充分體現了這個特質,關于kube scheduler 本身的介紹參加之前的文章,今天我想介紹如何給scheduler 添加一個調度plugin。
我們首先通過yaml定義這個plugin
- apiVersion: kubescheduler.config.k8s.io/v1beta1
- kind: KubeSchedulerConfiguration
- clientConnection:
- kubeconfig: "/etc/kubernetes/scheduler.conf"
- profiles:
- - schedulerName: default-scheduler
- plugins:
- score:
- enabled:
- - name: HelloWorldPlugin
- disabled:
- - name: "*"
- pluginConfig:
- - name: HelloWorldPlugin
- args:
- xxx: "xxx"
- yyy: "123"
- zzz: 3
我們定義了一個 HelloWorldPlugin 的插件,并且定義了這個插件的啟動參數。然后需要修改kube scheduler啟動參數通過 --config 指定上面的配置文件。
接下來我們就需要實現這個插件,scheduler是通過每個插件的打分的方式確定調度的主機。所以我們需要實現一個打分的接口
- type ScorePlugin interface {
- Plugin
- // 打分
- Score(ctx context.Context, state *CycleState, p *v1.Pod, nodeName string) (int64, *Status)
- ScoreExtensions() ScoreExtensions
- }
- type ScoreExtensions interface {
- // 打分歸一化,保證每個插件的公平性
- NormalizeScore(ctx context.Context, state *CycleState, p *v1.Pod, scores NodeScoreList) *Status
- }
我們根據自己的業務需求實現這個接口,譬如下面這個例子,基于主機網絡帶寬的調度:首先通過promethues獲取主機的網絡流量,打分依據網絡流量大小。
- func (n *HelloWorldPlugin) Score(ctx context.Context, state *framework.CycleState, p *v1.Pod, nodeName string) (int64, *framework.Status) {
- nodeBandwidth, err := n.prometheus.GetNodeBandwidthMeasure(nodeName)
- if err != nil {
- return 0, framework.NewStatus(framework.Error, fmt.Sprintf("error getting node bandwidth measure: %s", err))
- }
- klog.Infof("[NetworkTraffic] node '%s' bandwidth: %s", nodeName, nodeBandwidth.Value)
- return int64(nodeBandwidth.Value), nil
- }
我們希望網絡流量越大,得分越少,于是在歸一化處理的時候,我們通過下面簡單公式轉化成最終的分數。
- func (n *HelloWorldPlugin) NormalizeScore(ctx context.Context, state *framework.CycleState, pod *v1.Pod, scores framework.NodeScoreList) *framework.Status {
- for i, node := range scores {
- scores[i].Score = framework.MaxNodeScore - (node.Score * framework.MaxNodeScore / higherScore)
- }
- klog.Infof("[NetworkTraffic] Nodes final score: %v", scores)
- return nil
- }
這樣一個簡單的,基于網絡流量調度的插件就實現了。
原文鏈接:https://www.toutiao.com/i7046924965886591502/