Custom Scheduler Development

The Kubernetes scheduler can be highly configured with node selectors, node affinity, pod affinity, taints, and toleration rules. In the case of custom scheduling requirements, it is also possible to develop and deploy custom schedulers in a
Kubernetes cluster. Kubernetes supports running multiple schedulers out-of-the-box. A custom scheduler in Kubernetes can be developed with any programming language. However, since it will interact extensively with the Kubernetes API, it is
customary to use a programming language that has a Kubernetes client library:

The basic workflow of the scheduler can be divided into three main consecutive stages. The scheduler waits for the pods with the specific scheduler name and no node assignment. When such a pod is found, the scheduler runs its custom algorithms to find a suitable node. Finally, the scheduler creates a binding, which is a built-in subresource of a pod in Kubernetes.

A custom scheduler in Go is implemented in the k8s-scheduler-example/main. go file and the basic workflow of Wait, Find a suitable node, and the Create pod binding stages are combined together in the following code snippet:

 for {
// Request pods from all namespaces
pods, err := clientset.CoreV1().Pods(v1.NamespaceAll).List(metav1.ListOptions{})
...
// Check for pods
for _, pod := range pods.Items {
// If scheduler name is set and node is not assigned
if pod.Spec.SchedulerName == *schedulerName && pod.Spec.
NodeName == "" {
// Schedule the pod to a random node
err := schedule(pod.Name, randomNode(), pod.Namespace)
...
}
}
...
}

The schedule function in the following code snippet is provided to create a binding between the pod and a node. The Bind method is called under the pod in clientset in the last line of the function since it is a subresource of a pod:

func schedule(pod, node, namespace string) error {
fmt.Printf("Assigning %s/%s to %s ", namespace, pod, node)
// Create a binding with pod and node
binding := v1.Binding{
ObjectMeta: metav1.ObjectMeta{
Name: pod,
},
Target: v1.ObjectReference{
Kind: "Node",
APIVersion: "v1",
Name: node,
}}
return clientset.CoreV1().Pods(namespace).Bind(&binding)
}

This custom scheduler randomly assigns nodes to the pods with the custom scheduler named packt-scheduler. The build files and documentation are provided under the k8s-scheduler-example folder, and are ready to be deployed to the cluster. In the following section, the deployment and use of multiple schedulers in a Kubernetes cluster will be presented.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.135.191.86