@@ -513,6 +513,94 @@ those terminating Pods. By the time the Pod completes termination, the external
513
513
should have seen the node's health check failing and fully removed the node from the backend
514
514
pool.
515
515
516
+ ## Traffic Distribution
517
+
518
+ The ` spec.trafficDistribution ` field within a Kubernetes Service allows you to
519
+ express preferences for how traffic should be routed to Service endpoints.
520
+ Implementations like kube-proxy use the ` spec.trafficDistribution ` field as a
521
+ guideline. The behavior associated with a given preference may subtly differ
522
+ between implementations.
523
+
524
+ ` PreferClose ` with kube-proxy
525
+ : For kube-proxy, this means prioritizing sending traffic to endpoints within
526
+ the same zone as the client. The EndpointSlice controller updates
527
+ EndpointSlices with ` hints ` to communicate this preference, which kube-proxy
528
+ then uses for routing decisions. If a client's zone does not have any
529
+ available endpoints, traffic will be routed cluster-wide for that client.
530
+
531
+ In the absence of any value for ` trafficDistribution ` , the default routing
532
+ strategy for kube-proxy is to distribute traffic to any endpoint in the cluster.
533
+
534
+ ### Comparison with ` service.kubernetes.io/topology-mode: Auto `
535
+
536
+ The ` trafficDistribution ` field with ` PreferClose ` and the
537
+ ` service.kubernetes.io/topology-mode: Auto ` annotation both aim to prioritize
538
+ same-zone traffic. However, there are key differences in their approaches:
539
+
540
+ * ` service.kubernetes.io/topology-mode: Auto ` : Attempts to distribute traffic
541
+ proportionally across zones based on allocatable CPU resources. This heuristic
542
+ includes safeguards (such as the [ fallback
543
+ behavior] ( /docs/concepts/services-networking/topology-aware-routing/#three-or-more-endpoints-per-zone )
544
+ for small numbers of endpoints) and could lead to the feature being disabled
545
+ in certain scenarios for load-balancing reasons. This approach sacrifices some
546
+ predictability in favor of potential load balancing.
547
+
548
+ * ` trafficDistribution: PreferClose ` : This approach aims to be slightly simpler
549
+ and more predictable: "If there are endpoints in the zone, they will receive
550
+ all traffic for that zone, if there are no endpoints in a zone, the traffic
551
+ will be distributed to other zones". While the approach may offer more
552
+ predictability, it does mean that you are in control of managing a [ potential
553
+ overload] ( #considerations-for-using-traffic-distribution-control ) .
554
+
555
+ If the ` service.kubernetes.io/topology-mode ` annotation is set to ` Auto ` , it
556
+ will take precedence over ` trafficDistribution ` . (The annotation may be deprecated
557
+ in the future in favour of the ` trafficDistribution ` field).
558
+
559
+ ### Interaction with Traffic Policies
560
+
561
+ When compared to the ` trafficDistribution ` field, the traffic policy fields
562
+ (` externalTrafficPolicy ` and ` internalTrafficPolicy ` ) are meant to offer a
563
+ stricter traffic locality requirements. Here's how ` trafficDistribution `
564
+ interacts with them:
565
+
566
+ * Precedence of Traffic Policies: For a given Service, if a traffic policy
567
+ (` externalTrafficPolicy ` or ` internalTrafficPolicy ` ) is set to ` Local ` , it
568
+ takes precedence over ` trafficDistribution: PreferClose ` for the corresponding
569
+ traffic type (external or internal, respectively).
570
+
571
+ * ` trafficDistribution ` Influence: For a given Service, if a traffic policy
572
+ (` externalTrafficPolicy ` or ` internalTrafficPolicy ` ) is set to ` Cluster ` (the
573
+ default), or if the fields are not set, then `trafficDistribution:
574
+ PreferClose` guides the routing behavior for the corresponding traffic type
575
+ (external or internal, respectively). This means that an attempt will be made
576
+ to route traffic to an endpoint that is in the same zone as the client.
577
+
578
+ ### Considerations for using traffic distribution control
579
+
580
+ * ** Increased Probability of Overloaded Endpoints:** The ` PreferClose `
581
+ heuristic will attempt to route traffic to the closest healthy endpoints
582
+ instead of spreading that traffic evenly across all endpoints. If you do not
583
+ have a sufficient number of endpoints within a zone, they may become
584
+ overloaded. This is especially likely if incoming traffic is not
585
+ proportionally distributed across zones. To mitigate this, consider the
586
+ following strategies:
587
+
588
+ * [ Pod Topology Spread
589
+ Constraints] ( /docs/concepts/scheduling-eviction/topology-spread-constraints/ ) :
590
+ Use Pod Topology Spread Constraints to distribute your pods more evenly
591
+ across zones.
592
+
593
+ * Zone-specific Deployments: If you expect to see skewed traffic patterns,
594
+ create a separate Deployment for each zone. This approach allows the
595
+ separate workloads to scale independently. There are also workload
596
+ management addons available from the ecosystem, outside the Kubernetes
597
+ project itself, that can help here.
598
+
599
+ * ** Implementation-specific behavior:** Each dataplane implementation may handle
600
+ this field slightly differently. If you're using an implementation other than
601
+ kube-proxy, refer the documentation specific to that implementation to
602
+ understand how this field is being handled.
603
+
516
604
## {{% heading "whatsnext" %}}
517
605
518
606
To learn more about Services,
0 commit comments