0
0
Kubernetesdevops~10 mins

Using labels for service routing in Kubernetes - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Using labels for service routing
Create Pods with labels
Define Service selector
Service routes traffic
Traffic reaches matching Pods
Pods respond to requests
END
Pods are created with labels. The Service uses selectors to find Pods with matching labels. Traffic sent to the Service is routed to those Pods.
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 Pods with label 'app: myapp' and a Service selecting Pods with that label to route traffic on port 80.
Process Table
StepActionResourceLabels/SelectorResult
1Create PodPod 'pod1'app: myappPod created with label app=myapp
2Create PodPod 'pod2'app: myappPod created with label app=myapp
3Create ServiceService 'myservice'selector app=myappService created selecting pods with app=myapp
4Send traffic to ServiceService 'myservice'selector app=myappTraffic routed to pod1 and pod2
5Pods respondPods 'pod1' and 'pod2'app=myappPods handle requests
6Send traffic to ServiceService 'myservice'selector app=myappTraffic routed only to pods matching label
7Create PodPod 'pod3'app: otherappPod created with label app=otherapp (not selected)
8Send traffic to ServiceService 'myservice'selector app=myappTraffic NOT routed to pod3
9ENDRouting only to pods with matching labels
💡 Pods without matching labels are excluded from Service routing
Status Tracker
VariableStartAfter Step 1After Step 2After Step 7Final
Pods with label app=myapp01 (pod1)2 (pod1, pod2)2 (pod1, pod2)2 (pod1, pod2)
Pods with label app=otherapp0001 (pod3)1 (pod3)
Service selectornonenoneapp=myappapp=myappapp=myapp
Traffic routed podsnonenonepod1, pod2pod1, pod2pod1, pod2
Key Moments - 3 Insights
Why doesn't the Service route traffic to pod3 even though it exists?
Because pod3 has label app=otherapp which does not match the Service selector app=myapp (see execution_table step 8). Only pods with matching labels receive traffic.
What happens if a Pod is created without any labels?
The Service selector won't find it, so traffic won't be routed to that Pod. Labels are required for Service routing (see execution_table steps 1-3).
Can a Service route traffic to Pods with multiple different labels?
No, the Service selector matches Pods with the exact label set specified. To route to multiple label sets, multiple Services or label selectors with multiple keys are needed.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table at step 4. Which Pods receive traffic from the Service?
Apod1 and pod2
Bpod3 only
Cpod1, pod2, and pod3
DNo Pods receive traffic
💡 Hint
Check the 'Result' column at step 4 in the execution_table.
At which step does a Pod with a non-matching label get created?
AStep 2
BStep 5
CStep 7
DStep 3
💡 Hint
Look for the Pod with label app=otherapp in the execution_table.
If the Service selector changed to app=otherapp, which Pods would receive traffic?
Apod1 and pod2
Bpod3 only
CAll Pods
DNo Pods
💡 Hint
Refer to variable_tracker for Pods with label app=otherapp.
Concept Snapshot
Pods have labels as tags.
Services use selectors to find Pods by labels.
Traffic sent to Service goes only to matching Pods.
Pods without matching labels do not get traffic.
Labels enable flexible routing in Kubernetes.
Full Transcript
In Kubernetes, Pods are created with labels, which are like name tags. A Service uses a selector to find Pods with specific labels. When you send traffic to the Service, it routes the requests only to Pods whose labels match the selector. Pods without matching labels do not receive traffic. This method allows you to control which Pods handle which requests by labeling them accordingly.