<p>I'd like to launch a clustered Socket IO application in Kubernetes. When I create a service (whether NodePort or LoadBalancer) the client application keeps getting disconnected and it reconnects again with the following logs:</p>
<pre><code>undefined
oah4g28zZCw36g1MAAAm
undefined
undefined
oac4g28zZCw36g1MFAAAx
undefined
</code></pre>
<p>and this happens rapidly.</p>
<p>However, when I connect to a single Pod directly, the problem goes away and the connection becomes stable.</p>
<p>How I am creating the service is by the following command:</p>
<pre><code>kubectl expose deployment xxx --type=LoadBalancer --port=80 --target-port=3000
</code></pre>
<p>I know that something such as a KeepAlive or Timeout configuration is missing in the service, but how can I add those or better said properly configure the service for Socket IO?</p>

<p>You can use the <code>sessionAffinity: ClientIP</code>, which will manage the session from K8s service.</p>
<pre><code>kind: Service
apiVersion: v1
metadata:
  name: example
spec:
  selector:
    app: example
  ports:
  - name: http
    protocol: TCP
    port: 80
    targetPort: 80
  sessionAffinity: ClientIP
  sessionAffinityConfig:
    clientIP:
      timeoutSeconds: 1800
</code></pre>
<p>just for ref : <a href="https://stackoverflow.com/questions/41124840/does-the-ws-websocket-server-library-requires-sticky-session-when-it-is-used-beh">Does the ws websocket server library requires sticky session when it is used behind a load balancer?</a></p>
