博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
如何对kubernetes scheduler进行二次开发
阅读量:6345 次
发布时间:2019-06-22

本文共 3894 字,大约阅读时间需要 12 分钟。

hot3.png

更多关于kubernetes的深入文章,请看我或者的博客主页。

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

新增Predicate Policy

  • predicate Interface
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
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
plugin/pkg/scheduler/algorithmprovider/defaults/defaults.go:47func 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
/var/lib/kube-scheduler/policy.config{"kind" : "Policy","apiVersion" : "v1","predicates" : [    {"name" : "CustomPredicatePolicy"}    ],"priorities" : [    ]}

新增Priority Policy

  • Priority Interface
/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

/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
apiVersion: v1kind: Podmetadata:  name: nginx  labels:    app: nginxspec:  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

#!/bin/bashSERVER='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 1done

更多关于kubernetes的深入文章,请看我或者的博客主页。

转载于:https://my.oschina.net/jxcdwangtao/blog/898700

你可能感兴趣的文章
民航局:春运期间10个大型机场将延长国内航班运行时间
查看>>
比特币暴涨拉升至1w美元以上,说比特币崩盘的专家要失望了
查看>>
Python「八宗罪」
查看>>
你的隐私还安全吗?社交网络中浏览历史的去匿名化
查看>>
NeurIPS 2018|如何用循环关系网络解决数独类关系推理任务?
查看>>
iOS开发笔记:-ObjC所引起的那一个大坑
查看>>
singleton pattern & MVC pattern
查看>>
手把手深入理解 webpack dev middleware 原理與相關 plugins
查看>>
在现有的元素内添加新元素而不影响现有内容insertAdjacentElement/HTM/Text
查看>>
Android学习笔记2:Hello World程序解析
查看>>
JavaScript Ajax与Comet——“XMLHttpRequest2级”的注意要点
查看>>
野蛮生长的痛(一个伪前端的2015总结)
查看>>
浅谈Http验证
查看>>
HTML5 安全问题解析
查看>>
TLS False Start究竟是如何加速网站的
查看>>
使用 Chinese_pinyin + Friendly_id 为中文标题生成 Slug
查看>>
学做iOS开发:绘制线条
查看>>
[Algo] Parse XML Tree 解析XML文件
查看>>
如何用纯 CSS 创作彩虹背景文字
查看>>
ACL终身成就奖得主李生:自然语言处理研究的五点体会 ...
查看>>