我有一个简单的Node.js聊天示例,它使用socket.io。我已经在本地使用Docker容器测试了这个部署,并且Web客户端成功连接到WebSocket(socket.io)。当我尝试将其部署在具有ingress Controller的Kubernetes集群上时,问题出现了。
我的问题是,在使用入口控制器时,socket.io有什么特别的要求吗?
我得到的错误信息是<<browser name>> Can't estabilish a connection to the server at ws://<<address>>/socket.io/EIO=3&transport=websocket
下面是服务器端的简单实现
var io = socket(server);
io.on('connection', (socket)=>{
console.log('made socket connection')
socket.on('chat', (message)=>{
message:message.value
})
字符串
下面是客户端:
var socket = io.connect("http://localhost:3002", {
upgrade: false,
transports: ['websocket'],
secure: True
});
型
我的ingressyaml文件看起来像这样
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: ws-ingress
namespace: websocket-ns
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/rewrite-target: /
nginx.ingress.kubernetes.io/websocket-services: "ws-service"
nginx.ingress.kubernetes.io/proxy-read-timeout: '3600'
nginx.ingress.kubernetes.io/proxy-send-timeout: '3600'
nginx.ingress.kubernetes.io/server-snippet: |
http {
server {
listen 3002;
location = / {
proxy_set_header Upgrade "websocket";
proxy_http_version 1.1;
proxy_set_header X-Forwarded-Host $http_host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $host;
proxy_set_header Connection "upgrade";
proxy_cache_bypass $http_upgrade;
}
upstream nodes {
hash $remote_addr consistent;
server app01:3002;
}
}
spec:
rules:
- host: "$host_address"
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: ws-service
port:
number: 80
型
服务热线:
apiVersion: v1
kind: Service
metadata:
name: ws-service
namespace: websocket-ns
labels:
app: ws-service
spec:
type: LoadBalancer
selector:
app: ws-app
ports:
- port: 80
protocol: TCP
targetPort: 3002
型
服务器部署文件:
apiVersion: apps/v1
kind: Deployment
metadata:
name: ws-app
namespace: websocket-ns
labels:
app: ws-app
spec:
replicas: 1
selector:
matchLabels:
app: ws-app
template:
metadata:
labels:
app: ws-app
spec:
containers:
- name: ws-app
image: themuchy/socketexample
ports:
- containerPort: 3002
name: ws-app
型
2条答案
按热度按时间oxcyiej71#
这一个正在和一个
字符串
Nginx本身会升级HTTP请求并将其转换为WebSocket。您不必添加任何相同的注解。
Deployment
和service
、YAML
配置看起来不错,但是尝试删除注解server-snippet
。型
drnojrws2#
我一整天都在撞墙!发现注解中的一行解决了这个问题!我通过将副本减少到1来解决这个问题,然后它工作了,很快就很明显,整个问题就是会话粘滞!see socket io multiple nodes
强大的行是:nginx.ingress.kubernetes.io/upstream-hash-by:“$client_ip”
下面是我入口规则
字符串