How do you handle WebSocket connections in a Kubernetes environment?

Answer

Running WebSocket servers in Kubernetes (K8s) introduces specific challenges. (1) Sticky sessions — use NGINX Ingress with nginx.ingress.kubernetes.io/affinity: "cookie" annotation to ensure WebSocket clients reconnect to the same pod; (2) Pod restarts — K8s rolling updates terminate pods; clients must detect disconnection and reconnect to a new pod. Configure terminationGracePeriodSeconds generously (60-300s) to allow graceful connection draining; (3) HPA scaling — standard CPU/memory-based HPA doesn't work well for WebSocket workloads; use custom metrics (active connections per pod) with KEDA (Kubernetes Event Driven Autoscaling); (4) Service mesh — Istio and Linkerd support WebSocket with circuit breaking and observability via Envoy sidecar; (5) Redis adapter — mandatory for cross-pod communication; use Redis Sentinel or Redis Cluster for high availability; (6) Health checks — liveness probes must not close WebSocket connections; use a separate HTTP endpoint for health checks.