Nginx ingress controller websocket support

Kit*_*ura 6 nginx websocket kubernetes kubernetes-ingress nginx-ingress

Recently I've been working on a toy app using Kubernetes. Part of the app is a web server that needs to support WebSockets. Currently, I'm using port-forwarding to access the web server and everything works just fine.

I'd like to switch to using an Ingress and IngressController to avoid using the port forwarding.

Here is my Ingress config:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: app-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
    nginx.ingress.kubernetes.io/force-ssl-redirect: "true"
    nginx.ingress.kubernetes.io/secure-backends: "true"
    nginx.ingress.kubernetes.io/proxy-send-timeout: "3600"
    nginx.ingress.kubernetes.io/proxy-read-timeout: "3600"
spec:
  rules:
  - http:
      paths:
      - path: /app
        backend:
          serviceName: web-svc
          servicePort: 3030
      - path: /ws
        backend:
          serviceName: web-svc
          servicePort: 3030
Run Code Online (Sandbox Code Playgroud)

Now accessing the app through $(minikube ip)/app works just fine, but the WebSocket requests all fail because nginx is returning a 200 and not a 101.

I've tried adding the nginx.org/websocket-services annotation but that doesn't seem to be working either.

Has anyone encountered a similar situation?

Cheers

小智 14

这些对我有用。

看来他们通过注释添加了支持(文档中的示例):

nginx.org/websocket-services: "service1[,service2,...]"
Run Code Online (Sandbox Code Playgroud)

我测试了与telsocket的连接,这是一个连接到 WS/WSS 套接字的小工具。

有各种各样不同的客户端,这也可能有助于找到连接的罪魁祸首。

需要注意的是:有两个 nginx 入口控制器可用,更多信息请参见此处

这个答案仅限于 nginxinc 版本,与问题中使用的版本不同,接受的答案是目前唯一的解决方案。


Ric*_*ico 5

通过查看nginx入口控制器文档nginx文档,您可能需要像这样的内容作为Kubernetes上的注释Ingress

nginx.ingress.kubernetes.io/configuration-snippet: |
   proxy_http_version 1.1;
   proxy_set_header Upgrade "websocket";
   proxy_set_header Connection "Upgrade";
Run Code Online (Sandbox Code Playgroud)

请注意,添加该注释后,所有Ingress规则都将location在nginx配置的代码块中包含该代码段。因此,如果要忽略其他规则,则必须创建一个单独的Kubernetes Ingress

  • 好的,我做到了。我在这个要点中描述了工作配置:https://gist.github.com/jsdevtom/7045c03c021ce46b08cb3f41db0d76da#file-ingress-service-yaml (2认同)