📖 Scenario: You are managing a Kubernetes cluster for a small online store. You want to organize your pods and services so that the services can find the right pods using labels and selectors.
🎯 Goal: Learn how to create pods with labels and a service that selects those pods using matching labels.
📋 What You'll Learn
Create pods with specific labels
Create a service with a selector matching the pod labels
Verify the service selects the correct pods
💡 Why This Matters
🌍 Real World
Labels and selectors help organize and manage groups of pods in Kubernetes. Services use selectors to route traffic to the right pods automatically.
💼 Career
Understanding labels and selectors is essential for Kubernetes administrators and DevOps engineers to deploy and maintain scalable applications.
Progress0 / 4 steps
1
Create pods with labels
Create two pod YAML definitions named pod1.yaml and pod2.yaml. Each pod should have the label app: webstore. Use the image nginx for both pods.
Kubernetes
Hint
Use metadata.labels to add labels to pods. Both pods must have the label app: webstore.
2
Create a service with a selector
Create a service YAML definition named service.yaml that selects pods with the label app: webstore. Use the service type ClusterIP and expose port 80.
Kubernetes
Hint
Use spec.selector to match the label app: webstore. Set spec.type to ClusterIP and expose port 80.
3
Verify the service selects the pods
Write the kubectl command to list pods selected by the service webstore-service using the label selector app=webstore.
Kubernetes
Hint
Use kubectl get pods -l app=webstore to list pods with the label app=webstore.
4
Display the pods selected by the service
Run the command kubectl get pods -l app=webstore and print the output exactly as it appears.
Kubernetes
Hint
The output shows the pods with label app=webstore selected by the service.
Practice
(1/5)
1. What is the main purpose of a selector in a Kubernetes Service?
easy
A. To match labels on pods so the service knows which pods to send traffic to
B. To define the IP address of the service
C. To specify the container image used by pods
D. To set resource limits for pods
Solution
Step 1: Understand what selectors do
A selector in a Kubernetes Service is used to find pods with matching labels.
Step 2: Connect selectors to pods
The service uses the selector to route traffic only to pods that have the matching labels.
Final Answer:
To match labels on pods so the service knows which pods to send traffic to -> Option A
Quick Check:
Selector matches pod labels [OK]
Hint: Selector matches pod labels to connect service and pods [OK]
Common Mistakes:
Thinking selector sets IP or image
Confusing selector with resource limits
Assuming selector is for service IP
2. Which of the following is the correct way to specify a selector in a Service YAML to match pods with label app: frontend?
easy
A. matchLabels:
app: frontend
B. labels:
app: frontend
C. podSelector:
app: frontend
D. selector:
app: frontend
Solution
Step 1: Identify correct selector syntax in Service YAML
The Service spec uses selector: followed by key-value pairs to match pod labels.
Step 2: Compare options
selector:
app: frontend uses selector: with app: frontend, which is correct. Others use incorrect keys like labels, matchLabels, or podSelector which are not valid in Service spec.
Final Answer:
selector:
app: frontend -> Option D
Quick Check:
Service selector key is 'selector' [OK]
Hint: Service selector key is always 'selector' not 'labels' or others [OK]
Common Mistakes:
Using 'labels' instead of 'selector'
Confusing with pod label syntax
Using 'matchLabels' which is for label selectors, not Service
3. Given this Service selector:
selector:
tier: backend
env: prod
Which pods will this Service select?
medium
A. Pods with label tier=backend only
B. Pods with labels tier=backend and env=prod
C. Pods with label env=prod only
D. Pods with either tier=backend or env=prod
Solution
Step 1: Understand selector matching logic
A Service selector matches pods that have all the specified labels with exact values.
Step 2: Apply to given selector
The selector requires pods to have both tier=backend and env=prod labels to be selected.
Final Answer:
Pods with labels tier=backend and env=prod -> Option B
Quick Check:
Selector matches all labels, not any [OK]
Hint: Selector matches pods with all labels, not just one [OK]
Common Mistakes:
Thinking selector matches pods with any one label
Ignoring one of the labels in selector
Confusing OR with AND logic in selectors
4. You have a Service with this selector:
selector:
app: myapp
version: v1
But your pods have labels:
app: myapp
version: v2
What is the problem?
medium
A. The Service selector syntax is invalid
B. The Service will select pods with version v2 anyway
C. The Service selector does not match any pods because version labels differ
D. Pods need a label 'service: myapp' to be selected
Solution
Step 1: Compare Service selector and pod labels
The Service selector requires version: v1, but pods have version: v2.
Step 2: Understand label matching rules
Labels must match exactly for the pod to be selected. Different version values mean no match.
Final Answer:
The Service selector does not match any pods because version labels differ -> Option C
Quick Check:
Exact label match required for selection [OK]
Hint: Labels must match exactly; different values mean no selection [OK]
Common Mistakes:
Assuming partial label match works
Thinking selector syntax is wrong
Believing extra pod labels are needed
5. You want a Service to select pods with label app=web but exclude pods with label env=dev. How can you configure the selector?
hard
A. Use a selector with app: web and add a matchExpressions to exclude env=dev
B. Use selector:
app: web
env: !dev
C. Set selector:
app: web
env: dev
D. Label pods with app=web and remove env label from dev pods
Solution
Step 1: Understand label selector limitations
Simple selectors only match labels present; to exclude labels, use matchExpressions with operators.
Step 2: Use matchExpressions to exclude
You can specify matchExpressions with operator NotIn to exclude pods with env=dev.
Step 3: Review options
Use a selector with app: web and add a matchExpressions to exclude env=dev correctly describes using matchExpressions to exclude labels. Use selector:
app: web
env: !dev syntax is invalid. Set selector:
app: web
env: dev includes the label to exclude. Label pods with app=web and remove env label from dev pods is about pod labeling, not selector configuration.
Final Answer:
Use a selector with app: web and add a matchExpressions to exclude env=dev -> Option A
Quick Check:
Exclude labels using matchExpressions NotIn [OK]
Hint: Use matchExpressions with NotIn to exclude labels in selector [OK]
Common Mistakes:
Trying to exclude labels with simple selector syntax
Using invalid selector syntax like env: !dev
Confusing pod labeling with selector configuration
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.