0
0
Kubernetesdevops~5 mins

Label selectors (equality, set-based) in Kubernetes - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a label selector in Kubernetes?
A label selector is a way to filter Kubernetes objects based on their labels. It helps you find and manage groups of resources by matching key-value pairs.
Click to reveal answer
beginner
Explain equality-based label selectors.
Equality-based selectors match resources where a label key equals or does not equal a specific value. For example, environment=production selects resources with label environment set to production.
Click to reveal answer
intermediate
What are set-based label selectors?
Set-based selectors allow filtering resources based on whether a label's value is in or not in a set of values. For example, environment in (production, staging) selects resources labeled with environment as production or staging.
Click to reveal answer
beginner
How do you express 'not equal' in equality-based selectors?
Use the != operator. For example, tier!=frontend selects resources where the tier label is anything except frontend.
Click to reveal answer
intermediate
Give an example of a set-based selector that excludes certain values.
You can use notin to exclude values. For example, environment notin (dev, test) selects resources whose environment label is neither dev nor test.
Click to reveal answer
Which operator is used for equality-based label selectors to match exact values?
A=
Bin
Cnotin
Dexists
What does the selector app in (frontend, backend) do?
ASelects resources with app label equal to frontend or backend
BSelects resources without app label
CSelects resources with app label not equal to frontend or backend
DSelects resources with app label exactly 'frontend, backend'
How do you select resources where a label key exists regardless of its value?
Akey in ()
Bkey = *
Ckey exists
Dkey
Which selector excludes resources with label tier set to 'frontend'?
Atier in (frontend)
Btier=frontend
Ctier!=frontend
Dtier notin (frontend)
What is the correct syntax to select resources with environment label not in dev or test?
Aenvironment notin dev test
Benvironment notin (dev, test)
Cenvironment not (dev, test)
Denvironment != (dev, test)
Describe the difference between equality-based and set-based label selectors in Kubernetes.
Think about matching one value versus matching a group of values.
You got /3 concepts.
    How would you write a label selector to find all pods with label 'app' set to either 'web' or 'api' but not 'test'?
    Use 'app in (web, api)' and 'app notin (test)'
    You got /3 concepts.