Course contentsShow
← C#
Lesson 1859 of 1,90817. Deployment and Cloud ArchitectureFree lesson

Configuring Cluster Ingress for WebSocket and gRPC

Enable and configure ingress rules to support persistent connections like gRPC and WebSockets.

In this lesson, you will learn how to configure your Kubernetes Ingress controller to handle long-lived, stateful connections like WebSockets and gRPC, which require specific protocols beyond standard HTTP/1.1.

The Challenge of Persistent Connections

Standard Ingress configurations are often optimized for short-lived HTTP requests. WebSockets rely on an initial HTTP "Upgrade" handshake, while gRPC operates strictly over HTTP/2. If your Ingress controller is not explicitly configured to support these protocols, the persistent connections will be terminated prematurely, or the binary frames of gRPC will be incorrectly parsed.

Configuring the Ingress

Most modern Ingress controllers (like NGINX) require explicit annotations to handle these persistent streams. Without these, the controller may default to closing idle connections or failing to recognize the upgrade header.

To enable this for your .NET microservices, you update your Kubernetes Ingress manifest with the following annotations:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: api-ingress
  annotations:
    # Essential for gRPC (HTTP/2)
    nginx.ingress.kubernetes.io/backend-protocol: "GRPC"
    # Essential for WebSockets
    nginx.ingress.kubernetes.io/proxy-read-timeout: "3600"
    nginx.ingress.kubernetes.io/proxy-send-timeout: "3600"
spec:
  rules:
  - host: myapp.example.com
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: my-grpc-or-websocket-service
            port:
              number: 5000

Why These Settings Matter

  1. backend-protocol: "GRPC": This instructs the Ingress controller to treat the backend as a gRPC-capable service, ensuring it uses HTTP/2 correctly. Without this, the Ingress might attempt to communicate via HTTP/1.1, causing the gRPC handshake to fail.
  2. proxy-read/send-timeout: WebSockets are long-lived. If you do not increase these timeouts, the Ingress controller will view the connection as "idle" and terminate it after the default interval (usually 60 seconds), breaking the persistent socket.

By applying these configurations, you ensure that your .NET services can maintain stable, high-performance connections with clients, allowing your real-time updates and binary RPC calls to function seamlessly across your cluster boundaries.

Key Takeaway: To support WebSockets and gRPC in Kubernetes, you must explicitly configure your Ingress controller via annotations to use the correct backend protocol and increase connection timeouts to prevent premature termination.