kubernetes

Inside the Kubernetes Scheduling Framework

How kube-scheduler filters, scores, and binds pods across cluster nodes using its Go plugin architecture when workloads stay 0/1 Pending.

By Ashutosh Patole6 min read

Why This Matters

In a Kubernetes cluster, you declare what you want to run (a Pod), but someone needs to decide where it runs. The kube-scheduler is the control plane component responsible for making this critical decision. It ensures workloads are placed on nodes that have enough resources, avoiding overloading any single server while adhering to your custom placement and hardware rules.

Core Concepts

The Role of kube-scheduler

The kube-scheduler is the default scheduler for Kubernetes. It runs on the control plane and continuously watches the API server for newly created Pods that have no assigned node. Its primary job is to find the best available worker node for each unscheduled Pod based on resource requirements, hardware constraints, affinity rules, and data locality.

The Scheduler Cache and API Server

To make scheduling decisions quickly, the scheduler maintains a local scheduler cache, which is an in-memory snapshot of the cluster's state, including the capacities of all nodes and the Pods currently running on them.

Instead of querying the API server for the cluster state every single time a new Pod needs scheduling (which would create a massive performance bottleneck), the scheduler relies on this cache. It only communicates with the API server to watch for new unscheduled Pods and to announce when it has successfully assigned a Pod to a node.

The Pod Scheduling Cycle

When a developer creates a new deployment, the scheduler components step in to assign a node through a specific, ordered procedure.

Diagram
flowchart TD
    Watch["`**1. Watch for unscheduled Pods**
Detect new Pod with empty nodeName via API Server`"]
    Filter["`**2. Filter**
Eliminate nodes in cache that cannot run the Pod`"]
    Score["`**3. Score**
Rank remaining valid nodes by resources and locality`"]
    Bind["`**4. Bind**
Send Binding object for top node to API Server`"]

    Watch --> Filter --> Score --> Bind

The Filtering Phase

When you deploy an application, the Kubernetes scheduler must decide which server (worker node) runs it. The Filtering phase is the first step in this decision-making process. The primary goal of the Filtering phase is to evaluate all available nodes against your pod's specific requirements and create a shortlist of eligible nodes. It acts as a strict gateway. The scheduler looks at what the pod needs to function and checks every node to see if it can provide it.

Hard Constraints

Filtering relies exclusively on "hard constraints." This means the evaluation is completely binary - a node either passes every single check, or it fails and is removed from consideration. There is no partial credit or ranking at this stage. Common checks include verifying if the node has enough free CPU and memory (PodFitsResources) or if it matches the pod's required hardware labels (MatchNodeSelector).

Common Filter Predicates

PredicatePurpose
PodFitsResourcesEnsures the node has sufficient allocatable resources (CPU, memory, GPUs) to meet the pod's requests and limits.
MatchNodeSelectorEnforces nodeSelector or nodeAffinity rules, ensuring the pod only runs on nodes with matching labels.
TaintsTolerationChecks if the pod's tolerations align with any taints present on the node, preventing pods from being scheduled on tainted nodes unless they explicitly tolerate the taint.
PodFitsHostPortsVerifies that if a pod specifies hostPort, the target port is available on the node (or that the pod is allowed to use it despite conflicts).
NoVolumeZoneConflictFor stateful workloads, ensures that persistent volumes required by the pod are accessible from the node (e.g., not stuck in a different availability zone).

The Scoring Phase

But what happens when multiple Nodes have enough CPU and memory to host your Pod? The Scoring phase is where Kubernetes shifts from asking "Can this Node run the Pod?" to asking "Which Node is the best fit?"

Filtering vs. Scoring

Node selection happens in two main steps:

  • Filtering: This is a strict pass/fail test. If a Node lacks the required CPU, or does not match a hard nodeSelector, it is immediately eliminated.
  • Scoring: The nodes that survive the filtering phase are passed to the scoring phase. Here, the scheduler ranks the feasible Nodes by assigning them a score between 0 and 100.

How Plugins Calculate Scores

Kubernetes uses various scheduling plugins to evaluate the remaining Nodes. Each plugin grades the Nodes based on specific criteria:

  • NodeResourcesFit: Prefers Nodes that will have the most balanced resource usage after placing the Pod.
  • ImageLocality: Favors Nodes that already have the required container image pulled locally, reducing image download times.
  • InterPodAffinity: Rewards Nodes based on soft preferences (preferredDuringSchedulingIgnoredDuringExecution), such as keeping related Pods on the same Node or spreading them across different availability zones.

The scheduler multiplies each plugin's score by a configured weight, sums them up, and assigns the Pod to the Node with the highest final score.

FeatureFilteringScoring
PurposeDetermine feasibility (Can the node run the pod?)Determine preference (Which node is the best fit?)
EvaluationBinary Hard Constraints (Pass/Fail)Graded Soft Constraints (0-100 score)
ResultA list of nodes that can run the podA ranked list of nodes by desirability
PluginsPodFitsResources, MatchNodeSelector, TaintsToleration, etc.NodeResourcesFit, ImageLocality, InterPodAffinity, etc.

The Binding Phase

The scheduling process in Kubernetes isn't complete just because the scheduler has internally picked the best node. Until that decision is officially recorded and communicated, the worker node has no idea it needs to spin up new containers. The Binding Phase is this critical final handshake. Without it, your Pod would remain in a perpetual Pending state, and the cluster would never bridge the gap between the control plane's decision-making and the worker node's execution.

In the Kubernetes scheduling cycle, the Binding phase is the very last step. Once the scheduler has filtered out unsuitable nodes and scored the remaining ones, it selects the single best node for the Pod. The Binding phase is the process of applying this decision to the cluster's state.

During this phase, the scheduler creates a Binding object. This object acts as a formal declaration, linking the target Pod's unique ID to the target Node's name.

Notifying the API Server and the Kubelet

The scheduler doesn't talk directly to the worker nodes. Instead, Kubernetes relies on a declarative API architecture:

  • API Server Notification: The scheduler sends a POST request containing the Binding object to the API server (specifically targeting the Pod's binding subresource). The API server updates the Pod's specification in the etcd datastore, changing the Pod's nodeName field to match the selected node.
  • The Kubelet Takes Over: Every worker node runs an agent called the kubelet. The kubelet constantly watches the API server for Pods assigned to its specific nodeName. Once the Binding phase updates the API server, the destination node's kubelet immediately detects the change and instructs the container runtime (like containerd) to start downloading images and running the Pod's containers.
Table of contents
kubernetes

Cgroups v2 in Kubernetes: What Breaks

Upgrading to a modern OS changes how Kubernetes throttles memory and CPU. How the unified hierarchy works and what breaks moving from v1 to v2.

- 5 min read - kubernetes, cgroups, linux