πŸ“šBook Signing at KubeCon EU 2026Meet us at Booking.com HQ (Mon 18:30-21:00) & vCluster booth #521 (Tue 24 Mar, 12:30-1:30pm) β€” free book giveaway!RSVP Booking.com Event
Configuration beginner ⏱ 15 minutes K8s 1.28+

Kubernetes Labels and Selectors Guide

Master Kubernetes labels and selectors for organizing and querying resources. Covers label conventions, equality selectors, set-based selectors, and field selectors.

By Luca Berton β€’ β€’ πŸ“– 5 min read

πŸ’‘ Quick Answer: Master Kubernetes labels and selectors for organizing and querying resources. Covers label conventions, equality selectors, set-based selectors, and field selectors.

The Problem

This is one of the most searched Kubernetes topics. Having a comprehensive, well-structured guide helps both beginners and experienced users quickly find what they need.

The Solution

Add Labels

apiVersion: v1
kind: Pod
metadata:
  name: web-server
  labels:
    app: web
    version: v2
    tier: frontend
    environment: production
    team: platform
# Add/update labels
kubectl label pod web-server release=stable
kubectl label pod web-server release=canary --overwrite

# Remove label
kubectl label pod web-server release-

# Show labels
kubectl get pods --show-labels
kubectl get pods -L app,version    # Show specific label columns

Query with Selectors

# Equality-based
kubectl get pods -l app=web
kubectl get pods -l app!=web
kubectl get pods -l app=web,tier=frontend    # AND

# Set-based
kubectl get pods -l 'app in (web, api)'
kubectl get pods -l 'app notin (web)'
kubectl get pods -l 'version'                # Has label
kubectl get pods -l '!version'               # Missing label

# Field selectors (built-in fields)
kubectl get pods --field-selector status.phase=Running
kubectl get pods --field-selector spec.nodeName=worker-1
labels:
  # Standard Kubernetes labels
  app.kubernetes.io/name: web-server
  app.kubernetes.io/instance: web-prod
  app.kubernetes.io/version: "2.0.0"
  app.kubernetes.io/component: frontend
  app.kubernetes.io/part-of: e-commerce
  app.kubernetes.io/managed-by: helm

Labels in Selectors

# Service selector β€” routes traffic to matching pods
apiVersion: v1
kind: Service
spec:
  selector:
    app: web
    version: v2

# Deployment selector
apiVersion: apps/v1
kind: Deployment
spec:
  selector:
    matchLabels:
      app: web
    matchExpressions:
      - key: version
        operator: In
        values: ["v1", "v2"]
graph TD
    A[Labels on resources] --> B[Selectors query labels]
    B --> C[Service routes to pods]
    B --> D[Deployment manages pods]
    B --> E[kubectl filters output]
    B --> F[NetworkPolicy targets pods]

Frequently Asked Questions

Labels vs annotations?

Labels identify and select objects (used by selectors). Annotations store metadata (build info, git hash, descriptions) β€” not queryable by selectors.

Is there a limit on labels?

Each label key must be ≀63 chars (with optional prefix ≀253 chars). Each value must be ≀63 chars. No hard limit on number of labels per resource, but keep it reasonable.

Best Practices

  • Start simple β€” use the basic form first, add complexity as needed
  • Be consistent β€” follow naming conventions across your cluster
  • Document your choices β€” add annotations explaining why, not just what
  • Monitor and iterate β€” review configurations regularly

Key Takeaways

  • This is fundamental Kubernetes knowledge every engineer needs
  • Start with the simplest approach that solves your problem
  • Use kubectl explain and kubectl describe when unsure
  • Practice in a test cluster before applying to production
#labels #selectors #organization #filtering #kubernetes
Luca Berton
Written by Luca Berton

Principal Solutions Architect specializing in Kubernetes, AI/GPU infrastructure, and cloud-native platforms. Author of Kubernetes Recipes and creator of CopyPasteLearn courses.

Kubernetes Recipes book cover

Want More Kubernetes Recipes?

This recipe is from Kubernetes Recipes, our 750-page practical guide with hundreds of production-ready patterns.

Luca Berton Ansible Pilot Ansible by Example Open Empower K8s Recipes Terraform Pilot CopyPasteLearn ProteinLens