How to Bind Labels to Specific Namespaces in Kubernetes and Deploy Pods

How to Bind Labels to Specific Namespaces in Kubernetes and Deploy Pods

Kubernetes allows the labeling of nodes and binding them to specific namespaces to control where the pods get scheduled. This guide explains how to create namespaces, label nodes, and bind these labels to specific namespaces to schedule pods effectively.

Prerequisites

  1. You need to have a running Kubernetes cluster.

  2. Access to kubectl and the necessary permissions to create resources and labels.


Step 1: Label the Nodes

To schedule pods on specific nodes, we first need to label those nodes. For example, if we want to label a node as worker, you can do this with the following command:

controlplane $ kubectl label node <node-name> node=worker

Replace <node-name> with the actual name of your node.

Check the Labels of Your Nodes

After labeling the node, you can check the node’s labels using:

controlplane $ kubectl describe nodes <node-name>

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

To remove a label from the node, you can use:

controlplane $ kubectl label node <node-name> node-

Step 2: Create a Namespace

Namespaces in Kubernetes help you organize resources within a cluster. Let’s create a namespace for the dev-team:

controlplane $ kubectl create namespace dev-team

Verify the namespace creation:

controlplane $ kubectl get namespaces

You should see dev-team listed as an active namespace.


Step 3: Run a Pod in the Specific Namespace

Now, let’s create a pod in the dev-team namespace. You can do this using the following command:

controlplane $ kubectl run -n dev-team web-pod --image=nginx

This command creates a pod named web-pod using the nginx image within the dev-team namespace.

Verify the pod status:

controlplane $ kubectl -n dev-team get pods -o wide

OUTPUT:

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

Step 4: Bind Labels to Specific Namespaces

You can bind a label to a namespace using the scheduler.alpha.kubernetes.io/node-selector annotation. This binds specific labels to the namespace, so all pods in that namespace will only be scheduled on nodes with the matching labels.

To bind a label to the dev-team namespace, use the following:

controlplane $ kubectl edit namespaces dev-team

This will open the namespace configuration in an editor. Add the following annotations under metadata:

apiVersion: v1
kind: Namespace
metadata:
  creationTimestamp: "2024-12-13T12:49:34Z"
  labels:
    kubernetes.io/metadata.name: dev-team
  name: dev-team
  annotations:
    scheduler.alpha.kubernetes.io/node-selector: "node=worker"

Save and exit the editor.


Step 5: Verify Pod Scheduling Based on the Label

Now, create a pod in the dev-team namespace. The pod will only be scheduled on nodes that have the node=worker label:

controlplane $ kubectl run -n dev-team my-pod --image=nginx

Verify where the pod is running:

controlplane $ kubectl get pods -n dev-team -o wide

You should see that the pod is scheduled on the correct node, in this case, a node labeled worker.

Example output:

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

In this example, the pod is scheduled on the controlplane node because it matches the node label specified in the namespace.


Conclusion

By labeling nodes and binding them to namespaces, you can have fine-grained control over where your pods run in a Kubernetes cluster. This is particularly useful when you want to segregate workloads and ensure that pods are scheduled on nodes with specific resources or capabilities.


Tags:

  • Kubernetes

  • Node Labeling

  • Namespaces

  • Pods

  • Kubernetes Tutorial

  • DevOps

  • Kubernetes Scheduler