- Docs
- Networking
- Load balancing
- Route Service traffic with trafficDistribution
Route Service traffic with trafficDistribution
By default, a Kubernetes Service can route requests to any of its endpoints, regardless of which nodes they run on. To keep requests on the client's node when a local endpoint is available, set trafficDistribution: PreferSameNode on the Service.
Keep traffic on the same node
Add trafficDistribution: PreferSameNode to your Service:
apiVersion: v1
kind: Service
metadata:
name: my-service
spec:
selector:
app: my-app
trafficDistribution: PreferSameNode
ports:
- port: 80
targetPort: 8080
Requests then go only to endpoints on the same node as the client. If that node has no ready endpoint, traffic goes to any available endpoint of the Service. This setting is a preference, not a guarantee.
The same behavior applies to traffic coming through a NodePort. In this case, the node receiving the connection is treated as the client.
Run endpoints on the nodes that need them
trafficDistribution does not account for load. If a node has many clients but only one endpoint, all requests from those clients go to that endpoint, even when other nodes have idle endpoints.
To distribute requests across more endpoints, run an endpoint on every node that has clients. You can use a DaemonSet to do this.
Avoid zone-based routing
Kubernetes also supports PreferSameZone, PreferClose and the service.kubernetes.io/topology-mode: Auto annotation. These settings prefer endpoints on nodes with the same topology.kubernetes.io/zone label.
PreferClose is the older name for PreferSameZone.
However, zone labels on Syself Autopilot do not represent consistent physical zones:
| Node | Zone label | Example in Falkenstein |
|---|---|---|
| Cloud node | A fixed name for each location | fsn1-dc14 |
| Bare-metal node | The location name | fsn1 |
As a result, Kubernetes treats cloud and bare-metal nodes in the same location as being in different zones.
Use PreferSameNode instead. It compares node names rather than zone labels. If your Service uses a zone-based setting, remove it or replace it with PreferSameNode.
Check Service endpoint hints
Kubernetes stores routing hints in the Service's EndpointSlices. Run the following command to see each endpoint's node, the node it serves and the zone it serves:
$ kubectl get endpointslices -n <namespace> -l kubernetes.io/service-name=<service> \
-o jsonpath='{range .items[*].endpoints[*]}{.nodeName}{"\t"}{.hints.forNodes[*].name}{"\t"}{.hints.forZones[*].name}{"\n"}{end}'
With PreferSameNode, the second column shows the endpoint's node name. If the second and third columns are empty, the Service has no routing hints and traffic can go to any endpoint.
Pass TLS through at Layer 4
Terminate TLS inside your pod instead of at the ingress by passing raw TCP or TLS straight through the load balancer.
Install Traefik for HTTP routing
Place many HTTP applications behind one Hetzner load balancer with Traefik. The platform includes no ingress controller, so you install the one you prefer.