如何更改 kubernetes 中的默认 kube-scheduler

How to change default kube-scheduler in kubernetes

This doc 讲述了如何 运行 多个调度程序。但我无法理解默认调度程序是如何决定的?是否基于 --leader-elect 选项?

我可以告诉 Kubernetes 使用 my-custom-scheduler 作为默认调度程序而不是 kube-scheduler 吗? 除了 Pod/Deployment 规范

中的 schedulerName 之外,还有其他方法可以指定调度程序吗?

How does default scheduler is decided? Is it based on --leader-elect option?

不,它不是基于 --leader-elect,后者是 运行 同一个调度程序的多个复制副本,启用了领导者选举,因此在任何给定时间点只有一个副本充当领导者。

Can I tell Kubernetes to use my-custom-scheduler as default scheduler instead of kube-scheduler?

您不需要在 kubernetes 集群级别更改默认调度程序,因为您可以告诉 kubernetes 在 pod 规范中使用您的自定义调度程序。下面的示例使用 my-scheduler 而不是 default-scheduler

apiVersion: v1
kind: Pod
metadata:
  name: pod-with-custom-scheduler
spec:
  schedulerName: my-scheduler
  containers:
  - name: pod-with-custom-scheduler
    image: k8s.gcr.io/pause:2.0

以上 pod 将按 my-scheduler 而非默认 kube-scheduler 安排。如果省略 schedulerName 则默认安排 kube-scheduler

来自doc

By default, one profile with the scheduler name default-scheduler is created. This profile includes the default plugins described above. When declaring more than one profile, a unique scheduler name for each of them is required.

If a Pod doesn't specify a scheduler name, kube-apiserver will set it to default-scheduler. Therefore, a profile with this scheduler name should exist to get those pods scheduled

所以您可以将现有的 kube 调度程序替换为名称为 default-scheduler 的调度程序。

  1. 将 kube-scheduler 的 docker 图片替换为您在 /etc/kubernetes/manifests/kube-scheduler.yaml
  2. 中的图片
  3. 编辑 kube 调度程序部署并更改映像

首先确保配置自定义调度程序并确保禁用 --leader-elect=false 。并且您可以在规范部分的 pod 中像这样使用它

spec: 
  containers:
  - image: nginx
    name: nginx
  schedulerName: your-scheduler-name

我还需要用自定义的替换默认的 Kubernetes 调度程序。这是我的做法。

我认为这是答案的主要部分。我移动(或删除)了文件 etc/kubernetes/manifests/kube-scheduler.yaml。这会禁用(或删除)默认的 Kubernetes 调度程序。此外,您可以在删除文件之前和之后通过 运行ning kubectl get po -n kube-system | grep -i scheduler 检查它是否被删除。

现在禁用了默认调度程序,我有一个自定义调度程序(python 脚本)来进行调度。我只是运行而已。这是下面的脚本。它不是很干净,但它应该可以工作。您可以根据需要调整它。 请注意,我在清理脚本后没有尝试 运行ning 脚本。可能存在小错误。

#!/usr/bin/env python

import time
import random
import json

from kubernetes import client, config, watch

config.load_kube_config()
v1 = client.CoreV1Api()

def get_request_time(hostname):
    # You can do some magic here.
    print("returning 1.2")
    return 1.2

def best_request_time(nodes):
    if not nodes:
        return []
    node_times = [get_request_time(hostname) for hostname in nodes]
    best_node = nodes[node_times.index(min(node_times)) + 1]
    print("Best node: " + best_node)
    return best_node


def nodes_available():
    ready_nodes = []
    for n in v1.list_node().items:
            # This loops over the nodes available. n is the node. We are trying to schedule the pod on one of those nodes.
            for status in n.status.conditions:
                if status.status == "True" and status.type == "Ready":
                    ready_nodes.append(n.metadata.name)
    return ready_nodes


def scheduler(name, node, namespace="<YOUR-NAMESPACE-HERE"): # You can use "default" as a namespace.
    target=client.V1ObjectReference()
    target.kind="Node"
    target.apiVersion="v1"
    target.name= node
    meta=client.V1ObjectMeta()
    meta.name=name
    body=client.V1Binding(target=target)
    body.metadata=meta
    return v1.create_namespaced_binding(namespace, body, _preload_content=False)

def main():
    w = watch.Watch()
    for event in w.stream(v1.list_namespaced_pod, "<YOUR-NAMESPACE-HERE>"):
        # We get an "event" whenever a pod needs to be scheduled
        if event['object'].status.phase == "Pending": # and event['object'].spec.scheduler_name == scheduler_name:
            try:
                arg2 = best_request_time(nodes_available())
                print("Scheduling " + event['object'].metadata.name)
                res = scheduler(event['object'].metadata.name, arg2)
            except client.rest.ApiException as e:
                print("exception")
                print(json.loads(e.body)['message'])

if __name__ == '__main__':
    main()