Kubernetes Nginx Ingress 和 Socket.io 连接问题
Posted
技术标签:
【中文标题】Kubernetes Nginx Ingress 和 Socket.io 连接问题【英文标题】:Kubernetes Nginx Ingress and Socket.io Connection Issues 【发布时间】:2020-03-01 00:52:33 【问题描述】:我的 react 应用程序与 nodejs socket.io 应用程序聊天时遇到问题。
但是,我已缩小范围并认为这是入口配置错误。端口转发 socket.io nodejs pod 并通过 127.0.0.1:3020 与 react 连接工作正常。
Socket.io 部署文件
apiVersion: apps/v1
kind: Deployment
metadata:
name: websockettest-deployment
spec:
replicas: 1
selector:
matchLabels:
component: websockettest
template:
metadata:
labels:
component: websockettest
spec:
containers:
- name: websockettest
image: websockettest
imagePullPolicy: IfNotPresent
ports:
- containerPort: 3020
Socket IO 服务配置
apiVersion: v1
kind: Service
metadata:
name: websockettest-cluster-ip-service
spec:
type: ClusterIP
selector:
component: websockettest
ports:
- port: 3020
targetPort: 3020
入口配置
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: ingress-service
annotations:
kubernetes.io/ingress.class: nginx
nginx.org/websocket-services: websockettest-cluster-ip-service
spec:
rules:
- http:
paths:
- path: /websockettest/?(.*)
backend:
serviceName: websockettest-cluster-ip-service
servicePort: 3020
Nodejs Socket.io
const http = require('http');
const express = require('express');
var app = express();
var server = http.createServer(app);
var io = require('socket.io')(server);
io.set("transports", ["websocket"]);
io.on('connection', function (socket)
console.log('connected socket!');
socket.on('greet', function (data)
console.log(data);
socket.emit('respond', hello: 'Hello' );
);
socket.on('disconnect', function ()
console.log('Socket disconnected');
);
);
const port = process.env.PORT || 3020;
server.listen(port, () =>
console.log(`Server is up on port $port`);
);
React Socket.io-client
// Various Attempts:
// websockettest
// websockettest-cluster-ip-service
// http://192.168.64.11:3020
// :3020/websockettest
// 127.0.0.1:3020 - port forwarding works
const socket = io('192.168.64.11/websockettest',
'reconnection': false,
transports: ['websocket']
);
【问题讨论】:
io('192.168.64.11/websockettest',
是错误的。它应该是 io(':3020/websockettest/',
注意端口和斜杠(因为你的正则表达式)
如果你想要80端口那么你需要将port: 3020
更改为port: 80
,然后基本上忘记3020客户端因为它转发到80
您好,感谢您的回复。我在 ingress + 服务中将端口更新为 80,并交换为 io(':80/websockettest/')。不幸的是,没有运气。
【参考方案1】:
Socket.io 具有用于 websocket 通信的特定路径。因此,kubernetes 入口需要配置为支持“/socket.io”。此外,node.js 中间件还需要为套接字端点提供自定义命名空间。
ingress-ws-service.yaml
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: ingress-ws-service
annotations:
nginx.ingress.kubernetes.io/use-regex: "true"
nginx.org/websocket-services: "websockettest-cluster-ip-service"
spec:
rules:
- http:
paths:
- path: /websockettest/.*
backend:
serviceName: websockettest-cluster-ip-service
servicePort: 3020
- path: /socket.io/.*
backend:
serviceName: websockettest-cluster-ip-service
servicePort: 3020
Nodejs Socket.io
const http = require('http');
const express = require('express');
var app = express();
var server = http.createServer(app);
var io = require('socket.io')(server);
const nsp = io.of("/websockettest");
nsp.on('connection', function (socket)
console.log('connected socket!');
socket.on('greet', function (data)
console.log(data);
nsp.emit('respond', hello: 'Hello' );
);
socket.on('disconnect', function ()
console.log('Socket disconnected');
);
);
const port = process.env.PORT || 3020;
server.listen(port, () =>
console.log(`Server is up on port $port`);
);
反应客户端
const socket = io('/websockettest',
'reconnection': true,
transports: ['websocket']
);
【讨论】:
非常感谢......这个解决方案在 Nextjs 中也非常适合我......以上是关于Kubernetes Nginx Ingress 和 Socket.io 连接问题的主要内容,如果未能解决你的问题,请参考以下文章
Nginx.ingress.kubernetes.io/proxy-body-size 不工作
Kubernetes Ingress Nginx 加载资源 404