sticky sessions kubernetes service with apache and ingress

10/16/2019

I have an k8s Apache server which uses mod_jk to connect to different kubernetes services. mod_jk is sort of dummy here as workers are having k8s service URL. I am now trying to achieve sticky sessions based on JESSIONID in the cookie. I have traefik ingress controller which directs all requests to k8s Apache service. The requests is terminated at ingress level for TLS. What is the best way to achieve sticky session ?

I tried enabling sessionAffinity: ClientIP, but the client IP is always same. It is the ingress controller.

-- Matt
kubernetes
pod
service

1 Answer

10/22/2019

In the Kubernetes ingress object in the annotations label you have to define, which kind of Ingress will you use, so in your case: Traefik ingress controller. Please note, that the sticky session in Traefik is defined in the Service object with the annotation. Instead of a random cookie name, we define it as JSESSIONID.

Ingress object definition:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  annotations:
    kubernetes.io/ingress.class: traefik
  labels:
    app: session-affinity
  name: session-affinity
spec:
  tls:
  - host: <address>
    secretName:
  rules:
  - host: <address>
    http:
      paths:
      - path: /
        backend:
          serviceName: session-affinity
          servicePort: 8080

Service object definition:

apiVersion: v1
kind: Service
metadata:
  name: session-affinity
  labels:
    app: session-affinity
  annotations:
    traefik.ingress.kubernetes.io/affinity: "true"
    traefik.ingress.kubernetes.io/session-cookie-name: "JSESSIONID"
spec:
  type: NodePort
  ports:
  - port: 8080
    targetPort: 8080

You can find more information in documentation.

-- muscat
Source: StackOverflow