Labels are simple key-value pairs attached to Kubernetes objects. They help organize and select groups of objects for management and filtering.
Click to reveal answer
beginner
How do you list pods with a specific label using kubectl?
Use the command kubectl get pods -l key=value to list pods that have the label with the specified key and value.
Click to reveal answer
intermediate
What does the command kubectl get pods -l env=prod,tier=frontend do?
It lists pods that have both labels env=prod and tier=frontend. Both conditions must be true for a pod to be shown.
Click to reveal answer
intermediate
How can you filter Kubernetes objects by label existence, regardless of value?
Use kubectl get pods -l key to select pods that have the label key, no matter what its value is.
Click to reveal answer
intermediate
What is the difference between -l key=value and -l 'key!=value' in kubectl?
-l key=value selects objects with that exact label value. -l 'key!=value' selects objects where the label key exists but its value is not equal to the given value.
Click to reveal answer
Which kubectl command lists pods with the label app=web?
Akubectl get pods -l app=web
Bkubectl get pods --filter app=web
Ckubectl describe pods app=web
Dkubectl get pods --label app=web
✗ Incorrect
The correct syntax to filter by label is using -l key=value.
How do you select pods that have the label 'env' regardless of its value?
Akubectl get pods -l env
Bkubectl get pods -l env=*
Ckubectl get pods -l env!=null
Dkubectl get pods --env
✗ Incorrect
Using -l env selects pods that have the label key 'env' no matter the value.
What does kubectl get pods -l 'tier!=backend' do?
ASelects all pods
BSelects pods without the label 'tier'
CSelects pods where 'tier' label equals 'backend'
DSelects pods where 'tier' label exists and is not 'backend'
✗ Incorrect
The != operator filters pods where the label exists but its value is not 'backend'.
How to filter pods with labels env=prod and tier=frontend at the same time?
Akubectl get pods --label env=prod --label tier=frontend
Bkubectl get pods -l env=prod,tier=frontend
Ckubectl get pods -l env=prod tier=frontend
Dkubectl get pods -l env=prod|tier=frontend
✗ Incorrect
Multiple labels are combined with commas in the -l option.
Which command shows all pods without filtering by label?
Akubectl get pods -l '*'
Bkubectl get pods -l
Ckubectl get pods
Dkubectl get pods --all-labels
✗ Incorrect
Running kubectl get pods without -l shows all pods.
Explain how to use kubectl to filter Kubernetes objects by labels. Include examples of filtering by single label, multiple labels, and label existence.
Think about how labels help select groups of objects.
You got /3 concepts.
Describe the difference between filtering with 'key=value' and 'key!=value' in kubectl label selectors.
Consider how the != operator changes the selection.
You got /2 concepts.
Practice
(1/5)
1. What does the -l option do in the kubectl get command?
easy
A. Filters resources based on their labels
B. Lists all namespaces in the cluster
C. Deletes resources matching a label
D. Updates the labels of a resource
Solution
Step 1: Understand the purpose of -l in kubectl get
The -l option is used to specify a label selector to filter resources.
Step 2: Identify what filtering means in this context
Filtering means showing only resources that have the specified label(s).
Final Answer:
Filters resources based on their labels -> Option A
Quick Check:
-l filters by labels [OK]
Hint: Remember: -l means label filter in kubectl [OK]
Common Mistakes:
Confusing -l with namespace filtering
Thinking -l deletes resources
Assuming -l updates labels
2. Which of the following is the correct syntax to list pods with the label app=frontend?
easy
A. kubectl get pods -l app=frontend
B. kubectl get pods --label app=frontend
C. kubectl get pods -label app=frontend
D. kubectl get pods -filter app=frontend
Solution
Step 1: Recall the correct flag for label filtering
The correct flag is -l or --selector, not --label or -filter.
Step 2: Check the syntax for label selector
The syntax is -l key=value, so -l app=frontend is correct.
Final Answer:
kubectl get pods -l app=frontend -> Option A
Quick Check:
Use -l key=value for label filtering [OK]
Hint: Use -l key=value to filter by label [OK]
Common Mistakes:
Using incorrect flags like --label or -filter
Missing the equals sign between key and value
Using spaces instead of equals sign
3. Given these pods with labels: pod1: app=frontend, env=prod pod2: app=frontend, env=dev pod3: app=backend, env=prod What is the output of kubectl get pods -l 'app=frontend,env=prod' -o name?
medium
A. pod/pod3
B. pod/pod1
C. pod/pod2
D. pod/pod1
pod/pod2
Solution
Step 1: Understand label selector with multiple labels
The selector app=frontend,env=prod means pods must have both labels with these exact values.
Step 2: Match pods with both labels
pod1 has app=frontend and env=prod, so it matches. pod2 has env=dev, so no. pod3 has app=backend, so no.
Final Answer:
pod/pod1 -> Option B
Quick Check:
Multiple labels require all to match [OK]
Hint: Multiple labels with commas mean AND condition [OK]
Common Mistakes:
Assuming comma means OR instead of AND
Ignoring one of the labels in the selector
Expecting multiple pods when only one matches all labels
4. You run kubectl get pods -l app=frontend env=prod but get an error. What is the mistake?
medium
A. Labels must be quoted
B. Using wrong resource type
C. Missing comma between labels in the selector
D. kubectl does not support multiple labels
Solution
Step 1: Analyze the label selector syntax
The command uses -l app=frontend env=prod without a comma between labels, which is invalid syntax.
Step 2: Correct syntax requires comma separation
Labels must be separated by commas like -l app=frontend,env=prod to work properly.
Final Answer:
Missing comma between labels in the selector -> Option C
Quick Check:
Separate multiple labels with commas [OK]
Hint: Separate multiple labels with commas, no spaces [OK]
Common Mistakes:
Forgetting commas between multiple labels
Adding spaces instead of commas
Quoting labels unnecessarily
5. You want to list all pods that have label app=frontend but exclude those with label env=dev. Which command achieves this?
hard
A. kubectl get pods -l 'app=frontend' -l 'env!=dev'
B. kubectl get pods -l 'app=frontend' --exclude-label env=dev
C. kubectl get pods -l 'app=frontend' | grep -v env=dev
D. kubectl get pods -l 'app=frontend,env!=dev'
Solution
Step 1: Understand label selector for exclusion
To exclude pods with a label, use key!=value syntax inside the selector.
Step 2: Combine inclusion and exclusion in one selector
Use -l 'app=frontend,env!=dev' to include pods with app=frontend and exclude those with env=dev.
Final Answer:
kubectl get pods -l 'app=frontend,env!=dev' -> Option D
Quick Check:
Use key!=value to exclude labels [OK]
Hint: Use key!=value to exclude labels in selector [OK]
Common Mistakes:
Trying to use non-existent flags like --exclude-label
Using multiple -l flags instead of one combined selector
Using grep which is less efficient and error-prone
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.