Deploying and Managing Kubernetes Pods Using Node Selectors

Deploying and Managing Kubernetes Pods Using Node Selectors

This document explains how to deploy a pod in a Kubernetes cluster and assign it to a specific node using a nodeSelector. Follow the step-by-step instructions to understand the workflow.


Step 1: Verify Initial Cluster Setup

  1. Check for existing pods:

     controlplane $ k get pods
    

    Output:

     No resources found in default namespace.
    

    This confirms that no pods are currently running in the default namespace.

  2. Check node details:

     controlplane $ k get nodes -o wide
    

    Example output:

     NAME           STATUS   ROLES           AGE    VERSION   INTERNAL-IP   EXTERNAL-IP   OS-IMAGE             KERNEL-VERSION      CONTAINER-RUNTIME
     controlplane   Ready    control-plane   7d3h   v1.31.0   172.30.1.2    <none>        Ubuntu 20.04.5 LTS   5.4.0-131-generic   containerd://1.7.13
     node01         Ready    <none>          7d2h   v1.31.0   172.30.2.2    <none>        Ubuntu 20.04.5 LTS   5.4.0-131-generic   containerd://1.7.13
    

Step 2: Deploy a Pod

  1. Create a Pod definition file (pod.yaml):

     controlplane $ cat pod.yaml
    

    Content of pod.yaml:

     apiVersion: v1
     kind: Pod
     metadata:
       name: web-pod
     spec:
       containers:
       - image: nginx
         name: web-pod
         ports:
          - containerPort: 80
    
  2. Apply the Pod configuration:

     controlplane $ kubectl apply -f pod.yaml
    

    Output:

     pod/web-pod created
    
  3. Verify Pod status:

     controlplane $ k get pods
    

    Example output:

     NAME      READY   STATUS    RESTARTS   AGE
     web-pod   1/1     Running   0          20s
    

Step 3: Label the Node

  1. Add a label to the controlplane node:

     controlplane $ kubectl label node controlplane node=master
    

    Output:

     node/controlplane labeled
    
  2. Describe the node to confirm the label:

     controlplane $ k describe node controlplane
    

    Example output:

     Name:               controlplane
     Roles:              control-plane
     Labels:             beta.kubernetes.io/arch=amd64
                         beta.kubernetes.io/os=linux
                         kubernetes.io/arch=amd64
                         kubernetes.io/hostname=controlplane
                         kubernetes.io/os=linux
                         node=master
    

Step 4: Assign Pod to Specific Node

  1. Update the Pod definition to include nodeSelector:

     controlplane $ cat pod.yaml
    

    Updated content of pod.yaml:

     apiVersion: v1
     kind: Pod
     metadata:
       name: web-pod
     spec:
       containers:
       - image: nginx
         name: web-pod
         ports:
          - containerPort: 80
       nodeSelector:
         node: "master"
    
  2. Delete the existing Pod:

     controlplane $ k delete pod web-pod
    

    Output:

     pod "web-pod" deleted
    
  3. Reapply the updated Pod configuration:

     controlplane $ k apply -f pod.yaml
    

    Output:

     pod/web-pod created
    
  4. Verify Pod details with node assignment:

     controlplane $ k get pod -o wide
    

    Example output:

     NAME      READY   STATUS    RESTARTS   AGE   IP            NODE           NOMINATED NODE   READINESS GATES
     web-pod   1/1     Running   0          78s   192.168.0.4   controlplane   <none>           <none>
    

    This confirms that the Pod is running on the controlplane node.


Summary

By using a nodeSelector in the Pod configuration, you can assign a Pod to a specific node in the Kubernetes cluster. This workflow ensures that workloads are deployed to the desired node based on labels.