Traefik 2.2 + Docker + Websocket (ws) 不工作
Traefik 2.2 + Docker + Websocket (ws) not working
我已经设法让 Traefik 为 http 容器工作,但是当我切换到 websocket 时,运气不好。 Traefik 非常简单。让我分享它的 docker-compose 文件:
version: "3.3"
services:
traefik:
image: "traefik:v2.2"
container_name: "traefik"
command:
- "--log.level=DEBUG"
- "--api.insecure=true"
- "--providers.docker=true"
# - "--providers.docker.exposedbydefault=false"
- "--entrypoints.web.address=:80"
- "--entryPoints.ws.address=:81" #I ADDED THIS
- "--accesslog"
ports:
- "80:80"
- "81:81" #I ADDED THIS
- "8080:8080"
volumes:
- "/var/run/docker.sock:/var/run/docker.sock:ro"
为了方便起见,我添加了 #I ADDED THIS
之类的评论,这样您就可以看到我为使 WS 正常工作所做的更改。
现在在实际项目中 docker-compose 我已经添加了这个标签:
version: '2.1'
services:
test:
restart: unless-stopped
hostname: test
build:
context: .
dockerfile: ./Dockerfile
expose:
- 81
labels:
- "traefik.enable=true"
- "traefik.http.routers.test.rule=Host(`test.com`)"
- "traefik.http.routers.test.entrypoints=ws"
- "traefik.http.services.test.loadBalancer.sticky.cookie=true"
我正在尝试使用 Nodejs 进行连接:
var ws = new WebSocket('ws://test.com:81')
不知道还能尝试什么。
先谢谢你了。
编辑 在 traefik 日志中看到:
time="2020-08-23T16:07:28Z" level=debug msg="vulcand/oxy/roundrobin/rr: completed ServeHttp on request" Request="{\"Method\":\"GET\",\"URL\":{\"Scheme\":\"\",\"Opaque\":\"\",\"User\":null,\"Host\":\"\",\"Path\":\"/\",\"RawPath\":\"\",\"ForceQuery\":false,\"RawQuery\":\"\",\"Fragment\":\"\"},\"Proto\":\"HTTP/1.1\",\"ProtoMajor\":1,\"ProtoMinor\":1,\"Header\":{\"Connection\":[\"Upgrade\"],\"Sec-Websocket-Extensions\":[\"permessage-deflate; client_max_window_bits\"],\"Sec-Websocket-Key\":[\"jaXhUWe2lvrgxF0tOn3nWA==\"],\"Sec-Websocket-Version\":[\"13\"],\"Upgrade\":[\"websocket\"],\"X-Forwarded-Host\":[\"test.com:81\"],\"X-Forwarded-Port\":[\"81\"],\"X-Forwarded-Proto\":[\"ws\"],\"X-Forwarded-Server\":[\"58077bdceffd\"],\"X-Real-Ip\":[\"192.168.99.1\"]},\"ContentLength\":0,\"TransferEncoding\":null,\"Host\":\"test.com:81\",\"Form\":null,\"PostForm\":null,\"MultipartForm\":null,\"Trailer\":null,\"RemoteAddr\":\"192.168.99.1:56705\",\"RequestURI\":\"/\",\"TLS\":null}"
192.168.99.1 - - [23/Aug/2020:16:07:07 +0000] "GET / HTTP/1.1" 499 21 "-" "-" 56 "test@docker" "http://172.19.0.2:81" 21082ms
看起来不错,但 client/server 之间仍然没有通信。
我认为您需要向 test
容器添加服务端口:
- "traefik.http.services.test.loadbalancer.server.port=81"
编辑:
要让 traefik 能够发现您的服务,它需要在同一网络上。所以最好的解决方案似乎是为这两种服务定义外部网络或使用通过您的 websocket 服务定义的网络
我已经设法让 Traefik 为 http 容器工作,但是当我切换到 websocket 时,运气不好。 Traefik 非常简单。让我分享它的 docker-compose 文件:
version: "3.3"
services:
traefik:
image: "traefik:v2.2"
container_name: "traefik"
command:
- "--log.level=DEBUG"
- "--api.insecure=true"
- "--providers.docker=true"
# - "--providers.docker.exposedbydefault=false"
- "--entrypoints.web.address=:80"
- "--entryPoints.ws.address=:81" #I ADDED THIS
- "--accesslog"
ports:
- "80:80"
- "81:81" #I ADDED THIS
- "8080:8080"
volumes:
- "/var/run/docker.sock:/var/run/docker.sock:ro"
为了方便起见,我添加了 #I ADDED THIS
之类的评论,这样您就可以看到我为使 WS 正常工作所做的更改。
现在在实际项目中 docker-compose 我已经添加了这个标签:
version: '2.1'
services:
test:
restart: unless-stopped
hostname: test
build:
context: .
dockerfile: ./Dockerfile
expose:
- 81
labels:
- "traefik.enable=true"
- "traefik.http.routers.test.rule=Host(`test.com`)"
- "traefik.http.routers.test.entrypoints=ws"
- "traefik.http.services.test.loadBalancer.sticky.cookie=true"
我正在尝试使用 Nodejs 进行连接:
var ws = new WebSocket('ws://test.com:81')
不知道还能尝试什么。 先谢谢你了。
编辑 在 traefik 日志中看到:
time="2020-08-23T16:07:28Z" level=debug msg="vulcand/oxy/roundrobin/rr: completed ServeHttp on request" Request="{\"Method\":\"GET\",\"URL\":{\"Scheme\":\"\",\"Opaque\":\"\",\"User\":null,\"Host\":\"\",\"Path\":\"/\",\"RawPath\":\"\",\"ForceQuery\":false,\"RawQuery\":\"\",\"Fragment\":\"\"},\"Proto\":\"HTTP/1.1\",\"ProtoMajor\":1,\"ProtoMinor\":1,\"Header\":{\"Connection\":[\"Upgrade\"],\"Sec-Websocket-Extensions\":[\"permessage-deflate; client_max_window_bits\"],\"Sec-Websocket-Key\":[\"jaXhUWe2lvrgxF0tOn3nWA==\"],\"Sec-Websocket-Version\":[\"13\"],\"Upgrade\":[\"websocket\"],\"X-Forwarded-Host\":[\"test.com:81\"],\"X-Forwarded-Port\":[\"81\"],\"X-Forwarded-Proto\":[\"ws\"],\"X-Forwarded-Server\":[\"58077bdceffd\"],\"X-Real-Ip\":[\"192.168.99.1\"]},\"ContentLength\":0,\"TransferEncoding\":null,\"Host\":\"test.com:81\",\"Form\":null,\"PostForm\":null,\"MultipartForm\":null,\"Trailer\":null,\"RemoteAddr\":\"192.168.99.1:56705\",\"RequestURI\":\"/\",\"TLS\":null}"
192.168.99.1 - - [23/Aug/2020:16:07:07 +0000] "GET / HTTP/1.1" 499 21 "-" "-" 56 "test@docker" "http://172.19.0.2:81" 21082ms
看起来不错,但 client/server 之间仍然没有通信。
我认为您需要向 test
容器添加服务端口:
- "traefik.http.services.test.loadbalancer.server.port=81"
编辑:
要让 traefik 能够发现您的服务,它需要在同一网络上。所以最好的解决方案似乎是为这两种服务定义外部网络或使用通过您的 websocket 服务定义的网络