New batchZero to DevOps starts 29 Nov · limited seatsReserve your seat
Cheat sheetsKubernetesKubernetes
FreeSheet 07 of 16

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

Labels are key-value pairs used to organize and select Kubernetes resources.

Common Label Patterns

LabelExamplePurpose
appapp: nginxIdentify the application
environmentenvironment: productionEnvironment separation
tiertier: frontendApplication tier
versionversion: v1.2.3Version tracking
releaserelease: stableRelease channel
managed-bymanaged-by: terraformManagement tool

kubectl label — Manage Labels

Adds, updates, or removes labels on resources.

Basic Usage

Add a label:

bash
$ kubectl label pod nginx app=web
pod/nginx labeled

Add multiple labels:

bash
$ kubectl label pod nginx app=web tier=frontend

Add label to all pods:

bash
$ kubectl label pods --all app=web

Common Flags

FlagPurpose
--overwriteOverwrite existing label
-nNamespace
--allApply to all resources
-lLabel selector (filter)

Examples

Overwrite an existing label:

bash
$ kubectl label pod nginx app=webserver --overwrite

Remove a label:

bash
$ kubectl label pod nginx app-
pod/nginx unlabeled

Remove multiple labels:

bash
$ kubectl label pod nginx app- tier-

Add label to a deployment (affects pods):

bash
$ kubectl label deployment nginx app=web

Add label with namespace:

bash
$ kubectl label pod nginx app=web -n production

Add label to all pods in a namespace:

bash
$ kubectl label pods --all app=web -n production

Add label only to pods matching a selector:

bash
$ kubectl label pods -l environment=staging app=web

kubectl annotate — Manage Annotations

Adds, updates, or removes annotations on resources.

Basic Usage

Add an annotation:

bash
$ kubectl annotate pod nginx description="Web server for production"
pod/nginx annotated

Add multiple annotations:

bash
$ kubectl annotate pod nginx description="Web server" version="1.2.3"

Common Flags

FlagPurpose
--overwriteOverwrite existing annotation
-nNamespace
--allApply to all resources
-lLabel selector (filter)

Examples

Overwrite an annotation:

bash
$ kubectl annotate pod nginx description="Updated description" --overwrite

Remove an annotation:

bash
$ kubectl annotate pod nginx description-
pod/nginx unannotated

Annotate deployment:

bash
$ kubectl annotate deployment nginx deployment-tool="helm"

kubectl get with Selectors — Filter Resources

Use label selectors to filter resources.

Basic Syntax

bash
$ kubectl get pods -l <label-selector>

Equality-Based Selectors

OperatorPurposeExample
= or ==Equal-l app=nginx
!=Not equal-l app!=nginx

Set-Based Selectors

OperatorPurposeExample
inIn a set-l 'environment in (dev, staging)'
notinNot in a set-l 'environment notin (prod)'
existsKey exists-l app
!existsKey doesn't exist-l '!app'

Examples

Get pods with a specific label:

bash
$ kubectl get pods -l app=nginx

Get pods with labels matching multiple criteria:

bash
$ kubectl get pods -l app=nginx,environment=production

Get pods NOT having a label:

bash
$ kubectl get pods -l app!=nginx

Get pods with environment in a set:

bash
$ kubectl get pods -l 'environment in (dev, staging)'

Get pods where app label exists:

bash
$ kubectl get pods -l app

Get pods where app label does NOT exist:

bash
$ kubectl get pods -l '!app'

Get pods with custom output:

bash
$ kubectl get pods -l app=nginx -o wide

kubectl get --show-labels — Show Labels in Output

Displays labels alongside resource information.

Basic Usage

bash
$ 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=staging

Examples

Show labels on nodes:

bash
$ kubectl get nodes --show-labels

Show labels on deployments:

bash
$ kubectl get deployments --show-labels

Practical Examples

Label Management Workflow

1. Add labels to a deployment:

bash
$ kubectl label deployment nginx app=web environment=production

2. Get pods with these labels:

bash
$ kubectl get pods -l app=web,environment=production

3. Update deployment (and pods):

bash
$ kubectl label deployment nginx app=webserver --overwrite
$ kubectl get pods --show-labels | grep webserver

4. Remove labels:

bash
$ kubectl label deployment nginx app- environment-

Service Selector Example

Service with selector:

yaml
apiVersion: v1
kind: Service
metadata:
  name: nginx-service
spec:
  selector:
    app: nginx
    tier: frontend
  ports:
  - port: 80

Check matching pods:

bash
$ kubectl get pods -l app=nginx,tier=frontend

Quick Reference — Most Used Commands

TaskCommand
Add labelkubectl label pod nginx app=web
Add multiple labelskubectl label pod nginx app=web tier=frontend
Overwrite labelkubectl label pod nginx app=webserver --overwrite
Remove labelkubectl label pod nginx app-
Add annotationkubectl annotate pod nginx description="Web server"
Remove annotationkubectl annotate pod nginx description-
Filter by labelkubectl get pods -l app=nginx
Filter by multiple labelskubectl get pods -l app=nginx,environment=production
Filter NOT equalkubectl get pods -l app!=nginx
Filter IN setkubectl get pods -l 'environment in (dev, staging)'
Show labels in outputkubectl get pods --show-labels

Pro Tips

  1. Labels are for grouping and selection — use them for deployments, services, and networking.

  2. Annotations are for metadata — use them for tools, descriptions, and non-queryable data.

  3. Use consistent label keys — establish standards like app, environment, tier.

  4. Validate label selectors — kubectl get pods -l before applying manifests.

  5. Service selectors must match pod labels — otherwise endpoints remain empty.

  6. Show labels on all resources:

    bash
    kubectl get all --show-labels
  7. Use label selectors for deletion:

    bash
    kubectl delete pods -l app=old-version

Next in KubernetesNamespaces & Contexts Pro