<p>I'm trying to run a socket.io app using Google Container Engine. I've setup the ingress service which creates a Google Load Balancer that points to the cluster. If I have one pod in the cluster all works well. As soon as I add more, I get tons of socket.io errors. It looks like the connections end up going to different pods in the cluster and I suspect that is the problem with all the polling and upgrading socket.io is doing.</p>

<p>I setup the load balancer to use sticky sessions based on IP. </p>

<p>Does this only mean that it will have affinity to a particular NODE in the kubernetes cluster and not a POD?</p>

<p>How can I set it up to ensure session affinity to a particular POD in the cluster?</p>

<p>NOTE: I manually set the sessionAffinity on the cloud load balancer.
<a href="https://i.stack.imgur.com/oZcNd.png" rel="noreferrer"><img src="https://i.stack.imgur.com/oZcNd.png" alt="enter image description here"></a></p>

<p>Here would be my ingress yaml</p>

<pre><code>apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: my-ingress
  annotations:
    kubernetes.io/ingress.global-static-ip-name: my-static-ip
spec:
  backend:
    serviceName: my-service
    servicePort: 80
</code></pre>

<h1>Service</h1>

<pre><code>apiVersion: v1
kind: Service
metadata:
  name: my-service
  labels:
    app: myApp
spec:
  sessionAffinity: ClientIP
  type: NodePort
  ports:
    - port: 80
      targetPort: http-port
  selector:
    app: myApp
</code></pre>

<p>First off, you need to set "sessionAffinity" at the <code>Ingress</code> resource level, not your load balancer (this is only related to a specific node in the target group):</p>

<p>Here is an example <code>Ingress</code> spec:</p>

<pre><code>apiVersion: extensions/v1beta1  
kind: Ingress  
metadata:  
  name: nginx-test-sticky
  annotations:
    kubernetes.io/ingress.class: "nginx"
    nginx.ingress.kubernetes.io/affinity: "cookie"
    nginx.ingress.kubernetes.io/session-cookie-name: "route"
    nginx.ingress.kubernetes.io/session-cookie-hash: "sha1"
spec:
  rules:
  - host: $HOST
    http:
      paths:
      - path: /
        backend:
          serviceName: $SERVICE_NAME
          servicePort: $SERVICE_PORT
</code></pre>

<p>Second, you probably need to tune your <code>ingress-controller</code> to allow longer connection times. Everything else, by default, supports websocket proxying.</p>

<p>If you are still having issues please provide outputs for <code>kubectl describe -oyaml pod/&lt;ingress-controller-pod&gt;</code> and <code>kubectl describe -oyaml ing/&lt;your-ingress-name&gt;</code></p>

<p>Hope this helps, good luck!</p>
