Labels & Selectors
A comprehensive developer cheatsheet covering Kubernetes labels and annotations, featuring essential CLI commands, common flags, and operational workflows for organizing cluster resources, attaching metadata, filtering with label selectors, and inspecting label outputs using kubectl.
Table of Contents
- Labels — Resource Metadata
- kubectl label — Manage Labels
- kubectl annotate — Manage Annotations
- kubectl get with Selectors — Filter Resources
- Quick Reference — Most Used Commands
- Pro Tips
Labels — Resource Metadata
Labels are key-value pairs used to organize and select Kubernetes resources.
Common Label Patterns
| Label | Example | Purpose |
|---|---|---|
app | app: nginx | Identify the application |
environment | environment: production | Environment separation |
tier | tier: frontend | Application tier |
version | version: v1.2.3 | Version tracking |
release | release: stable | Release channel |
managed-by | managed-by: terraform | Management tool |
kubectl label — Manage Labels
Adds, updates, or removes labels on resources.
Basic Usage
Add a label:
$ kubectl label pod nginx app=web
pod/nginx labeledAdd multiple labels:
$ kubectl label pod nginx app=web tier=frontendAdd label to all pods:
$ kubectl label pods --all app=webCommon Flags
| Flag | Purpose |
|---|---|
--overwrite | Overwrite existing label |
-n | Namespace |
--all | Apply to all resources |
-l | Label selector (filter) |
Examples
Overwrite an existing label:
$ kubectl label pod nginx app=webserver --overwriteRemove a label:
$ kubectl label pod nginx app-
pod/nginx unlabeledRemove multiple labels:
$ kubectl label pod nginx app- tier-Add label to a deployment (affects pods):
$ kubectl label deployment nginx app=webAdd label with namespace:
$ kubectl label pod nginx app=web -n productionAdd label to all pods in a namespace:
$ kubectl label pods --all app=web -n productionAdd label only to pods matching a selector:
$ kubectl label pods -l environment=staging app=webkubectl annotate — Manage Annotations
Adds, updates, or removes annotations on resources.
Basic Usage
Add an annotation:
$ kubectl annotate pod nginx description="Web server for production"
pod/nginx annotatedAdd multiple annotations:
$ kubectl annotate pod nginx description="Web server" version="1.2.3"Common Flags
| Flag | Purpose |
|---|---|
--overwrite | Overwrite existing annotation |
-n | Namespace |
--all | Apply to all resources |
-l | Label selector (filter) |
Examples
Overwrite an annotation:
$ kubectl annotate pod nginx description="Updated description" --overwriteRemove an annotation:
$ kubectl annotate pod nginx description-
pod/nginx unannotatedAnnotate deployment:
$ kubectl annotate deployment nginx deployment-tool="helm"kubectl get with Selectors — Filter Resources
Use label selectors to filter resources.
Basic Syntax
$ kubectl get pods -l <label-selector>Equality-Based Selectors
| Operator | Purpose | Example |
|---|---|---|
= or == | Equal | -l app=nginx |
!= | Not equal | -l app!=nginx |
Set-Based Selectors
| Operator | Purpose | Example |
|---|---|---|
in | In a set | -l 'environment in (dev, staging)' |
notin | Not in a set | -l 'environment notin (prod)' |
exists | Key exists | -l app |
!exists | Key doesn't exist | -l '!app' |
Examples
Get pods with a specific label:
$ kubectl get pods -l app=nginxGet pods with labels matching multiple criteria:
$ kubectl get pods -l app=nginx,environment=productionGet pods NOT having a label:
$ kubectl get pods -l app!=nginxGet pods with environment in a set:
$ kubectl get pods -l 'environment in (dev, staging)'Get pods where app label exists:
$ kubectl get pods -l appGet pods where app label does NOT exist:
$ kubectl get pods -l '!app'Get pods with custom output:
$ kubectl get pods -l app=nginx -o widekubectl get --show-labels — Show Labels in Output
Displays labels alongside resource information.
Basic Usage
$ kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
nginx-1 1/1 Running 0 5d app=nginx,environment=production
nginx-2 1/1 Running 0 5d app=nginx,environment=stagingExamples
Show labels on nodes:
$ kubectl get nodes --show-labelsShow labels on deployments:
$ kubectl get deployments --show-labelsPractical Examples
Label Management Workflow
1. Add labels to a deployment:
$ kubectl label deployment nginx app=web environment=production2. Get pods with these labels:
$ kubectl get pods -l app=web,environment=production3. Update deployment (and pods):
$ kubectl label deployment nginx app=webserver --overwrite
$ kubectl get pods --show-labels | grep webserver4. Remove labels:
$ kubectl label deployment nginx app- environment-Service Selector Example
Service with selector:
apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
selector:
app: nginx
tier: frontend
ports:
- port: 80Check matching pods:
$ kubectl get pods -l app=nginx,tier=frontendQuick Reference — Most Used Commands
| Task | Command |
|---|---|
| Add label | kubectl label pod nginx app=web |
| Add multiple labels | kubectl label pod nginx app=web tier=frontend |
| Overwrite label | kubectl label pod nginx app=webserver --overwrite |
| Remove label | kubectl label pod nginx app- |
| Add annotation | kubectl annotate pod nginx description="Web server" |
| Remove annotation | kubectl annotate pod nginx description- |
| Filter by label | kubectl get pods -l app=nginx |
| Filter by multiple labels | kubectl get pods -l app=nginx,environment=production |
| Filter NOT equal | kubectl get pods -l app!=nginx |
| Filter IN set | kubectl get pods -l 'environment in (dev, staging)' |
| Show labels in output | kubectl get pods --show-labels |
Pro Tips
-
Labels are for grouping and selection — use them for deployments, services, and networking.
-
Annotations are for metadata — use them for tools, descriptions, and non-queryable data.
-
Use consistent label keys — establish standards like
app,environment,tier. -
Validate label selectors —
kubectl get pods -lbefore applying manifests. -
Service selectors must match pod labels — otherwise endpoints remain empty.
-
Show labels on all resources:
bashkubectl get all --show-labels -
Use label selectors for deletion:
bashkubectl delete pods -l app=old-version