简体   繁体   English

无法在非根上下文路径中通过kubernetes ingress-nginx获取websocket应用程序

[英]Unable to get a websocket app work through kubernetes ingress-nginx in a non-root context path

Here is a sample WebSocket app that I'm trying to get it to work from a Kubernetes ingress-nginx controller. 这是一个示例WebSocket应用程序,我试图让它从Kubernetes ingress-nginx控制器工作。

Kubernetes yaml: Kubernetes yaml:

echo "
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: ws-example
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: wseg
    spec:
      containers:
      - name: websocketexample
        image: nicksardo/websocketexample
        imagePullPolicy: Always
        ports:
        - name: http
          containerPort: 8080
        env:
        - name: podname
          valueFrom:
            fieldRef:
              fieldPath: metadata.name
---
apiVersion: v1
kind: Service
metadata:
  name: ws-example-svc
  labels:
    app: wseg
spec:
  type: NodePort
  ports:
  - port: 80
    targetPort: 8080
    protocol: TCP
  selector:
    app: wseg
---

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: ws-example-svc
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
  - host: myhostname.com
    http:
      paths:
      - backend:
          serviceName: ws-example-svc
          servicePort: 80
        path: /somecontext

" | kubectl create -f -

I get this error: 我收到此错误:

WebSocket connection to 'ws://myhostname.com/somecontext/ws?encoding=text' failed: Error during WebSocket handshake: Unexpected response code: 400

When I try to connect using a WebSocket client web page like this http://www.websocket.org/echo.html 当我尝试使用像http://www.websocket.org/echo.html这样的WebSocket客户端网页进行连接时

The version of ingress-nginx is 0.14.0. ingress-nginx的版本是0.14.0。 This version supports WebSockets. 此版本支持WebSockets。


Update, I'm able to directly access the websocket running pod, when I port-forward from my localhost to pod's port. 更新,当我从本地端口转发到pod的端口时,我能够直接访问websocket运行pod。

[rpalaniappan@sdgl15280a331:~/git/zalenium] $ kubectl get pods -l app=wseg
NAME                          READY     STATUS    RESTARTS   AGE
ws-example-5dddb98cfb-vmdt5   1/1       Running   0          5h
[rpalaniappan@sdgl15280a331:~/git/zalenium] $ kubectl port-forward ws-example-5dddb98cfb-vmdt5 8080:8080
Forwarding from 127.0.0.1:8080 -> 8080
Forwarding from [::1]:8080 -> 8080
Handling connection for 8080

[rpalaniappan@sdgl15280a331:~/git/zalenium] $ wscat -c ws://localhost:8080/ws
connected (press CTRL+C to quit)
< Connected to ws-example-5dddb98cfb-vmdt5
> hi
< hi
< ws-example-5dddb98cfb-vmdt5 reports time: 2018-12-28 01:19:00.788098266 +0000 UTC

So basically this: 基本上这个:

nginx.ingress.kubernetes.io/rewrite-target: /

is stripping the /ws from the request (combined with path: /ws ) that gets sent to the backend everytime your browser tries to issue a WebSocket connection request. 被剥离/ws从请求(联合path: /ws ),其被发送到后端每次浏览器尝试发出WebSocket连接请求。 The backend expects /ws when it receives a connection request. 后端在收到连接请求时需要/ws

If you specify path: /mypath and /mypath/* it works (works for me): 如果指定path: /mypath/mypath/*它可以工作(适用于我):

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: ws-example-svc
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
  - host: myhostname.com
    http:
      paths:
      - backend:
          serviceName: ws-example-svc
          servicePort: 80
        path: /mypath
      - backend:
          serviceName: ws-example-svc
          servicePort: 80
        path: /mypath/*

https://kubernetes.github.io/ingress-nginx/user-guide/miscellaneous/#websockets https://kubernetes.github.io/ingress-nginx/user-guide/miscellaneous/#websockets

If the NGINX ingress controller is exposed with a service type=LoadBalancer make sure the protocol between the loadbalancer and NGINX is TCP. 如果使用服务类型= LoadBalancer公开NGINX入口控制器,请确保负载均衡器和NGINX之间的协议是TCP。

Sample AWS L4 Service https://github.com/kubernetes/ingress-nginx/blob/master/deploy/provider/aws/service-l4.yaml#L11 AWS L4服务示例https://github.com/kubernetes/ingress-nginx/blob/master/deploy/provider/aws/service-l4.yaml#L11

# Enable PROXY protocol
service.beta.kubernetes.io/aws-load-balancer-proxy-protocol: "*"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM