(Day 28) Task : Kubernetes Labels and Selectors:-

Aditya SharmaAditya Sharma
3 min read

Labels and selectors are the hidden heroes behind managing, organizing, and operating workloads in Kubernetes clusters. Whether you're working with a single Pod or deploying hundreds of microservices, labels and selectors provide a powerful mechanism for targeting specific sets of resources in your Kubernetes environment.

In today's blog post, I’ll walk you through:

  • What labels and selectors are.

  • How to assign and filter them.

  • Common issues and best practices.

  • My hands-on experiments with labels.

What Are Labels?

  • Labels in Kubernetes are key-value pairs attached to objects like Pods, Nodes, Services, and Deployments. They’re used to identify, group, and filter resources.

  • Labels are the mechanism used to organize Kubernetes objects.

  • A label is a key-value pair without any predefined meaning that can be attached to the object.

  • Labels are similar to tags in AWS or Git, where we use a name to quick reference.

  • Multiple labels can be added to a single object. Labels will always reside inside metadata.

Syntax:

labels:
  key: value

Example:

labels:
  env: development
  app: nginx
  region: asia

What Are Label Selectors?

  • Label selectors are expressions or queries used to filter Kubernetes objects based on their labels. They are used in commands like kubectl get, and also in controller specs (like Deployments or Services).

  • Unlike Name/UIDs, Label does not provide any uniqueness. We can think of many objects to carry same label.

  • Once all object has labels, then LabelSelector helps to narrow down.

  • The API currently supports two types of Selector, EqualityBase and SetBase.

Types of Selectors:

  • Equality-based selectors

    • env=production

    • env!=development

  • Set-based selectors

    • env in (development, testing)

    • env notin (staging, qa)

Hands-On Commands:

Creating a Pod with Labels :

I created a simple pod named delhipod with these labels:

kind: Pod
apiVersion: v1
metadata:
  name: delhipod
  labels:
    env: development
    class: pods
spec:
  containers:
     - name: c00
       image: ubuntu
       command: ["/bin/bash", "-c", "while true; do echo Hello-Aditya; sleep 5 ; done"]

Applied using :

kubectl apply -f pod2.yaml

Verifying the Pod :

kubectl get pods --show-labels

Output:

NAME       READY   STATUS    RESTARTS   AGE     LABELS
delhipod   1/1     Running   0          2m1s      class=pods,env=development

Adding a Label Post-Creation :

kubectl label pod delhipod myname=Aditya  # Object = pod , Objectname = delhipod , myname=Aditya --> Key : Value

Now the pod has:

LABELS
class=pods,env=development,myname=Aditya

Filtering Pods Using Label Selectors :-

  1. Now listing pods matching same labels :(Equality based)
kubectl get pods -l env=development
  1. Now list where development label not present :(Equality based)
kubectl get pods -l env!=development
  1. Set-Based Filter :
kubectl get pods -l 'env in (development, testing)'
  1. Multi-Label Filter :
kubectl get pods -l class=pods,myname=Aditya

Be careful not to add spaces around commas — Kubernetes will throw an error:

Common Errors I Faced (and Solved!) :-

1. Invalid Pod Name :

The Pod "Adipod" is invalid: metadata.name: Invalid value: "Adipod"

Fix: Pod names must be lowercase and follow DNS-1123 format:

[a-z0-9]([-a-z0-9]*[a-z0-9])?

2. Incorrect Label Command Syntax :

kubectl label pods delhipod myname = Aditya

Fix: No spaces allowed around the equal sign.

Final Thoughts

Labels and selectors may seem simple, but they’re powerful tools for managing complex Kubernetes environments. From grouping deployments to performing rolling updates and clean deletions, labels give you a declarative way to organize and manipulate cluster objects.

This hands-on session gave me real insight into how developers and DevOps engineers can leverage this system effectively. Onwards to more!

✍️ Written by Aditya Sharma
🎯 #90DaysOfDevOps | Day 28
📍 Follow me on Hashnode for more Kubernetes and DevOps insights!

Let me know if you'd like to add a YAML manifest snippet for multiple containers, Services, or Deployments using selectors!

1
Subscribe to my newsletter

Read articles from Aditya Sharma directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Aditya Sharma
Aditya Sharma

DevOps Enthusiast | Python | Chef | Docker | GitHub | Linux | Shell Scripting | CI/CD & Cloud Learner | AWS