Configure a sticky session

Sticky sessions enable users who participate in split testing to consistently see a particular feature. Adding sticky sessions to the initial request forces NGINX Ingress Controller to route follow-up requests to the same Pod.

  1. Enable the sticky session in the Kubernetes Ingress resource:

    nginx.ingress.kubernetes.io/affinity: "cookie"
    
  2. Specify the name of the required cookie (default: INGRESSCOOKIE).

    nginx.ingress.kubernetes.io/session-cookie-name: "<cookie-name>"
    
  3. Specify the time before the cookie expires (in seconds):

    nginx.ingress.kubernetes.io/session-cookie-max-age: "<cookie-duration>"
    

The following is an example of a Kubernetes Ingress configuration file with a sticky session enabled:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: sticky-session-test
  annotations:
    nginx.ingress.kubernetes.io/affinity: "cookie"
    nginx.ingress.kubernetes.io/session-cookie-name: "route"
    nginx.ingress.kubernetes.io/session-cookie-max-age: "172800"

spec:
  rules:
  - host: stickyingress.example.com
    http:
      paths:
      - backend:
          serviceName: http-svc
          servicePort: 80
        path: /

Note

NGINX Ingress Controller only supports cookie-based sticky sessions.

Sticky sessions in the NGINX Ingress Controller documentation.