Kubernetes - SignalR Behind Ingress Connection Disconnected With 1006

Viewed 146

i have a simple signalr application running on Kubernetes/Docker Container.

Which is really a Ping/Pong at this point. When i have it on local Docker it works perfectly.

But when i publish it via Jenkins to a Kubernetes cluster, it connects, handshake is successful, but heartbeat is never sent/recieved and disconnects with Error 1006.

<html>
    <head>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/microsoft-signalr/6.0.1/signalr.js"></script>
    </head>
    <body>
        <script type="text/javascript">
            const connection = new signalR.HubConnectionBuilder()
                .withUrl("http://localhost:8081/ChatHub")
                .configureLogging(signalR.LogLevel.Trace)
                .build();

            async function start() {
                try {
                    await connection.start({jsonp: true});
                    console.log("SignalR Connected.");
                } catch (err) {
                    console.log(err);
                    setTimeout(start, 5000);
                }
            };

            connection.onclose(async () => {
                await start();
            });

            // Start the connection.
            start();
        </script>
    </body>
</html>

[2022-04-18T17:37:22.313Z] Debug: Starting HubConnection.
[2022-04-18T17:37:22.314Z] Debug: Starting connection with transfer format 'Text'.
[2022-04-18T17:37:22.314Z] Debug: Sending negotiation request: http://localhost:8081/ChatHub/negotiate?negotiateVersion=1.
[2022-04-18T17:37:22.335Z] Debug: Selecting transport 'WebSockets'.
[2022-04-18T17:37:22.336Z] Trace: (WebSockets transport) Connecting.
[2022-04-18T17:37:22.344Z] Information: WebSocket connected to ws://localhost:8081/ChatHub?id=O5dgUQ2Tov6pEuy_sLYE-w.
[2022-04-18T17:37:22.345Z] Debug: The HttpConnection connected successfully.
[2022-04-18T17:37:22.346Z] Debug: Sending handshake request.
[2022-04-18T17:37:22.347Z] Trace: (WebSockets transport) sending data. String data of length 32.
[2022-04-18T17:37:22.347Z] Information: Using HubProtocol 'json'.
[2022-04-18T17:37:22.354Z] Trace: (WebSockets transport) data received. String data of length 3.
[2022-04-18T17:37:22.355Z] Debug: Server handshake complete.
[2022-04-18T17:37:22.355Z] Debug: HubConnection connected successfully.
SignalR Connected.

But it gets complicated when i put it on the live, Behind Ingress. SignalR Connects, gets and error and i reconnect via catch block and connects and gets disconnected again and again loop continues...

You can test here:

<html>
    <head>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/microsoft-signalr/6.0.1/signalr.js"></script>
    </head>
    <body>
        <script type="text/javascript">
            const connection = new signalR.HubConnectionBuilder()
                .withUrl("https://streamt.acente365.com/ChatHub")
                .configureLogging(signalR.LogLevel.Trace)
                .build();

            async function start() {
                try {
                    await connection.start({jsonp: true});
                    console.log("SignalR Connected.");
                } catch (err) {
                    console.log(err);
                    setTimeout(start, 5000);
                }
            };

            connection.onclose(async () => {
                await start();
            });

            // Start the connection.
            start();
        </script>
    </body>
</html>

My Dockerfile as follows:

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
WORKDIR /src
COPY ["CoreHub.csproj", ""]
RUN dotnet restore "./CoreHub.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "CoreHub.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "CoreHub.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "CoreHub.dll"]

My Service.yml:

---
apiVersion: v1
kind: Service
metadata:
  name: streamt-acente365-svc
  namespace: acente365-microservice
spec:
  selector:
    app: streamt-acente365-app
  ports:
  - protocol: TCP
    name: http
    port: 80
    targetPort: 80
  type: ClusterIP
  sessionAffinity: ClientIP
  sessionAffinityConfig:
    clientIP:
      timeoutSeconds: 10800

My Ingress.yml as follows:

---
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: streamt-acente365-virtualhost
  namespace: acente365-microservice
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/ssl-redirect: "false"
    nginx.ingress.kubernetes.io/affinity: cookie
    nginx.ingress.kubernetes.io/session-cookie-hash: sha1
    nginx.ingress.kubernetes.io/session-cookie-name: REALTIMESERVERID
spec:
  rules:
  - host: streamt.acente365.com
    http:
      paths:
      - path: /
        backend:
          serviceName: streamt-acente365-svc
          servicePort: 80
  tls:
  - hosts:
    - streamt.acente365.com
    secretName: wildcard-acente365-com

I tried all the answers on StackOverflow but no luck. Can anybody please help me. Thank you.

0 Answers
Related