Bird
Raised Fist0
Kubernetesdevops~10 mins

Service selectors and labels in Kubernetes - Step-by-Step Execution

Choose your learning style10 modes available
Process Flow - Service selectors and labels
Define Labels on Pods
↓
Create Service with Selector
↓
Service Queries Pods
↓
Selector Matches Labels?
No→No Pods Selected
Yes↓
Service Routes Traffic to Matched Pods
Pods get labels, Services use selectors to find matching Pods, then route traffic to them.
Execution Sample
Kubernetes
apiVersion: v1
kind: Pod
metadata:
  labels:
    app: myapp
---
apiVersion: v1
kind: Service
metadata:
  name: myservice
spec:
  selector:
    app: myapp
  ports:
  - port: 80
    targetPort: 80
Defines a Pod with label 'app: myapp' and a Service selecting Pods with that label to route traffic on port 80.
Process Table
StepActionSelectorPod LabelsMatch ResultService Behavior
1Pod createdN/Aapp: myappN/APod exists with label
2Service created with selectorapp: myappN/AN/AService ready to select pods
3Service queries podsapp: myappapp: myappMatchRoutes traffic to this pod
4Service queries podsapp: myappapp: otherappNo MatchDoes not route traffic
5Pod deletedapp: myappN/AN/ANo pods matched, no traffic routed
💡 No pods match selector after deletion, service has no endpoints
Status Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 5
Pod LabelsNoneapp: myappapp: myappapp: myappNone
Service SelectorNoneNoneapp: myappapp: myappapp: myapp
Service EndpointsNoneNoneNonePod with app: myappNone
Key Moments - 2 Insights
Why does the Service not route traffic to pods without matching labels?
Because the Service uses the selector to find pods with matching labels only, as shown in execution_table step 4 where labels differ and no match occurs.
What happens if a pod with matching labels is deleted?
The Service stops routing traffic to that pod since it no longer exists, as shown in execution_table step 5 where no pods match the selector.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which step does the Service start routing traffic to a pod?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Check the 'Service Behavior' column for when routing begins.
According to variable_tracker, what is the Service Endpoints value after Step 2?
ANone
Bapp: myapp
CPod with app: myapp
DNone, because no pods exist yet
💡 Hint
Look at the 'Service Endpoints' row and the 'After Step 2' column.
If the pod label changed to 'app: otherapp', what would happen to the Service routing?
AService routes traffic to all pods regardless of label
BService routes traffic to the pod
CService stops routing traffic to the pod
DService crashes
💡 Hint
Refer to execution_table step 4 where labels do not match selector.
Concept Snapshot
Service selectors use labels to find pods.
Pods must have matching labels to receive traffic.
Selector is a key-value map in Service spec.
If no pods match, Service has no endpoints.
Labels and selectors must match exactly.
Full Transcript
In Kubernetes, Services use selectors to find pods with matching labels. First, pods are created with labels like 'app: myapp'. Then, a Service is created with a selector 'app: myapp'. The Service queries pods and routes traffic only to those whose labels match the selector. If a pod's labels do not match, the Service ignores it. When a matching pod is deleted, the Service stops routing traffic to it. This process ensures traffic goes only to intended pods.

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

  1. Step 1: Understand what selectors do

    A selector in a Kubernetes Service is used to find pods with matching labels.
  2. Step 2: Connect selectors to pods

    The service uses the selector to route traffic only to pods that have the matching labels.
  3. Final Answer:

    To match labels on pods so the service knows which pods to send traffic to -> Option A
  4. 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

  1. Step 1: Identify correct selector syntax in Service YAML

    The Service spec uses selector: followed by key-value pairs to match pod labels.
  2. 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.
  3. Final Answer:

    selector: app: frontend -> Option D
  4. 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

  1. Step 1: Understand selector matching logic

    A Service selector matches pods that have all the specified labels with exact values.
  2. Step 2: Apply to given selector

    The selector requires pods to have both tier=backend and env=prod labels to be selected.
  3. Final Answer:

    Pods with labels tier=backend and env=prod -> Option B
  4. 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

  1. Step 1: Compare Service selector and pod labels

    The Service selector requires version: v1, but pods have version: v2.
  2. Step 2: Understand label matching rules

    Labels must match exactly for the pod to be selected. Different version values mean no match.
  3. Final Answer:

    The Service selector does not match any pods because version labels differ -> Option C
  4. 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

  1. Step 1: Understand label selector limitations

    Simple selectors only match labels present; to exclude labels, use matchExpressions with operators.
  2. Step 2: Use matchExpressions to exclude

    You can specify matchExpressions with operator NotIn to exclude pods with env=dev.
  3. 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.
  4. Final Answer:

    Use a selector with app: web and add a matchExpressions to exclude env=dev -> Option A
  5. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard