Kubernetes 集群中的 Websocket 连接与 nginx-ingress

Jac*_*ack 5 websocket kubernetes 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
Run Code Online (Sandbox Code Playgroud)

服务yaml文件:

apiVersion: v1
kind: Service
metadata:
  name: socket-cluster-ip-service
spec:
  type: ClusterIP
  selector:
    component: socket
  ports:
  - port: 3000
    targetPort: 3000
Run Code Online (Sandbox Code Playgroud)

部署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
Run Code Online (Sandbox Code Playgroud)

快递服务器代码:

...

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');
});

...
Run Code Online (Sandbox Code Playgroud)

客户端代码:

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);
};
Run Code Online (Sandbox Code Playgroud)

我不断从客户端收到错误消息“无效的 Sec-WebSocket-Accept 响应”。但我认为这只是客户端无法与服务器建立 websocket 连接的事实。

我使用的服务器端库是ws

我已按照其他帖子官方 ingress-nginx 文档的建议将以下注释添加到我的 ingress 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"
Run Code Online (Sandbox Code Playgroud)

小智 1

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

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