Ingress support for websocket

5/1/2020

I have a jetty web app running under k8s. This web app has a websocket end point. The service deployed is exposed via an nginx ingress on https.

Everything works fine, I have the web app running and the websockets work fine (ie messages get pushed and received) but the websockets close with a 1006 error code, which to be honest doesn't stop my code from working but doesn't look good either.

The websocket is exposed @ /notifications. In a "normal" config, ie not k8s, just plain software installed on a VM, I would need to add the following to nginx.conf

location /notifications {
   proxy_pass http://XXX/notifications;
   proxy_read_timeout 3700s;
   proxy_http_version 1.1;
   proxy_set_header Upgrade $http_upgrade;
   proxy_set_header Connection "Upgrade";
   proxy_set_header Origin '';
}

I tried doing this via the ingress

nginx.ingress.kubernetes.io/configuration-snippet: |

  location /notifications {
    proxy_pass http://webapp:8080/notifications;
    proxy_http_version 1.1;
    proxy_set_header Upgrade "websocket";
    proxy_set_header Connection "Upgrade";
  }

But it has no effect, ie I checked the nginx.conf generated and there is no such block added...

Anybody has had issues like this before? any clue on how to solve the 1006 issue?

-- Thomas
jetty
kubernetes
kubernetes-ingress
nginx
websocket

1 Answer

5/2/2020

1006 meaning

As per RFC-6455 1006 means Abnormal Closure:

Used to indicate that a connection was closed abnormally (that is, with no close frame being sent) when a status code is expected.

Also see CloseReason.CloseCodes (Java(TM) EE 7 Specification APIs)

There are so many possible causes either on server or on client.

Client errors: try websocket.org Echo Test

To isolate and debug client's errors, you may use websocket.org Echo Test

As for server error

Jetty

Jetty-related discussion is here: Question regarding abnormal · Issue #604 · eclipse/jetty.project. But it doesn't contain any solutions.

Race detector for golang server code

If your server is written on golang, you may try Data Race Detector - The Go Programming Language

Data races are among the most common and hardest to debug types of bugs in concurrent systems. A data race occurs when two goroutines access the same variable concurrently and at least one of the accesses is a write. See the The Go Memory Model for details.

Here is an example of a data race that can lead to crashes and memory corruption:

func main() {
  c := make(chan bool)
  m := make(map\[string\]string)
  go func() {
      m\["1"\] = "a" // First conflicting access.
      c <- true
  }()
  m\["2"\] = "b" // Second conflicting access.
  <-c
  for k, v := range m {
      fmt.Println(k, v)
  }
}

Case for PHP code

The case for PHP code discussed here: Unclean a closed connection by close() websocket's method (1006) · Issue #236 · walkor/Workerman

-- Yasen
Source: StackOverflow