在WebSocket握手过程中出错。意外的响应代码。在Minikube的Ingress上出现502

试图在minikube上创建一个简单的websocket服务器与ingress.当连接到ws:/192.168.99.119得到以下错误。

WebSocket连接到’ws:/192.168.99.119’失败。WebSocket握手过程中出错。意外的响应代码。502

请帮我解决这个问题。下面提供了实现的细节。

Websocket服务器的实现

import * as express from 'express';
import * as http from 'http';
import * as WebSocket from 'ws';

interface ExtWebSocket extends WebSocket {
    id: string; // your custom property
    isAlive: boolean
}
const PORT = parseInt(process.env.NODE_PORT) || 8010;

const app = express();

//initialize a simple http server
const server = http.createServer(app);

//initialize the WebSocket server instance
const wss = new WebSocket.Server({ server });

wss.on('connection', (ws: ExtWebSocket) => {

    //connection is up, let's add a simple simple event
    ws.on('message', (message: string) => {

        //log the received message and send it back to the client
        console.log('received: %s', message);

        const broadcastRegex = /^broadcast\:/;

        ws.isAlive = true;

        ws.on('pong', () => {
            ws.isAlive = true;
        });

        if (broadcastRegex.test(message)) {
            message = message.replace(broadcastRegex, '');

            //send back the message to the other clients
            wss.clients
                .forEach(client => {
                    if (client != ws) {
                        client.send(`Hello, broadcast message -> ${message}`);
                    }
                });

        } else {
            ws.send(`Hello, you sent -> ${message}`);
        }
    });

    //send immediatly a feedback to the incoming connection
    ws.send('Hi there, I am a WebSocket server');
});

setInterval(() => {
    wss.clients.forEach((ws: ExtWebSocket) => {

        if (!ws.isAlive) return ws.terminate();

        ws.isAlive = false;
        ws.ping(null, false);
    });
}, 10000);

//start our server
server.listen(PORT, () => {
    console.log(`Server started on port ${JSON.stringify(server.address())}`);
});

Docker文件


FROM node:alpine
WORKDIR /app
COPY package*.json /app/
RUN npm install

COPY ./ /app/
RUN npm run build

CMD ["node","./dist/server.js"]

EXPOSE 8010

Websocket服务yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: socketserver
spec:
  replicas: 1
  selector:
    matchLabels:
      app: socketserver
  template:
    metadata:
      labels:
        app: socketserver
    spec:
      containers:
      - name: socketserver
        image: kireeti123/socketserver:1.0.2
        resources:
          limits:
            memory: "128Mi"
            cpu: "500m"
        ports:
        - containerPort: 8010
          name: wsport
---
apiVersion: v1
kind: Service
metadata:
  name: socketserver-svc
spec:
  selector:
    app: socketserver
  ports:
  - port: 8010
    targetPort: 80
---

Ingress.yaml

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: demo-ingress
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/websocket-services: socketserver-svc
    nginx.org/websocket-services: socketserver-svc
spec:
  rules:
  - http:
      paths:
      - path: /
        backend:
          serviceName: socketserver-svc
          servicePort: 80

解决方案:

为什么你有 targetPort 设为 80 而你 Pod 暴露端口 8010 ? 它不会工作。

切换你的端口号 Service 定义,所以它看起来像下面的例子。

apiVersion: v1
kind: Service
metadata:
  name: socketserver-svc
spec:
  selector:
    app: socketserver
  ports:
  - port: 80
    targetPort: 8010

希望对你有帮助

给TA打赏
共{{data.count}}人
人已打赏
解决方案

TypeError .map()不是一个函数。

2022-4-20 20:00:07

解决方案

pandas从数据框中用连续的差异过滤行< n。

2022-4-20 20:00:09

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索