Difference between ingress and service's kubernetes session affinity

1.3k Views Asked by At

What are the differences between using an nginx ingress with the session affinity annotations (as below example) and a service with the service.spec.sessionAffinity set to ClientIP.

From what I understand, both reach the same goal but I wonder if there are some differences other than the primer being done at ingress level with a cookie while second operates at the service level using the clients IP. Also, would that be a bad idea to use both a the same time ?

And last question, If I want to enable sticky session between pods within the cluster, would the service.spec.sessionAffinity: ClientIP definition would be enough to ensure a backend pod connects everytime to the same database pod ?

Ingress example :

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/rewrite-target: /
    nginx.ingress.kubernetes.io/affinity: cookie
    nginx.ingress.kubernetes.io/affinity-mode: persistent
    nginx.ingress.kubernetes.io/session-cookie-change-on-failure: "true"
    nginx.ingress.kubernetes.io/session-cookie-expires: "172800"
    nginx.ingress.kubernetes.io/session-cookie-max-age: "172800"
    nginx.ingress.kubernetes.io/session-cookie-name: STICKY_SESSION
    nginx.ingress.kubernetes.io/use-regex: "false"

From the Kubernetes' service API documentation :

sessionAffinity : Supports "ClientIP" and "None". Used to maintain session affinity. Enable client IP based session affinity. Must be ClientIP or None. Defaults to None. More info: https://kubernetes.io/docs/concepts/services-networking/service/#virtual-ips-and-service-proxies

0

There are 0 best solutions below