title: Sticky sessions on Kubernetes cluster
body: <p>Currently, I'm trying to create a Kubernetes cluster on Google Cloud with two <strong>load balancers</strong>: one for backend (in Spring boot) and another for frontend (in Angular), where each service (load balancer) communicates with 2 replicas (pods). To achieve that, I created the following ingress:</p>
  
  <pre><code>apiVersion: extensions/v1beta1
  kind: Ingress
  metadata:
    name: sample-ingress
  spec:
    rules:
      - http:
          paths:
            - path: /rest/v1/*
              backend:
                serviceName: sample-backend
                servicePort: 8082
            - path: /*
              backend:
                serviceName: sample-frontend
                servicePort: 80
  </code></pre>
  
  <p>The ingress above mentioned can make the frontend app communicate with the REST API made available by the backend app. However, I have to create <strong>sticky sessions</strong>, so that every user communicates with the same POD because of the authentication mechanism provided by the backend. To clarify, if one user authenticates in POD #1, the cookie will not be recognized by POD #2.</p>
  
  <p>To overtake this issue, I read that the <strong>Nginx-ingress</strong> manages to deal with this situation and I installed through the steps available here: <a href="https://kubernetes.github.io/ingress-nginx/deploy/" rel="noreferrer">https://kubernetes.github.io/ingress-nginx/deploy/</a> using Helm. </p>
  
  <p>You can find below the diagram for the architecture I'm trying to build:</p>
  
  <p><a href="https://i.stack.imgur.com/iZeHX.png" rel="noreferrer"><img src="https://i.stack.imgur.com/iZeHX.png" alt="enter image description here"></a></p>
  
  <p>With the following services (I will just paste one of the services, the other one is similar):</p>
  
  <pre><code>apiVersion: v1
  kind: Service
  metadata:
    name: sample-backend
  spec:
    selector:
      app: sample
      tier: backend
    ports:
      - protocol: TCP
        port: 8082
        targetPort: 8082
    type: LoadBalancer
  </code></pre>
  
  <p>And I declared the following ingress:</p>
  
  <pre><code>apiVersion: extensions/v1beta1
  kind: Ingress
  metadata:
    name: sample-nginx-ingress
    annotations:
      kubernetes.io/ingress.class: "nginx"
      nginx.ingress.kubernetes.io/affinity: cookie
      nginx.ingress.kubernetes.io/affinity-mode: persistent
      nginx.ingress.kubernetes.io/session-cookie-hash: sha1
      nginx.ingress.kubernetes.io/session-cookie-name: sample-cookie
  spec:
    rules:
      - http:
          paths:
            - path: /rest/v1/*
              backend:
                serviceName: sample-backend
                servicePort: 8082
            - path: /*
              backend:
                serviceName: sample-frontend
                servicePort: 80
  </code></pre>
  
  <p>After that, I run <code>kubectl apply -f sample-nginx-ingress.yaml</code> to apply the ingress, it is created and its status is OK. However, when I access the URL that appears in "Endpoints" column, the browser can't connect to the URL.
  Am I doing anything wrong?</p>
  
  <h1>Edit 1</h1>
  
  <p>** Updated service and ingress configurations **</p>
  
  <p>After some help, I've managed to access the services through the Ingress Nginx. Above here you have the configurations:</p>
  
  <h2>Nginx Ingress</h2>
  
  <p>The paths shouldn't contain the "<em>", unlike the default Kubernetes ingress that is mandatory to have the "</em>" to route the paths I want.</p>
  
  <pre><code>apiVersion: extensions/v1beta1
  kind: Ingress
  metadata:
    name: sample-ingress
    annotations:
      kubernetes.io/ingress.class: "nginx"
      nginx.ingress.kubernetes.io/affinity: "cookie"
      nginx.ingress.kubernetes.io/session-cookie-name: "sample-cookie"
      nginx.ingress.kubernetes.io/session-cookie-expires: "172800"
      nginx.ingress.kubernetes.io/session-cookie-max-age: "172800"
  
  spec:
    rules:
      - http:
          paths:
            - path: /rest/v1/
              backend:
                serviceName: sample-backend
                servicePort: 8082
            - path: /
              backend:
                serviceName: sample-frontend
                servicePort: 80
  </code></pre>
  
  <h2>Services</h2>
  
  <p>Also, the services shouldn't be of type "LoadBalancer" but "<strong>ClusterIP</strong>" as below:</p>
  
  <pre><code>apiVersion: v1
  kind: Service
  metadata:
    name: sample-backend
  spec:
    selector:
      app: sample
      tier: backend
    ports:
      - protocol: TCP
        port: 8082
        targetPort: 8082
    type: ClusterIP
  </code></pre>
  
  <p>However, I still can't achieve sticky sessions in my Kubernetes Cluster, once I'm still getting 403 and even the cookie name is not replaced, so I guess the annotations are not working as expected.</p>