ClientIP affinity keys on the source address each node sees, keeps per-node state and slides a 3-hour default timeout. Why ingress, NAT and spreading defeat it.
Sticky sessions that did not stick, and pods that sat idle
The order service at Belgavi's Book Store keeps a short-lived checkout cache in memory, so Hansini set sessionAffinity: ClientIP on its Kubernetes Service to send each shopper back to the same pod. Two complaints followed within a week. Some shoppers still lost their basket between requests. And of the four pods, two received almost no traffic while the other two ran hot.
Both symptoms come from the same three facts about how kube-proxy implements ClientIP affinity: what address it keys on, where it keeps the table, and how its timeout behaves. This guide explains each from the Kubernetes documentation and the kube-proxy source at v1.37.2, models the consequences with a small simulation, and lists what to check before relying on it. Nothing here was measured on a cluster; the simulation encodes the rules as read from the source.
What the API gives you
A Service has .spec.sessionAffinity, None by default or ClientIP. With ClientIP, .spec.sessionAffinityConfig.clientIP.timeoutSeconds sets how long the stickiness lasts. The API defaults it to 10800 seconds, three hours, when it is not set, and validation rejects values above 86400, one day. The Virtual IPs documentation adds that setting the timeout is not supported on Windows nodes.
apiVersion: v1
kind: Service
metadata:
name: orders
spec:
selector: {app: orders}
ports: [{port: 80, targetPort: 8080}]
sessionAffinity: ClientIP
sessionAffinityConfig:
clientIP:
timeoutSeconds: 1800 # 30 minutes instead of the 3-hour defaultHow kube-proxy implements it
In iptables mode, each endpoint gets its own chain. Traffic that enters an endpoint chain is recorded with the iptables recent match, -m recent --name <endpoint chain> --set. Before the random endpoint choice, the Service chain has one rule per endpoint, -m recent --name <endpoint chain> --rcheck --seconds <timeout> --reap, that jumps straight to that endpoint if the packet's source address was seen there within the timeout. In nftables mode the same idea uses one set per endpoint, created with a timeout, and an update @set { ip saddr } rule in the endpoint chain. A comment in the nftables proxier states the key exactly: affinity is recorded for connections "from a particular source IP (without regard to source port) to a particular ServicePort (without regard to which service IP was used to reach the service)".
Three consequences follow.
- The key is the source address as the node sees it. Not a cookie, not a user, not a TCP connection.
- The table lives in each node's kernel. Every node running kube-proxy keeps its own
recentlists or nftables sets. Two nodes know nothing about each other's entries. - The timeout slides. Each new connection through an endpoint chain refreshes the entry, so the timeout is measured from the client's latest new connection, not from its first. A client that connects more often than the timeout never moves.
Only new connections go through these rules. Packets of an established connection follow connection tracking, so affinity is about which pod a client's next connection lands on.
Where the source address goes wrong
Behind an ingress or any L7 proxy. If shoppers reach the order Service through ingress-controller pods, kube-proxy sees the ingress pods' addresses, not the shoppers'. With two ingress pods there are two affinity keys, so at most two backends receive traffic, and each is pinned to its ingress pod for as long as connections keep coming. Basket stickiness, if needed, belongs in the ingress layer, which can see cookies.
Behind a cloud load balancer that proxies. The Kubernetes source-IP tutorial notes that only some cloud providers preserve the client source address. Where the balancer proxies the connection, every client arrives with the balancer's address.
Through a NodePort or LoadBalancer with externalTrafficPolicy: Cluster. The receiving node may forward to an endpoint on another node and replace the source address with its own. Affinity is still decided on the receiving node before that rewrite, but the external balancer can spread one client across several nodes, and each node keeps its own table. The same client then has an independent entry on each node and can be pinned to different pods depending on which node the balancer chose. externalTrafficPolicy: Local keeps the client address and only uses endpoints on the receiving node.
A whole office behind one NAT address. Every shopper behind it shares one key and one pod.
A simulation of the consequences
The companion example encodes those rules: a table per node keyed by source address, a sliding timeout, and a seeded hash standing in for kube-proxy's random choice of endpoint. Four backend pods, orders-0 to orders-3.
| Scenario | Connections | Affinity keys | Idle pods | Busiest pod share | Sources split across pods |
|---|---|---|---|---|---|
| 40 in-cluster clients, direct | 400 | 40 | 0 | 0.375 | 0 |
| same requests through 2 ingress pods | 400 | 2 | 2 | 0.500 | 0 |
| one NAT address, balancer spreads over 3 nodes | 30 | 1 | 2 | 0.667 | 1 |
| one client every 2 hours, default timeout | 12 | 1 | 3 | 1.000 | 0 |
| one client every 2 hours, 1-hour timeout | 12 | 1 | 0 | 0.333 | 1 |
The second row is the idle-pod complaint: two keys can never use four pods. The third row is the lost-basket complaint: one client, three node tables, and in this run two different pods. The last two rows show the sliding timeout. With the three-hour default and a connection every two hours, the entry never expires and the client stays on one pod for the whole day. With a one-hour timeout the entry expires before each new connection and the client is re-picked 11 times. Even direct clients do not spread evenly: with only 40 keys the busiest pod took 37.5 percent of connections instead of 25, because affinity balances keys, not load.
How to verify on a real cluster
- Confirm the proxy mode with
curl http://localhost:10249/proxyModeon a node, as the source-IP tutorial shows. - In iptables mode, list the Service's rules with
iptables-save -t nat | grep <service name>and look forrecentwith--rcheck --seconds. The recorded entries are in/proc/net/xt_recent/, one file per endpoint chain, on each node. - In nftables mode, list the
affinity-sets withnft list table ip kube-proxyand check their timeout and elements. - Log the source address the backend pods see. If it is a handful of ingress or node addresses, ClientIP affinity cannot do what you want.
Choosing the timeout
Size the timeout from the state it protects. A checkout cache that lives 20 minutes needs about 20 minutes of stickiness, not three hours. A long timeout makes rebalancing slower after a scale-out, because existing clients stay where they are until they pause longer than the timeout. A short one sends clients to new pods more often. If losing affinity breaks correctness rather than just making things slower, the state does not belong in pod memory: move it to a shared store, and use affinity, if at all, as a cache-hit optimisation.
Failure modes
Assuming affinity survives pod restarts. When an endpoint disappears its chain or set goes with it, and its clients are re-picked.
Assuming it is per user. It is per source address per node.
Mixing affinity with topology-aware routing or internalTrafficPolicy: Local. These narrow the set of endpoints a node can choose from, and the affinity entry only applies among them.
Testing from one laptop. A single test client always looks perfectly sticky. Test with many source addresses and through the same path production traffic takes.
Verification
The companion tests check the per-node behaviour, the sliding timeout boundary, the idle pods behind two proxies, and the API's timeout bounds. They model the documented rules and the source; run the commands above on your own nodes before deciding.