0
0
Kubernetesdevops~5 mins

Label-based filtering with kubectl in Kubernetes - Commands & Configuration

Choose your learning style9 modes available
Introduction
When you have many Kubernetes resources, it can be hard to find the ones you want. Labels are like name tags for resources. Using labels, you can quickly find and manage specific groups of resources.
When you want to see all pods related to a specific app in your cluster.
When you need to delete all resources with a certain label to clean up.
When you want to check the status of all services tagged for a particular environment like staging.
When you want to update or patch only the deployments labeled as frontend.
When you want to monitor or debug a group of pods that share the same label.
Commands
This command lists all pods and shows their labels so you can see what labels each pod has.
Terminal
kubectl get pods --show-labels
Expected OutputExpected
NAME READY STATUS RESTARTS AGE LABELS my-app-pod-1 1/1 Running 0 10m app=my-app,env=prod my-app-pod-2 1/1 Running 0 10m app=my-app,env=prod other-pod 1/1 Running 0 5m app=other,env=dev
--show-labels - Displays the labels assigned to each resource in the list.
This command filters and shows only pods that have the label 'app=my-app'.
Terminal
kubectl get pods -l app=my-app
Expected OutputExpected
NAME READY STATUS RESTARTS AGE my-app-pod-1 1/1 Running 0 10m my-app-pod-2 1/1 Running 0 10m
-l - Filters resources by label selector.
This command filters pods that have both labels 'app=my-app' and 'env=prod'.
Terminal
kubectl get pods -l 'app=my-app,env=prod'
Expected OutputExpected
NAME READY STATUS RESTARTS AGE my-app-pod-1 1/1 Running 0 10m my-app-pod-2 1/1 Running 0 10m
-l - Filters resources by multiple labels combined with AND logic.
This command shows pods where the label 'env' is not equal to 'prod'.
Terminal
kubectl get pods -l 'env!=prod'
Expected OutputExpected
NAME READY STATUS RESTARTS AGE other-pod 1/1 Running 0 5m
-l - Supports label filtering with inequality.
Key Concept

If you remember nothing else from this pattern, remember: use the -l flag with kubectl to filter resources by their labels quickly and precisely.

Common Mistakes
Using incorrect label syntax like missing quotes around multiple labels.
The shell may misinterpret commas or special characters, causing the command to fail or filter incorrectly.
Always put multiple labels in single quotes like -l 'app=my-app,env=prod' to ensure correct parsing.
Trying to filter by a label that does not exist on any resource.
kubectl returns no results, which can be confusing if you expect matches.
Check labels with --show-labels first to confirm label names before filtering.
Using spaces around commas in label selectors.
Spaces break the label selector syntax and cause errors.
Do not add spaces; write labels as 'app=my-app,env=prod' without spaces.
Summary
Use 'kubectl get pods --show-labels' to see labels on pods.
Use 'kubectl get pods -l <label-selector>' to filter pods by labels.
Label selectors support multiple labels combined with commas and operators like !=.