DevOps Pro

Let's start becoming Pro in DevOps

Custom Scheduler in Kubernetes

Custom Scheduler in K8S

Kubernetes comes with a default scheduler i.e Kube-scheduler. The scheduler plays a key role to choose the right node for each pod by taking care of all the conditions such as – resource requirements, applied taints and toleration, node affinity, and node selectors in the pod’s configuration.In this article, we are going to understand the concept of scheduling, the use of multiple schedulers and will write the code to create our own custom scheduler and will use that scheduler to schedule a pod. Let’s start with our first concept i.e Scheduling Strategy.

Scheduling Strategy

Let’s understand this concept by imagining a scenario. We have a user named Jack and he wants a pod with x number of CPU and x number of memory to run the application smoothly. He runs the kubectl command to create the pod. This request will first go to the kube-apiserver, and the kube-apiserver will first check the pod’s resource and all other requirements and then will create a pod for him. The pod will be in the pending state due to no node assigned to it yet.

Now, Kube-scheduler comes into the picture and looks for the newly created pods that are unscheduled yet. As soon as it gets a pod, first of all, it starts selecting the right node for it considering its resource requirements and other requirements in 2 steps.

Step 1:- Filtering the nodes

In this step, Kube-scheduler first filters out all the nodes that are suitable according to the pod’s requirements and makes a list. If the list is empty it means that no node is capable of scheduling that pod and in such condition, the pod remains in a pending and unscheduled state. But if the list is not empty it means there are some nodes available that are capable enough to schedule the pods.

Step 2:- Scoring the nodes

In this step, Kube-scheduler assigns a score to each node present in the list to choose the most suitable node for the pod and the pod gets scheduled in the node with the highest score. If there is more than one node with equal scores, Kube-scheduler selects one of these at random.

Custom Scheduler

We know that Kubernetes comes with kube-scheduler as a default scheduler. But if the default scheduler does not suit our needs we can implement our own scheduler and can
even run multiple schedulers simultaneously alongside the default scheduler and instruct Kubernetes what scheduler to use for each of our pods. Let’s learn how to implement a custom scheduler in Kubernetes.

As we know, if we set up a Kubernetes cluster with the kubeadm tool then all the control plane components are run as a static pod and their YAML files are stored at /etc/kubernetes/manifests/ path.
To implement a custom scheduler we have two options –
We can download the Kube-scheduler binary.
We can use the same static pod YAML file to create a custom scheduler.
In this article, we will implement our custom scheduler with the second option.

Steps to Implement Custom Scheduler

Below 10 steps will help you to create a custom scheduler as a pod and to use that scheduler to schedule an Nginx pod. Let’s copy the Kube-scheduler.yaml located under the /etc/kubernetes/manifests/ directory to my-scheduler.yaml file outside the /etc/kubernetes/manifests/ directory and update the my-scheduler.yaml file as mentioned in the below steps.

1. Change the name under the labels section to my-scheduler and we will create this pod under the default namespace.

2. Under command field update –leader-elect=true to –leader-elect=false. Because we only have a single master node in our cluster. If we had multiple master nodes, we would choose –leader-elect=true.

3. For our custom scheduler we have to find an available port to bind to the pod. In my case, no service is running on the 10282 port. You can check the free port number by running the below command.

netstat -natulp | grep 10282

4. Now set –scheduler-name property and assign the name you want to give to your custom scheduler. I am using my-scheduler as a name so it will look like –

5. Leave other things as it is and change the port number in the livenessProbe section.
port: 10282.

6. The final YAML file will look like the below code.

7. Now, save the my-scheduler.yaml file and run the below command to create the pod.

kubectl create -f my-scheduler.yaml

8. It will create a custom scheduler pod in the default namespace. You can check via the command below.

kubectl get pods

9. Let’s create a pod YAML file and instruct Kubernetes to use my-scheduler as a scheduler for this pod by using the schedulerName option in the file. YAML file will look like this –

10. Now, we can check whether my-scheduler as a scheduler is used or not by looking into the event section of this pod. Use the below command and check the events of this pod.

kubectl describe pod mypod

You will see the output of the Event section like this –

Events:
Type        Reason      Age       From                Message
------    ---------   ------     -------            ------------
Normal    Scheduled    16m      my-scheduler        Successfully assigned mypod to minikube
Normal    Pulling      16m      kubelet, minikube   Pulling image "nginx"
Normal    Pulled       16m      kubelet, minikube   Successfully pulled image "nginx"
Normal    Created      16m      kubelet, minikube   Created container nginx
Normal    Started      16m      kubelet, minikube   Started container nginx

Congratulations!! We have successfully created the custom scheduler and we have also tried to schedule a pod with that custom scheduler.

Conclusion

This article covered how the scheduler works and how he chooses the right node for a pod. Then we covered what a custom scheduler is and how we can create our own custom scheduler step by step and finally we covered how to use a custom scheduler to schedule a pod by that scheduler. I hope you have learned a lot by reading this article. Thank You 🙂

Check out my other blogs to continue learning.

Custom Scheduler in Kubernetes

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top