Course contentsShow
← Node.js
Lesson 1164 of 1,98010. Real-time Communication and WebsocketsFree lesson

Deploying WebSocket Clusters on Kubernetes

You will configure ingress controllers to handle long-lived WebSocket connections in a containerized environment.

In this lesson, you will learn how to configure Kubernetes Ingress controllers to maintain persistent WebSocket connections across a distributed cluster of Node.js pods.

When you scale your WebSocket application across multiple pods, the biggest challenge is that HTTP is stateless, but WebSockets are stateful. While your application code handles the pub/sub logic using tools like Redis (as discussed in lesson 22), the network layer must ensure that the initial "Upgrade" handshake succeeds and that the connection remains open without being prematurely dropped by load balancers or proxy timeouts.

Configuring the Ingress Controller

Standard Kubernetes Ingress controllers, such as NGINX, often have default timeouts that close "idle" connections. Since WebSockets are long-lived, you must explicitly increase these timeouts to prevent the Ingress from killing your connections.

You apply these settings using annotations on your Ingress resource:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: socket-ingress
  annotations:
    nginx.ingress.kubernetes.io/proxy-read-timeout: "3600"
    nginx.ingress.kubernetes.io/proxy-send-timeout: "3600"
    nginx.ingress.kubernetes.io/proxy-buffering: "off"
spec:
  rules:
  - host: sockets.example.com
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: socket-service
            port:
              number: 3000

Why these settings matter:

  1. proxy-read/send-timeout: Sets the duration the proxy waits for a response. By increasing this to 3600 seconds (1 hour), you stop the Ingress from dropping a connection just because there hasn't been a message sent in a few minutes.
  2. proxy-buffering: "off": This is critical for WebSockets. If buffering is on, the proxy waits for a buffer to fill before sending data to the client, which introduces latency and breaks the "real-time" nature of your socket events.

By configuring these timeouts at the Ingress level, you ensure that your Node.js pods can hold thousands of open connections without the infrastructure layer interfering with the underlying TCP stream. Once the Ingress is tuned, your application remains responsible for managing connection state and handling heartbeats to keep the connection healthy.

Key Takeaway: To successfully deploy WebSocket clusters, you must disable proxy buffering and extend connection timeouts in your Ingress annotations to ensure long-lived, persistent streams remain uninterrupted by the infrastructure.