Kubernetes has steadily advanced into an trade commonplace for container orchestration, powering platforms from small developer clusters to hyperscale AI and knowledge infrastructures. Each new launch introduces options that not solely make workloads simpler to handle but additionally enhance efficiency, price effectivity, and resilience.
With the v1.34 launch, one of many standout enhancements is the introduction of visitors distribution preferences for Kubernetes Companies. Particularly:
- PreferSameNode: route visitors to the identical node because the consumer pod if doable.
- PreferSameZone: routing visitors by giving choice to endpoints in the identical topology zone earlier than going for cross-zone.
These insurance policies add smarter, locality-aware routing to Service visitors distribution. As an alternative of treating all pods equally, Kubernetes can now choose pods which might be nearer to the consumer, whether or not on the identical node or in the identical availability zone (AZ).
This transformation is straightforward, but it surely has significant implications for performance-sensitive and cost-sensitive workloads, notably in giant multi-node and multi-zone clusters.
Site visitors Distribution Significance
Historically, a Kubernetes Service balances visitors evenly throughout all endpoints/pods that match its selector. This even visitors distribution is straightforward, predictable, and works nicely for many use instances.
Nevertheless, it doesn’t think about topology, the bodily or logical placement of pods throughout nodes and zones.
Spherical-Robin Challenges
- Elevated latency: If a consumer pod on Node A routes to a Service endpoint on Node B (or worst case to a unique zone), the additional community hop provides milliseconds of delay.
- Cross-zone prices: In cloud environments, cross-az visitors is usually billed by cloud suppliers; even a couple of mb’s of cross-zone visitors throughout 1000’s of pods can rack up important prices.
- Cache inefficiency: Some ML inference companies cache fashions in reminiscence per pod. If requests bounce throughout pods randomly, cache hit charges enhance, hurting each efficiency and useful resource effectivity.
What’s New in Kubernetes v1.34
The brand new trafficDistribution discipline, Kubernetes companies now help an non-compulsory discipline below spec:
spec:
 trafficDistribution: PreferSameNode | PreferSameZone
- Default habits (if unset): visitors remains to be distributed evenly throughout all endpoints.
- PreferSameNode: The kube-proxy (or service proxy) will try to ship visitors to pods working on the identical node because the consumer pod. If no such endpoints can be found, it falls again to zone-level or cluster-wide balancing.
- PreferSameZone: The proxy will prioritize endpoints throughout the identical topology zone because the consumer pod. If none can be found, it falls again to cluster-wide distribution.
Site visitors Distribution Excessive-Stage Diagram
These preferences are non-compulsory, and if no choice is specified, then by default, the visitors might be evenly distributed throughout all endpoints within the cluster.
Advantages
- Decrease latency: Requests take fewer community hops when served regionally on the identical node or throughout the identical zone. That is particularly crucial for microservices with low SLA necessities or ML workloads the place inference instances are measured in milliseconds.
- Decreased prices: Cloud suppliers usually cost for cross-zone visitors. Routing to native pods first avoids these expenses until essential.
- Improved cache utilization: Workloads comparable to ML inference pods typically hold fashions, embeddings, or function shops heat in reminiscence, with the identical node routing, which will increase cache hit charges.
- Constructed-in fault tolerance: Each insurance policies are preferences, not onerous necessities. If no native endpoints exist as a result of a node being drained or a zone outage, then Kubernetes seamlessly falls again to cluster-wide distribution.
Use Circumstances
- ML inference companies cache heat fashions within the pod.
- Distributed programs the place knowledge nodes align with zones.
- Bigger orgs deploying throughout a number of AZs can obtain sensible failover as visitors stays native below regular circumstances, however failover seamlessly if the zone experiences an outage.
Demo Walkthrough
We are going to attempt to cowl visitors distribution eventualities — default, PreferSameZone, PreferSameNode, and fallback — within the demo beneath.
Demo: Set Up Cluster, Deploy Pods, Companies, and Consumer
Step 1: Begin a multi-node cluster on minikube and label the nodes with zones:
minikube begin -p mnode --nodes=3 --kubernetes-version=v1.34.0
kubectl config use-context mnode
kubectl label node mnode-m02 topology.kubernetes.io/zone=zone-a --overwrite
kubectl label node mnode-m03 topology.kubernetes.io/zone=zone-b --overwrite
Step 2: Deploy the echo app with two replicas and the echo service.
# echo-pod.yaml
apiVersion: apps/v1
sort: Deployment
metadata:
title: echo
spec:
replicas: 2
selector:
matchLabels:
app: echo
template:
metadata:
labels:
app: echo
spec:
containers:
- title: echo
picture: hashicorp/http-echo
args:
- "-text=Howdy from $(POD_NAME)"
env:
- title: POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.title
ports:
- containerPort: 5678
# echo-service.yaml
apiVersion: v1
sort: Service
metadata:
title: echo-svc
spec:
selector:
app: echo
ports:
- port: 80
targetPort: 5678
kubectl apply -f echo-pod.yaml
kubectl apply -f echo-service.yaml
# confirm pods are working on separate nodes and zones
kubectl get pods -l app=echo -o=custom-columns=NAME:.metadata.title,NODE:.spec.nodeName --no-headers
| whereas learn pod node; do
zone=$(kubectl get node "$node" -o jsonpath="{.metadata.labels.topology.kubernetes.io/zone}")
printf "%-35s %-15s %sn" "$pod" "$node" "$zone"
accomplished
As you possibly can see within the screenshot beneath, two echo pods spin up on separate nodes (mnode-m02, mnode-m03) and availability zones (zone-a, zone-b).
Step 3: Deploy a consumer pod in zone A.
# consumer.yaml
apiVersion: v1
sort: Pod
metadata:
title: consumer
spec:
nodeSelector:
topology.kubernetes.io/zone: zone-a
restartPolicy: By no means
containers:
- title: consumer
picture: alpine:3.19
command: ["sh", "-c", "sleep infinity"]
stdin: true
tty: true
kubectl apply -f consumer.yaml
kubectl get pod consumer -o=custom-columns=NAME:.metadata.title,NODE:.spec.nodeName --no-headers
| whereas learn pod node; do
zone=$(kubectl get node "$node" -o jsonpath="{.metadata.labels.topology.kubernetes.io/zone}")
printf "%-35s %-15s %sn" "$pod" "$node" "$zone"
accomplished
Consumer pod is scheduled on node mnode-m02 in zone-a.
Step 4: Arrange a helper script within the consumer pod.
kubectl exec -it consumer -- sh
apk add --no-cache curl jq
cat > /hit.sh <<'EOS'
#!/bin/sh
COUNT="${1:-20}"
SVC="${2:-echo}"
PORT="${3:-80}"
i=1
whereas [ "$i" -le "$COUNT" ]; do
curl -s "http://${SVC}:${PORT}/"
| jq -r '.env.POD_NAME + "@" + .env.NODE_NAME'
i=$((i+1))
accomplished | kind | uniq -c
EOS
chmod +x /hit.sh
exit
Demo: Default Habits
Type consumer shell run script: hit.sh to generate visitors from consumer pod to echo service.
Habits: Within the beneath screenshot, you possibly can see visitors routed to each pods (10 requests every) in round-robin type.
Demo: PreferSameNode
Patch echo service definition spec so as to add/patch visitors distribution: PreferSameNode.
kubectl patch svc echo --type merge -p '{"spec":{"trafficDistribution":"PreferSameNode"}}'
Type consumer shell run script:hit.sh to generate visitors from consumer pod to echo service.
Habits: Site visitors ought to get routed to pod:echo-687cbdc966-mgwn5@mnode-m02 residing on the identical node:mnode-m02 as consumer pod.
Demo: PreferSameZone
Replace echo service definition spec so as to add/patch visitors distribution: PreferSameNode
kubectl patch svc echo --type merge -p '{"spec":{"trafficDistribution":"PreferSameZone"}}'
Type consumer shell run script:hit.sh to generate visitors from consumer pod to echo service.
Habits: visitors ought to get routed to the pod residing in the identical zone (zone-a) because the consumer pod.
Demo: Fallback
Drive all echo pods to zone-b, then check once more:
kubectl scale deploy echo --replicas=1
kubectl patch deploy echo --type merge -p
'{"spec":{"template":{"spec":{"nodeSelector":{"topology.kubernetes.io/zone":"zone-b"}}}}}'
kubectl rollout standing deploy/echo
Type consumer shell run script:hit.sh to generate visitors from consumer pod to echo service.
End result Abstract
| coverage | habits |
|---|---|
|
Default |
Site visitors distributed throughout all endpoints in round-robin trend. |
|
PreferSameNode |
Prefers pods on the identical node, falls again if none accessible. |
|
PreferSameZone |
Prefers pods in the identical zone, falls again if none accessible. |
Conclusion
Kubernetes launch v1.34 provides two small however impactful capabilities: PreferSameNode and PreferSameZone, these preferences helps builders and k8s operators to make visitors routing smarter, making certain visitors prioritizes native endpoints whereas sustaining resiliency with fallback mechanism.







