Skip to main content

Chapter 3.3 - Labels and Selectors

Learning Objectives

By the end of this chapter, you will be able to:

  • Understand the role of Labels
  • Create and use Labels
  • Select resources with Selectors
  • Organize your resources effectively

What is a Label?

A Label is a key-value pair attached to a Kubernetes resource to identify and organize it.


Why Use Labels?

Organization

Labels allow you to:

  • Group resources logically
  • Filter and select resources
  • Organize by environment, application, version, etc.

Label Examples

labels:
app: my-application # Application name
env: production # Environment
tier: frontend # Layer (frontend/backend)
version: v1.2.3 # Version
team: backend-team # Responsible team
component: api-server # Component

Declaring Labels

In YAML

apiVersion: v1
kind: Pod
metadata:
name: web-pod
labels:
app: web
env: production
tier: frontend
version: v1
spec:
containers:
- name: nginx
image: nginx:1.20

Via kubectl

# Add a label
kubectl label pod web-pod team=backend

# Modify an existing label
kubectl label pod web-pod env=staging --overwrite

# Remove a label
kubectl label pod web-pod team-

Selectors

Selectors allow you to select resources based on their Labels.

Simple Selectors

selector:
matchLabels:
app: web
env: production

Selectors with Expressions

selector:
matchExpressions:
- key: env
operator: In
values:
- production
- staging
- key: version
operator: NotIn
values:
- v1

Operators:

  • In: The value is in the list
  • NotIn: The value is not in the list
  • Exists: The key exists
  • DoesNotExist: The key does not exist

Usage in kubectl

Filtering with Labels

# Pods with a specific label
kubectl get pods -l app=web

# Pods with multiple labels
kubectl get pods -l app=web,env=production

# Pods without a label
kubectl get pods -l '!env'

# Pods with a label in a list
kubectl get pods -l 'env in (production,staging)'

Complex Selectors

# AND expression
kubectl get pods -l app=web,env=production

# OR expression (via multiple queries)
kubectl get pods -l app=web
kubectl get pods -l app=api

Use Case: Services

Services use Selectors to find Pods:

apiVersion: v1
kind: Service
metadata:
name: web-service
spec:
selector:
app: web
tier: frontend
ports:
- port: 80
targetPort: 8080

Best Practices

Naming Conventions

  • Use short, descriptive names
  • Avoid special characters (except - and _)
  • Use lowercase values
  • Maximum 63 characters per label
labels:
app.kubernetes.io/name: my-app
app.kubernetes.io/instance: my-app-prod
app.kubernetes.io/version: "1.2.3"
app.kubernetes.io/component: frontend
app.kubernetes.io/part-of: my-system
app.kubernetes.io/managed-by: helm

Summary

In this chapter, you learned:

Labels: Key-value pairs to organize resources
Selectors: Select resources based on Labels
Usage: Services, Deployments, ReplicaSets use Selectors
Commands: kubectl label, filtering with -l
Best practices: Naming conventions and recommended labels


Next Steps

Now that you have mastered Labels:

Chapter 3.4: Pod Lifecycle
Chapter 3.5: ReplicaSets


Chapter created: December 2024