前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >如何对kubernetes scheduler进行二次开发

如何对kubernetes scheduler进行二次开发

作者头像
Walton
发布2018-04-16 11:39:54
2.1K0
发布2018-04-16 11:39:54
举报
文章被收录于专栏:Kubernetes

通过新增Predicates&Priorities Policies来扩展default scheduler

新增Predicate Policy

  • predicate Interface
代码语言:javascript
复制
plugin/pkg/scheduler/algorithm/types.go:31

// FitPredicate is a function that indicates if a pod fits into an existing node.
// The failure information is given by the error.

type FitPredicate func(pod *v1.Pod, meta interface{}, nodeInfo *schedulercache.NodeInfo) (bool, []PredicateFailureReason, error)
  • Implement a predicate func
代码语言:javascript
复制
func PodFitsHostNew(pod *v1.Pod, meta interface{}, nodeInfo *schedulercache.NodeInfo) (bool, []algorithm.PredicateFailureReason, error) {
	if len(pod.Spec.NodeName) == 0 {
		return true, nil, nil
	}
	node := nodeInfo.Node()
	if node == nil {
		return false, nil, fmt.Errorf("node not found")
	}
	if pod.Spec.NodeName == node.Name {
		return true, nil, nil
	}
	return false, []algorithm.PredicateFailureReason{ErrPodNotMatchHostName}, nil
}
  • register the custom predicate policy with a custom name
代码语言:javascript
复制
plugin/pkg/scheduler/algorithmprovider/defaults/defaults.go:47

func init() {
	...
	
	factory.RegisterAlgorithmProvider(factory.DefaultProvider, defaultPredicates(), defaultPriorities())
	// Cluster autoscaler friendly scheduling algorithm.
	factory.RegisterAlgorithmProvider(ClusterAutoscalerProvider, defaultPredicates(),
		copyAndReplace(defaultPriorities(), "LeastRequestedPriority", "MostRequestedPriority"))
	...
	
	factory.RegisterFitPredicate("CustomPredicatePolicy", predicates.PodFitsHostNew)
	
	...
}	
  • rebuild kube-scheduler and restart with flag of --policy-config-file

kube-scheduler xxxx --policy-config-file=/var/lib/kube-scheduler/policy.config

  • the content of --policy-config-file specified file
代码语言:javascript
复制
/var/lib/kube-scheduler/policy.config

{
"kind" : "Policy",
"apiVersion" : "v1",
"predicates" : [
    {"name" : "CustomPredicatePolicy"}
    ],
"priorities" : [
    ]
}

新增Priority Policy

  • Priority Interface
代码语言:javascript
复制
/Users/garnett/workspace/go/src/k8s.io/kubernetes/plugin/pkg/scheduler/algorithm/types.go

// PriorityMapFunction is a function that computes per-node results for a given node.

type PriorityMapFunction func(pod *v1.Pod, meta interface{}, nodeInfo *schedulercache.NodeInfo) (schedulerapi.HostPriority, error)
  • Implement a predicate func
  • register the custom predicate policy with a custom name
  • rebuild kube-scheduler and restart with flag of --policy-config-file
  • the content of --policy-config-file specified file
代码语言:javascript
复制
/var/lib/kube-scheduler/policy.config

{
"kind" : "Policy",
"apiVersion" : "v1",
"predicates" : [
    ],
"priorities" : [
    {"name" : "CumtomPriorityPolicy", "weight" : 1}
    ]
}

新增custom scheduler,pod指定scheduler-name进行调度

  • A custom scheduler can be written in any language and can be as simple or complex as you need.
  • Specify the “scheduleName” in pod.spec
代码语言:javascript
复制
apiVersion: v1
kind: Pod
metadata:
  name: nginx
  labels:
    app: nginx
spec:
  schedulerName: my-scheduler
  containers:
  - name: nginx
    image: nginx:1.10

Here is a very simple example of a custom scheduler written in Bash that assigns a node randomly. Note that you need to run this along with kubectl proxy for it to work.

kubectl proxy --port=8001

代码语言:javascript
复制
#!/bin/bash
SERVER='localhost:8001'
while true;
do
    for PODNAME in $(kubectl --server $SERVER get pods -o json | jq '.items[] | select(.spec.schedulerName == "my-scheduler") | select(.spec.nodeName == null) | .metadata.name' | tr -d '"')
;
    do
        NODES=($(kubectl --server $SERVER get nodes -o json | jq '.items[].metadata.name' | tr -d '"'))
        NUMNODES=${#NODES[@]}
        CHOSEN=${NODES[$[ $RANDOM % $NUMNODES ]]}
        curl --header "Content-Type:application/json" --request POST --data '{"apiVersion":"v1", "kind": "Binding", "metadata": {"name": "'$PODNAME'"}, "target": {"apiVersion": "v1", "kind"
: "Node", "name": "'$CHOSEN'"}}' http://$SERVER/api/v1/namespaces/default/pods/$PODNAME/binding/
        echo "Assigned $PODNAME to $CHOSEN"
    done
    sleep 1
done
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 通过新增Predicates&Priorities Policies来扩展default scheduler
    • 新增Predicate Policy
      • 新增Priority Policy
      • 新增custom scheduler,pod指定scheduler-name进行调度
      相关产品与服务
      容器服务
      腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档