sticky session - Almax84/consulting-wiki GitHub Wiki

Internal services can have sticky sessions by providing just one parameter in their manifest spec.sessionAffinity: ClientIP

kind: Service
apiVersion: v1
metadata:
  name: my-service
spec:
  selector:
    app: my-app
  ports:
  - name: http
    protocol: TCP
    port: 80
    targetPort: 80
  sessionAffinity: ClientIP

If you also want a route with session affinity:

  1. expose the route
  2. annotate the route with metadata.annotations.router.openshift.io/cookie_name: sticky #or any other cookie name
  3. the client must call the route and save the cookie ex. curl -s -o /dev/null --cookie-jar cookies.txt http://myspringboot-rai-pod-session-affinity.apps.lab02.gpslab.club/greeting
  4. call the route with a simple curl curl --cookie cookies.txt http://myspringboot-rai-pod-session-affinity.apps.lab02.gpslab.club/greeting

https://dev.to/ckaserer/sticky-session-in-kubernetes-hello-world-2kmo

OpenShift Container Platform provides sticky sessions, which enables stateful application traffic by ensuring all traffic hits the same endpoint. However, if the endpoint Pod terminates, whether through restart, scaling, or a change in configuration, this statefulness can disappear.

OpenShift Container Platform can use cookies to configure session persistence. The Ingress controller selects an endpoint to handle any user requests, and creates a cookie for the session. The cookie is passed back in the response to the request and the user sends the cookie back with the next request in the session. The cookie tells the Ingress Controller which endpoint is handling the session, ensuring that client requests use the cookie so that they are routed to the same Pod.

https://access.redhat.com/documentation/en-us/openshift_container_platform/4.1/html/networking/configuring-routes#nw-using-cookies-keep-route-statefulness_route-configuration paragrafo 9.14