Session affinity keeps repeated requests from one client associated with the same backend instance. Cookie-based affinity does this with an HTTP cookie, so it requires an application-layer HTTP component rather than a transport-layer Service. section 12.4.1 section 11.1.3
Distinguish the two Kubernetes mechanisms:
- A Service defaults to None, distributing connections without guaranteeing the destination pod; ClientIP instead keeps connections from one IP on the same pod for a configurable period. section 11.1.3
- An Ingress operates at HTTP layer 7 and can read cookies, but its affinity annotations are specific to the chosen controller. section 12.4.1
Enable affinity in an Nginx Ingress
Add nginx.ingress.kubernetes.io/affinity: cookie to enable cookie routing and nginx.ingress.kubernetes.io/session-cookie-name: SESSION_COOKIE to choose the cookie name. An initial request returns Set-Cookie; send that value in a subsequent Cookie header, and repeated requests are forwarded to the same pod. section 12.4.1
$ kubectl exec -it quote-001 -c nginx -- sh / #
Use the manifest listing to inspect the complete networking.k8s.io/v1 Ingress configuration, including both affinity annotations. section 12.4.1
/ # curl http://10.96.136.190 #1 This is the quiz service running in pod quiz / # curl http://10.96.74.151 #2 This is the quote service running in pod quote-canary
Use the request listings to see the returned session cookie and replay it through the Cookie header. section 12.4.1

