Kubernetes集群中具有Nginx-ingress的Websocket连接

时间:2019-08-25 01:36:09

标签: kubernetes websocket nginx-ingress

我正在尝试在Kubernetes集群中运行的服务器上建立一个简单的websocket连接。

Websocket连接可以在本地测试机上建立,但是在用nginx-ingress部署到GKE后,我无法将客户端连接到服务器。

入口Yaml文件:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: ingress-service
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.org/websocket-services : "socket-cluster-ip-service"
    nginx.ingress.kubernetes.io/rewrite-target: /
    nginx.ingress.kubernetes.io/websocket-services : "socket-cluster-ip-service"
    nginx.ingress.kubernetes.io/proxy-send-timeout: "1800"
    nginx.ingress.kubernetes.io/proxy-read-timeout: "1800"
spec:
  rules:
    - host: my-socket.com
      http:
        paths:
          - path: /
            backend:
              serviceName: socket-cluster-ip-service
              servicePort: 3000
    - host: www.my-socket.com
      http:
        paths:
          - path: /
            backend:
              serviceName: socket-cluster-ip-service
              servicePort: 3000

服务Yaml文件:

apiVersion: v1
kind: Service
metadata:
  name: socket-cluster-ip-service
spec:
  type: ClusterIP
  selector:
    component: socket
  ports:
  - port: 3000
    targetPort: 3000

部署Yaml文件:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: socket-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      component: socket
  template:
    metadata:
      labels:
        component: socket
    spec:
      containers:
      - name: socket
        image: gcr.io/my-socket/socket
        ports:
        - containerPort: 3000

表达服务器代码:

...

var server = http.createServer(app);
const WebSocket = require('ws');

const wss = new WebSocket.Server({ server: server });

wss.on('connection', function connection(ws) {
  console.log("new connection", ws)
  ws.on('message', function incoming(message) {
    console.log('received: %s', message);
  });

  ws.send('something');
});

...

客户端代码:

var ws = new WebSocket('ws://www.my-socket.com/');

ws.onopen = () => {
  // connection opened
  ws.send('something'); // send a message
};

ws.onmessage = (e) => {
  // a message was received
  console.log(e.data);
};

ws.onerror = (e) => {
  // an error occurred
  console.log(e.message);
};

ws.onclose = (e) => {
  // connection closed
  console.log(e.code, e.reason);
};

我一直从客户端收到错误消息“ Invalid Sec-WebSocket-Accept response”。但是我认为,这仅仅是客户端无法与服务器建立Websocket连接的事实。

我使用的服务器端库是ws

我已按照other postofficial ingress-nginx documentation的建议在我的入口Yaml文件中添加了以下注释,但仍无法正常工作:

nginx.org/websocket-services : "socket-cluster-ip-service"
nginx.ingress.kubernetes.io/websocket-services : "socket-cluster-ip-service"
nginx.ingress.kubernetes.io/proxy-send-timeout: "1800"
nginx.ingress.kubernetes.io/proxy-read-timeout: "1800"

1 个答案:

答案 0 :(得分:0)

对我来说,它就像一种魅力!
我正在使用 kong-docker-kubernetes-ingress-controller.bintray.io/kong-ingress-controller:0.9.0 在我的私有kubernetes集群上。
因此,您的问题可能与GCE有关。

还请检查您的TLS配置。
在您的帖子中,您提到的是ws://...而不是wss://,这表明您甚至没有使用TLS。这可能会被您的浏览器(例如Chrome)拒绝。