Sub topic 14
Session affinity and traffic policies
This chapter assumes you can write ClusterIP, NodePort and LoadBalancer Services, and that you know how EndpointSlices and kube-proxy pick backend Pods. By default, a Service spreads connections across all ready Pods with no regard for who the client is or where the Pod runs. Three Service settings change that.
Session affinity
Setting sessionAffinity: ClientIP makes kube-proxy send connections from the same client IP to the same Pod. The affinity entry expires after timeoutSeconds of inactivity. The default is 10800 seconds (3 hours).
apiVersion: v1
kind: Service
metadata:
name: web-sticky
spec:
selector:
app: web
sessionAffinity: ClientIP
sessionAffinityConfig:
clientIP:
timeoutSeconds: 3600
ports:
- name: http
port: 80
targetPort: httpAffinity has these limits:
- It uses only the source IP. Cookie-based stickiness needs an Ingress controller or Gateway implementation that supports it.
- Clients behind one NAT gateway share an IP, so they all land on one Pod.
- If the Pod leaves the endpoint list, for example when it fails readiness, clients are remapped.
Traffic policies
Two fields control which endpoints a Service may use.
| Field | Applies to | Values | Effect of `Local` |
|---|---|---|---|
| `externalTrafficPolicy` | NodePort and LoadBalancer traffic | `Cluster` (default), `Local` | Only Pods on the node that received the traffic are used. |
| `internalTrafficPolicy` | Traffic from inside the cluster | `Cluster` (default), `Local` | Only Pods on the client's own node are used. |
With externalTrafficPolicy: Local, kube-proxy does not forward to another node. This keeps the client source IP, because no extra source NAT hop is needed, and it saves a network hop. The cost is uneven load, because nodes with more Pods get the same share of traffic as nodes with fewer. For a LoadBalancer Service, Kubernetes allocates a healthCheckNodePort. The cloud load balancer probes it and sends traffic only to nodes that have a ready local Pod.
apiVersion: v1
kind: Service
metadata:
name: web-public-local
spec:
type: LoadBalancer
externalTrafficPolicy: Local
selector:
app: web
ports:
- name: http
port: 80
targetPort: httpWith internalTrafficPolicy: Local, a node that has no ready local endpoint drops the traffic. It does not fall back to other nodes.
Topology-aware routing
spec.trafficDistribution: PreferClose asks kube-proxy to prefer endpoints in the same zone as the client when any exist. Unlike Local, it falls back to other endpoints. Check that your cluster version supports the field.
Choosing a setting
- Need the client IP in your application? Use
externalTrafficPolicy: Local. - Need the same client to reach the same Pod? Use
sessionAffinity: ClientIP, or move stickiness to the Ingress layer. - Want to reduce cross-zone traffic without risking dropped requests? Use
trafficDistribution: PreferClose.
Exercise
A LoadBalancer Service uses externalTrafficPolicy: Local. The Deployment has 3 replicas, all scheduled on node A. A cluster has nodes A, B and C. What happens to a request the load balancer sends to node B, and how does the load balancer avoid this?
Show answer
Node B has no local ready endpoint, so it would drop the request. The load balancer probes the Service's healthCheckNodePort on each node. Nodes B and C fail the probe, so the load balancer sends traffic only to node A.