Hey all, as I am preparing for the CNCF (Cloud Native Computing Foundation) exam, I am clearing my doubt from basics because my belief is if your basic foundations are strong you can easily achieve the hard part of any technologies.
While going through Kubernetes components, I started exploring the way how Kubernetes Scheduler really works?
So, without wasting any time let’s get started with the working procedure of Kubernetes Scheduler.
First thing First, What is scheduler?
When you are creating pods in the Kubernetes Cluster, Scheduler takes the pod and assigns it to particular nodes.
Let’s learn with a simple example,
Note** The below example is only for understanding the purpose, later I will give you more explanation of it’s working.
See, the below code,
pods = search_for_all_pods_on_cluster()
for pod in pods:
if pod.node == nil
The above code is pretty simple to understand, let me explain the code for you.
While True, means you have created the Cluster and if it’s True, then find all the pods assigned to the nodes in the cluster.
Now, suppose you have Ten pods in the cluster running on different nodes.
Pod in each node should never be nil. If, then new pods get assigned to that nodes.
Pretty simple right, but not always.
The above explanation is only for understanding the basics of Scheduler.
Now, let’s move into actual logic and why in real time it’s different from the above explanation.
Suppose you have thousands of pods running in the cluster, It is not possible to run above logics on thousands of pods as it will take time to execute and will not be reliable on a hugely complex infrastructure.
Whenever you are creating a pod, It sits in the queue first and waits for its time to come. 😀
Now Scheduler checks the pods in the queue, and verify whether the pods get assigned to any nodes or not.
The scheduler continuously takes the pods from the queue and keeps on scheduling.
When you will check the code of Scheduler in the Git repository, you will find that Kubernetes uses the function called CACHE, QUEUES, and CALLBACK.
So now instead of querying on entire pods and keep on checking its state, Kubernetes maintains the records in the cache, which significantly enhance the performance of scheduling the pods.
This is how Kubernetes Scheduler works in simple but for advanced and in-depth knowledge, I will suggest you visit this link.