在WebSocket握手过程中出错。意外的响应代码。在Minikube的Ingress上出现502
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在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
希望对你有帮助
以上是关于在WebSocket握手过程中出错。意外的响应代码。在Minikube的Ingress上出现502的主要内容,如果未能解决你的问题,请参考以下文章
Django:WebSocket 握手期间出错:意外的响应代码:500
气氛错误:WebSocket 握手期间出错:意外响应代码:501