<p>I have an k8s Apache server which uses <code>mod_jk</code> to connect to different kubernetes services. <code>mod_jk</code> is sort of dummy here as workers are having k8s service URL. I am now trying to achieve sticky sessions based on <code>JESSIONID</code> 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 ?</p>

<p>I tried enabling <code>sessionAffinity: ClientIP</code>, but the client IP is always same. It is the ingress controller.</p>

Matt
<p>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 <code>JSESSIONID</code>.</p>

<p>Ingress object definition:</p>

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

<p>Service object definition:</p>

<pre><code>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
</code></pre>

<p>You can find more information in <a href="https://docs.traefik.io/v1.7/configuration/backends/kubernetes/" rel="nofollow noreferrer">documentation</a>.</p>
