peter (admin)   -  



 

Kubernetes Scheduling

We will learn how Kubernetes scheduling works and observe how the scheduler decides which node should run a pod based on available resources and cluster conditions.

The purpose of this process is to ensure that pods are placed on the most suitable node so applications run efficiently and cluster resources are used properly.

Let's get started.

Verify the Kubernetes Nodes

For this, we are already logged into both the Master node and the Worker node.

Let's first verify the nodes available in the Kubernetes cluster by retrieving the node information with extended details:

kubectl get nodes -o wide

Here you can see that the cluster currently has two nodes: one acting as the control-plane node called k8s-master and the other acting as the worker node called k8s-worker.

Both nodes show the status Ready, which means they are healthy and available to participate in the cluster.

The output also displays information such as:

  • Internal IP addresses

  • Operating system

  • Kernel version

  • Container runtime

  • Kubernetes version

  • Node roles

Create a Pod with a Node Selector

Now we create a Pod configuration using a YAML definition.

YAML allows us to clearly describe how a Pod should run and where Kubernetes should place it in the cluster.

The purpose of this example is to observe how the Kubernetes scheduler behaves when we control the node placement of a Pod.

We can create the YAML definition directly from the terminal using a here-document:

cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: Pod
metadata:
  name: schedule-demo
spec:
  nodeSelector:
    kubernetes.io/hostname: k8s-worker1
  containers:
    - name: web
      image: nginx
EOF

At the beginning of the definition, we specify:

apiVersion: v1
kind: Pod

The apiVersion identifies the Kubernetes API version used for this resource.

The kind: Pod field tells Kubernetes that we want to create a Pod.

Next, we assign the Pod a name:

metadata:
  name: schedule-demo

The name schedule-demo becomes the identifier Kubernetes uses to track and manage this Pod inside the cluster.

Configure the Node Selector

Within the Pod specification, we define a nodeSelector:

nodeSelector:
  kubernetes.io/hostname: k8s-worker1

A nodeSelector tells the Kubernetes scheduler that the Pod should only run on a node that has a matching label.

In this example, we use the built-in kubernetes.io/hostname label and specify:

k8s-worker1

This means the scheduler can only place the Pod on the k8s-worker node.

The purpose of this configuration is to demonstrate how scheduling decisions can be constrained by defining node-selection rules.

Configure the Container

After defining where the Pod should run, we specify the container that will execute inside the Pod:

containers:
  - name: web
    image: nginx

The container is named web and uses the NGINX container image.

When the configuration is submitted, Kubernetes creates the Pod and the scheduler evaluates its scheduling requirements.

Because the Pod contains a nodeSelector requiring k8s-worker, the scheduler assigns it to that node.

Verify the Pod's Node

We can verify the scheduling decision by retrieving the Pod information:

kubectl get pods -o wide

The schedule-demo Pod should be in the Running state.

For example:

NAME            READY   STATUS    RESTARTS   AGE   IP           NODE
schedule-demo   1/1     Running   0          30s   10.x.x.x     k8s-worker1

In the NODE column, you can see:

k8s-worker1

This confirms that Kubernetes placed the Pod on the Worker node according to the nodeSelector rule defined in the configuration.

Describe the Pod

If we want more detailed information about the Pod, we can use:

kubectl describe pod schedule-demo

After pressing Enter, you can see information about the Pod, including:

  • Pod status

  • Assigned node

  • Container image

  • Container state

  • Container start time

  • Node selector

  • Events

The output should contain the node selector:

Node-Selectors: kubernetes.io/hostname=k8s-worker1

This confirms that the Pod has a scheduling constraint requiring the k8s-worker node.

Check the Scheduling Events

The Events section of the kubectl describe output provides additional information about the scheduling process.

You should see an event similar to:

Successfully assigned default/schedule-demo to k8s-worker1

This verifies that the Kubernetes scheduler successfully assigned the Pod to the Worker node.

Verify the Pod from the Worker Node

Let's move on and verify this directly from the Worker node because this is where the Pod was scheduled to run.

First, retrieve the Pod sandboxes from the container runtime using:

sudo crictl pods

The crictl pods command communicates with the container runtime through the Container Runtime Interface (CRI) and displays the Pod sandboxes known to the runtime.

You should see the schedule-demo Pod in the output with a state of Ready.

For example:

POD ID          CREATED         STATE   NAME
xxxxxxxxxxxx    2 minutes ago   Ready   schedule-demo

This confirms that the Pod sandbox exists and is active on the Worker node.

Verify the Running Container

Next, retrieve the containers running on the Worker node:

sudo crictl ps

The container named web should be associated with the schedule-demo Pod.

Its state should show:

Running

This confirms that the NGINX container is successfully running on the Worker node where the Kubernetes scheduler placed the Pod.

How Kubernetes Scheduling Works

Kubernetes scheduling determines which node should run a newly created Pod.

When a Pod is created without a specific node already assigned, the Kubernetes scheduler examines the available nodes and determines which nodes are suitable.

The scheduling process can be represented as:

Pod Created
     ↓
Scheduler Detects Unscheduled Pod
     ↓
Evaluate Available Nodes
     ↓
Check Pod Requirements
     ↓
Filter Unsuitable Nodes
     ↓
Select a Suitable Node
     ↓
Assign Pod to Node
     ↓
Kubelet Starts Pod

Kubernetes can consider many factors when making scheduling decisions, including available CPU and memory, node labels, taints and tolerations, affinity and anti-affinity rules, and other scheduling constraints.

In our example, we explicitly constrained the scheduling decision using:

nodeSelector:
  kubernetes.io/hostname: k8s-worker1

Therefore, instead of allowing the scheduler to choose freely between eligible nodes, the Pod could only be scheduled onto a node matching the k8s-worker hostname label.

This demonstrates how Kubernetes scheduling can be controlled using node-selection constraints while the scheduler remains responsible for assigning the Pod to an eligible node.

 


Comments (0)

There are no comments yet. Be the first to comment.

Log in or register to leave a comment. all comments are subject to admin approval