Skip to content

Kubernetes_labels_selectors

Chapter 25: Kubernetes Labels and Selectors - Organization and Discovery

Section titled “Chapter 25: Kubernetes Labels and Selectors - Organization and Discovery”
  1. Introduction to Labels
  2. Label Syntax
  3. Using Labels
  4. Label Selectors
  5. Annotations
  6. Hands-on Lab
  7. Summary

Labels are key-value pairs attached to Kubernetes objects. They are used to identify, organize, and select subsets of objects. Unlike names, labels are not unique and multiple labels can be attached to the same object.

┌─────────────────────────────────────────────────────────────────────────────┐
│ KUBERNETES LABELS │
├─────────────────────────────────────────────────────────────────────────────┤
│ │
│ ┌───────────────────────────────────────────────────────────────────┐ │
│ │ POD │ │
│ │ metadata: │ │
│ │ labels: │ │
│ │ app: myapp # Identify the application │ │
│ │ tier: frontend # Tier (frontend, backend, db) │ │
│ │ environment: production # Environment │ │
│ │ version: v1.0.0 # Version of the app │ │
│ │ team: platform # Team responsible │ │
│ │ release: stable # Release channel │ │
│ │ │ │
│ └───────────────────────────────────────────────────────────────────┘ │
│ │
│ Labels allow you to: │
│ • Group objects by characteristics │
│ • Select specific objects for operations │
│ • Organize resources logically │
│ • Implement service discovery │
│ │
└─────────────────────────────────────────────────────────────────────────────┘

# Labels in Pod definition
apiVersion: v1
kind: Pod
metadata:
name: myapp-pod
labels:
app: myapp
tier: frontend
environment: production
version: v1.0.0
spec:
containers:
- name: myapp
image: nginx:latest
┌─────────────────────────────────────────────────────────────────────────────┐
│ LABEL SYNTAX RULES │
├─────────────────────────────────────────────────────────────────────────────┤
│ │
│ Valid Label Key: │
│ ───────────────── │
│ • Two segments: optional prefix + name, separated by '/' │
│ • Prefix is optional, name is required │
│ • Name segment: 1-63 characters │
│ • Can contain: alphanumeric, '-', '_', '.' │
│ • Must start/end with alphanumeric │
│ │
│ Examples: │
│ ┌────────────────────────────────────────────────────────────────────┐ │
│ │ app ✓ Valid │ │
│ │ App ✓ Valid │ │
│ │ app.myapp ✓ Valid │ │
│ │ app-name ✓ Valid │ │
│ │ kubernetes.io/app ✓ Valid (with prefix) │ │
│ │ app_1 ✓ Valid │ │
│ │ App ✓ Valid │ │
│ │ (empty) ✗ Invalid │ │
│ │ -app ✗ Invalid (starts with '-') │ │
│ │ app@prod ✗ Invalid (contains '@') │ │
│ └────────────────────────────────────────────────────────────────────┘ │
│ │
│ Valid Label Value: │
│ ───────────────── │
│ • 0-63 characters │
│ • Can contain: alphanumeric, '-', '_', '.' │
│ • Must start/end with alphanumeric │
│ │
│ Examples: │
│ ┌────────────────────────────────────────────────────────────────────┐ │
│ │ production ✓ Valid │ │
│ │ v1.0.0 ✓ Valid │ │
│ │ v1.0 ✓ Valid │ │
│ │ my-app-v2 ✓ Valid │ │
│ │ (empty) ✓ Valid (but not recommended) │ │
│ │ -invalid ✗ Invalid │ │
│ └────────────────────────────────────────────────────────────────────┘ │
│ │
└─────────────────────────────────────────────────────────────────────────────┘

Terminal window
# Create pod with labels
kubectl run myapp --image=nginx --labels="app=myapp,tier=frontend"
# Add labels to existing pod
kubectl label pods myapp-pod environment=production
# Add labels to nodes
kubectl label nodes node-01 disktype=ssd
# Add labels to namespaces
kubectl label namespace production env=prod
Terminal window
# View labels
kubectl get pods --show-labels
# Label pods with specific labels
kubectl get pods -L app,tier
# Update a label
kubectl label pods myapp-pod tier=backend --overwrite
# Remove a label
kubectl label pods myapp-pod tier-
# Filter by label
kubectl get pods -l app=myapp
kubectl get pods -l 'app in (myapp, api)'
kubectl get pods -l tier=frontend,tier=backend

Terminal window
# Equals (= or ==)
kubectl get pods -l app=myapp
kubectl get pods -l app==myapp
# Not Equals (!=)
kubectl get pods -l tier!=frontend
Terminal window
# In
kubectl get pods -l 'app in (myapp, api)'
# Not In
kubectl get pods -l 'tier notin (frontend, backend)'
# Exists
kubectl get pods -l app
# Not Exists
kubectl get pods -l '!app'
# Combination
kubectl get pods -l 'app in (myapp, api),environment=production'
# ReplicaSet selector
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: myapp-replicaset
spec:
selector:
matchLabels:
app: myapp
matchExpressions:
- key: tier
operator: In
values:
- frontend
- backend
---
# Service selector
apiVersion: v1
kind: Service
metadata:
name: myapp-service
spec:
selector:
app: myapp
ports:
- port: 80
targetPort: 8080
┌─────────────────────────────────────────────────────────────────────────────┐
│ LABEL SELECTOR EXAMPLES │
├─────────────────────────────────────────────────────────────────────────────┤
│ │
│ ┌─────────────────────────────────────────────────────────────────┐ │
│ │ LABELED OBJECTS │ │
│ │ │ │
│ │ Pod1: {app: myapp, tier: frontend, env: prod} │ │
│ │ Pod2: {app: myapp, tier: backend, env: prod} │ │
│ │ Pod3: {app: api, tier: backend, env: staging} │ │
│ │ Pod4: {app: myapp, tier: frontend, env: staging} │ │
│ │ │ │
│ └─────────────────────────────────────────────────────────────────┘ │
│ │
│ Selector Examples: │
│ ──────────────── │
│ │
│ app=myapp │
│ ┌─────────────────────────────────────────────────────────────────┐ │
│ │ Selected: Pod1, Pod2, Pod4 │ │
│ └─────────────────────────────────────────────────────────────────┘ │
│ │
│ tier=frontend │
│ ┌─────────────────────────────────────────────────────────────────┐ │
│ │ Selected: Pod1, Pod4 │ │
│ └─────────────────────────────────────────────────────────────────┘ │
│ │
│ app in (myapp, api), env=prod │
│ ┌─────────────────────────────────────────────────────────────────┐ │
│ │ Selected: Pod1, Pod2 │ │
│ └─────────────────────────────────────────────────────────────────┘ │
│ │
│ tier notin (frontend) │
│ ┌─────────────────────────────────────────────────────────────────┐ │
│ │ Selected: Pod2, Pod3 │ │
│ └─────────────────────────────────────────────────────────────────┘ │
│ │
└─────────────────────────────────────────────────────────────────────────────┘

Annotations are similar to labels but are intended for non-identifying metadata. They are used to attach arbitrary metadata that can be used by tools, libraries, or for documentation purposes.

# Pod with annotations
apiVersion: v1
kind: Pod
metadata:
name: myapp-pod
annotations:
description: "Production web server for myapp"
maintainer: "team@example.com"
kubernetes.io/change-cause: "Update to version 2.0"
prometheus.io/scrape: "true"
prometheus.io/port: "8080"
spec:
containers:
- name: myapp
image: nginx:latest
┌─────────────────────────────────────────────────────────────────────────────┐
│ LABELS VS ANNOTATIONS │
├─────────────────────────────────────────────────────────────────────────────┤
│ │
│ Feature │ Labels │ Annotations │
│ ────────────────┼────────────────────┼─────────────────── │
│ Purpose │ Identify & select │ Attach metadata │
│ Used by K8s │ Yes (selectors) │ No │
│ Queryable │ Yes │ Usually not │
│ Character limit│ 63 char │ No limit │
│ Recommended │ Key info │ Additional info │
│ Use cases │ • app name │ • Description │
│ │ • tier │ • Maintainer info │
│ │ • environment │ • Build info │
│ │ • version │ • Links to docs │
│ │
│ Examples: │
│ ┌────────────────────────────────────────────────────────────────────┐ │
│ │ Labels: │ │
│ │ app: myapp, tier: frontend, version: v1.0 │ │
│ │ │ │
│ │ Annotations: │ │
│ │ description: "Frontend web server for customer-facing app" │ │
│ │ build: "jenkins-build-1234" │ │
│ │ kubernetes.io/ingress.class: "nginx" │ │
│ └────────────────────────────────────────────────────────────────────┘ │
│ │
└─────────────────────────────────────────────────────────────────────────────┘

  • A running Kubernetes cluster
Terminal window
# Step 1: Create pods with labels
kubectl run frontend --image=nginx --labels="app=myapp,tier=frontend,environment=dev"
kubectl run backend --image=nginx --labels="app=myapp,tier=backend,environment=dev"
kubectl run cache --image=redis --labels="app=myapp,tier=cache,environment=dev"
# Step 2: List pods with labels
kubectl get pods --show-labels
# Step 3: Filter by labels
kubectl get pods -l app=myapp
kubectl get pods -l tier=frontend
kubectl get pods -l 'app in (myapp, api)'
kubectl get pods -l 'environment=dev,tier=frontend'
# Step 4: Add labels to existing pod
kubectl label pods backend environment=production
# Step 5: Verify label change
kubectl get pods -l environment
# Step 6: Use labels with services
cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: Service
metadata:
name: myapp-service
spec:
selector:
app: myapp
ports:
- port: 80
targetPort: 80
EOF
# Step 7: Use labels with deployments
cat <<EOF | kubectl apply -f -
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp-deployment
spec:
selector:
matchLabels:
app: myapp
replicas: 3
template:
metadata:
labels:
app: myapp
tier: frontend
spec:
containers:
- name: nginx
image: nginx:latest
EOF
# Step 8: Node labels (for scheduling)
kubectl label nodes <node-name> disktype=ssd
# Step 9: Add annotations
kubectl annotate pod myapp-pod description="Production API server"
# Step 10: View annotations
kubectl describe pod myapp-pod | grep -A 10 Annotations
# Step 11: Clean up
kubectl delete pod frontend backend cache
kubectl delete service myapp-service
kubectl delete deployment myapp-deployment

  1. Labels - Key-value pairs for identification and selection
  2. Selectors - Filter objects by label criteria
  3. Equality-based - =, ==, !=
  4. Set-based - in, notin, exists, !
  5. Annotations - Non-identifying metadata
Terminal window
# Add labels
kubectl label pods <pod> key=value
# Remove label
kubectl label pods <pod> key-
# Filter by label
kubectl get pods -l key=value
# Add annotation
kubectl annotate pod <pod> key=value
# Get with labels
kubectl get pods -L key1,key2
kubectl get pods --show-labels

This completes the Kubernetes Fundamentals section. You now have a solid understanding of:

  • Kubernetes Introduction
  • Pods
  • Deployments
  • Services
  • Installation
  • ReplicaSets
  • ConfigMaps and Secrets
  • Storage
  • Namespaces
  • Labels and Selectors

Continue to Kubernetes Advanced topics (Chapter 26-32):

  • Ingress Controllers
  • Horizontal Pod Autoscaling
  • StatefulSets
  • Network Policies
  • RBAC
  • Helm
  • Operators