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:
undefined
oah4g28zZCw36g1MAAAm
undefined
undefined
oac4g28zZCw36g1MFAAAx
undefined
and this happens rapidly.
However, when I connect to a single Pod directly, the problem goes away and the connection becomes stable.
How I am creating the service is by the following command:
kubectl expose deployment xxx --type=LoadBalancer --port=80 --target-port=3000
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?
CodePudding user response:
You can use the sessionAffinity: ClientIP
, which will manage the session from K8s service.
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
just for ref : Does the ws websocket server library requires sticky session when it is used behind a load balancer?