Janam Writes

How a Pod Schedules in Kubernetes

Explore the intricate dance of Kubernetes pod scheduling and its policies in this insightful post, unraveling the behind-the-scenes orchestration of containers. Dive into the world of resource limits, affinity, and spread constraints to ensure your orchestra performs harmoniously on the Kubernetes stage.

Learning about pod scheduling and scheduling policies in Kubernetes is like mastering the art of orchestrating a symphony of containers. It's essential for ensuring that each "musician" (pod) gets the right stage (node) with the necessary resources to perform at its best. With this knowledge, you can balance performance, availability, and cost, allowing your "orchestra" (Kubernetes cluster) to play harmoniously and troubleshoot issues when a note goes off-key.

Here is an overview of how the Kubernetes scheduler works:

The Scheduling Process

Pod scheduling is a critical process in Kubernetes that determines where pods will run in a cluster. When a new pod is created, such as through a deployment rollout or standalone YAML definition, the scheduling process kicks off behind the scenes. Let's walk through the key steps:

  1. Submission of Pod YAML Definition: The pod YAML definition is submitted to the API server, which validates the request and persists the desired state in etcd. The new pod is added to the scheduler's queue.

  2. Evaluation by kube-scheduler: The kube-scheduler evaluates the pod's resource requirements, constraints, and other factors to select the optimal node for it to run on. This decision involves filter and predicate checks.

  3. Binding to Node: Once a node is chosen, the pod is bound to that node and marked as scheduled. This information is persisted in etcd. Up to this point, everything happens in the control plane.

  4. Pod Creation by kubelet: The kubelet agent on the target node gets the scheduling orders and handles the actual pod creation. It leverages the Container Runtime Interface (CRI), the Container Networking Interface (CNI), and the Container Storage Interface (CSI) to set up the pod's containers, networking, and storage.

  5. Networking Setup: With the CNI's help, the new pod gets a routable IP address assigned to it and gets connected to the cluster network.

  6. Status Reporting: The kubelet reports comprehensive status back to the API server, completing the control loop. Etcd is updated with the final pod details like IP address.

  7. Running State: The pod transitions to a Running state, ready to serve requests. Users can now locate and connect to the pod via its IP.

Understanding these scheduling stages is key to debugging issues and optimizing workload allocation across nodes. This coordination between the control plane and nodes brings pods to life in Kubernetes!

Scheduling Policies

In the world of Kubernetes scheduling policies, think of it as a matchmaking dance where each pod is looking for its perfect partner in the form of a node to run on.

  • Resource limits: These act like compatibility checks, ensuring that a node has the right "dance floor" size (CPU, memory, storage) for the pod's requirements. Filter out nodes that do not meet the pod's CPU, memory, or storage resource requests.

  • Taints and Tolerations: These are like personal boundaries, allowing pods to say "yes" or "no" to specific nodes based on certain attributes or conditions. Taints allow nodes to repel pods that don't tolerate them. Pods can tolerate taints to schedule on tainted nodes.

  • Node affinity: This is akin to choosing the right dance partner by name or shared interests (labels), ensuring the pod lands on a node with compatible characteristics. Pods can specify affinity for certain nodes, either by name or other labels. This ensures pods land on the right hardware.

  • Pod affinity/anti-affinity: These policies are all about finding or avoiding dancing buddies among other pods, depending on the situation or requirements. Allows pods to group together or avoid other pods if needed.

  • Node selectors: Think of these as specifying your dance venue preferences, ensuring pods are scheduled on nodes with specific labels that match their needs. Pods can specify to be scheduled on nodes matching specific labels.

  • Spread Constraints: This is the choreography that prevents too many pods from crowding onto a single node, ensuring a balanced distribution. Pods can be distributed to avoid too many on one node.

  • Pod priority: This is the final touch, affecting scheduling decisions based on the priority of each pod and resource availability.

All rights reserved. Janam Khatiwada