0
0
Kubernetesdevops~10 mins

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

Choose your learning style9 modes available
Process Flow - Service selectors and labels
Define Labels on Pods
Create Service with Selector
Service Queries Pods
Selector Matches Labels?
NoNo 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.