Skip to content

Commit 5f50b28

Browse files
authored
Merge pull request #45276 from gauravkghildiyal/new-topology-field
Document .spec.trafficDistribution field in Service
2 parents a7c220c + 56b83b5 commit 5f50b28

File tree

4 files changed

+138
-0
lines changed

4 files changed

+138
-0
lines changed

content/en/docs/concepts/services-networking/service.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -967,6 +967,35 @@ to control how Kubernetes routes traffic to healthy (“ready”) backends.
967967

968968
See [Traffic Policies](/docs/reference/networking/virtual-ips/#traffic-policies) for more details.
969969

970+
### Trafic distribution
971+
972+
{{< feature-state feature_gate_name="ServiceTrafficDistribution" >}}
973+
974+
The `.spec.trafficDistribution` field provides another way to influence traffic
975+
routing within a Kubernetes Service. While traffic policies focus on strict
976+
semantic guarantees, traffic distribution allows you to express _preferences_
977+
(such as routing to topologically closer endpoints). This can help optimize for
978+
performance, cost, or reliability. This optional field can be used if you have
979+
enabled the `ServiceTrafficDistribution` [feature
980+
gate](/docs/reference/command-line-tools-reference/feature-gates/) for your
981+
cluster and all of its nodes. In Kubernetes {{< skew currentVersion >}}, the
982+
following field value is supported:
983+
984+
`PreferClose`
985+
: Indicates a preference for routing traffic to endpoints that are topologically
986+
proximate to the client. The interpretation of "topologically proximate" may
987+
vary across implementations and could encompass endpoints within the same
988+
node, rack, zone, or even region. Setting this value gives implementations
989+
permission to make different tradeoffs, e.g. optimizing for proximity rather
990+
than equal distribution of load. Users should not set this value if such
991+
tradeoffs are not acceptable.
992+
993+
If the field is not set, the implementation will apply its default routing strategy.
994+
995+
See [Traffic
996+
Distribution](/docs/reference/networking/virtual-ips/#traffic-distribution) for
997+
more details
998+
970999
### Session stickiness
9711000

9721001
If you want to make sure that connections from a particular client are passed to

content/en/docs/concepts/services-networking/topology-aware-routing.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,3 +198,8 @@ yet cover some relevant and plausible situations.
198198
## {{% heading "whatsnext" %}}
199199

200200
* Follow the [Connecting Applications with Services](/docs/tutorials/services/connect-applications-service/) tutorial
201+
* Learn about the
202+
[trafficDistribution](/docs/concepts/services-networking/service/#trafic-distribution)
203+
field, which is closely related to the `service.kubernetes.io/topology-mode`
204+
annotation and provides flexible options for traffic routing within
205+
Kubernetes.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
title: ServiceTrafficDistribution
3+
content_type: feature_gate
4+
5+
_build:
6+
list: never
7+
render: false
8+
9+
stages:
10+
- stage: alpha
11+
defaultValue: false
12+
fromVersion: "1.30"
13+
---
14+
Allows usage of the optional `spec.trafficDistribution` field in Services. The
15+
field offers a way to express preferences for how traffic is distributed to
16+
Service endpoints.

content/en/docs/reference/networking/virtual-ips.md

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,94 @@ those terminating Pods. By the time the Pod completes termination, the external
513513
should have seen the node's health check failing and fully removed the node from the backend
514514
pool.
515515

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+
516604
## {{% heading "whatsnext" %}}
517605

518606
To learn more about Services,

0 commit comments

Comments
 (0)