无法使用 traefik 负载均衡器从不同的 docker 容器访问 service2.localhost
Can't access service2.localhost from a different docker container with traefik load balancer
我有一个简单的设置。 2 BE 服务在单独的容器中,traefik 在另一个容器中作为代理和负载平衡器。现在一切都在我的 MacBook 本地 运行ning 上,我不使用 docker swarm。所有这些都是 运行 通过 docker 撰写的。
当我从 service1 向 service2 发出 API 请求时,我收到此消息:
{
"message": "connect ECONNREFUSED 127.0.0.1:80",
"name": "Error",
"stack": "Error: connect ECONNREFUSED 127.0.0.1:80\n at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1161:16)\n at TCPConnectWrap.callbackTrampoline (node:internal/async_hooks:130:17)",
"config": {
"transitional": {
"silentJSONParsing": true,
"forcedJSONParsing": true,
"clarifyTimeoutError": false
},
"transformRequest": [null],
"transformResponse": [null],
"timeout": 5000,
"xsrfCookieName": "XSRF-TOKEN",
"xsrfHeaderName": "X-XSRF-TOKEN",
"maxContentLength": -1,
"maxBodyLength": -1,
"headers": {
"Accept": "application/json, text/plain, */*",
"Content-Type": "application/json",
"User-Agent": "axios/0.23.0",
"Content-Length": 121
},
"maxRedirects": 5,
"baseURL": "example.com",
"method": "post",
"url": "http://service1.localhost/users",
"data": "{\"firstname\":\"firstname\",\"lastname\":\"lastname\",\"username\":\"username\",\"email\":\"email@email.com\",\"password\":\"password123!\"}"
},
"code": "ECONNREFUSED",
"status": null
}
从对 service1 的基本 FE 调用 API 一切正常,我也可以使用我为每个 API 生成的 swagger,但我无法在它们之间工作。
会是什么?我需要在容器上配置一些额外的东西吗?
这是我的 docker-compose.yml 个文件:
服务 1:
version: '3.8'
services:
service1:
build: .
env_file:
- .env
labels:
- "traefik.enable=true"
- "traefik.http.routers.service1.rule=Host(`service1.localhost`)"
- "traefik.http.routers.service1.entrypoints=web"
- "traefik.http.middlewares.testheader.headers.accesscontrolallowmethods=GET,OPTIONS,PUT,POST"
- "traefik.http.middlewares.testheader.headers.accesscontrolalloworigin=*"
- "traefik.http.middlewares.testheader.headers.accesscontrolmaxage=100"
- "traefik.http.middlewares.testheader.headers.addvaryheader=true"
# - "--providers.docker.useBindPortIP=true"
networks:
- traefik
- BE
networks:
traefik:
name: traefik_webgatewa
BE:
external: true
服务 2:
version: '3.8'
services:
service2:
build: .
env_file:
- .env.dev
labels:
- "traefik.enable=true"
- "traefik.http.routers.service2.rule=Host(`service2.localhost`)"
- "traefik.http.routers.service2.entrypoints=web"
- "traefik.http.middlewares.testheader.headers.accesscontrolallowmethods=GET,OPTIONS,PUT,POST"
- "traefik.http.middlewares.testheader.headers.accesscontrolalloworigin=*"
- "traefik.http.middlewares.testheader.headers.accesscontrolmaxage=100"
- "traefik.http.middlewares.testheader.headers.addvaryheader=true"
networks:
- traefik
- BE
networks:
traefik:
name: traefik_webgateway
BE:
external: true
traefik:
version: "3.8"
services:
traefik:
image: "traefik:v2.4"
container_name: "traefik"
command:
- "--log.level=DEBUG"
- "--api.insecure=true"
- "--providers.docker=true"
- "--providers.docker.exposedbydefault=false"
- "--entrypoints.web.address=:80"
- "--log.filePath=/traefik.log"
- "--log.format=json"
- "--accesslog.filepath=/access.log"
- "--metrics.prometheus=true"
- "--metrics.prometheus.buckets=0.1,0.3,1.2,5.0"
- "--metrics.prometheus.addEntryPointsLabels=true"
- "--providers.docker.watch"
- "--metrics.prometheus.addServicesLabels=true"
ports:
- "80:80"
- "8080:8080"
volumes:
- "/var/run/docker.sock:/var/run/docker.sock:ro"
networks:
- traefik
- BE
networks:
traefik:
name: traefik_webgatewa
BE:
external: true
找到这个 and 。
在 traefik compose yml 文件中,我调整了服务的网络以利用来自 docker 的 DNS。
networks:
traefik:
aliases:
- fozzie-auth.localhost
- fozzie-label.localhost
- fozzie-gateway.localhost
我有一个简单的设置。 2 BE 服务在单独的容器中,traefik 在另一个容器中作为代理和负载平衡器。现在一切都在我的 MacBook 本地 运行ning 上,我不使用 docker swarm。所有这些都是 运行 通过 docker 撰写的。
当我从 service1 向 service2 发出 API 请求时,我收到此消息:
{
"message": "connect ECONNREFUSED 127.0.0.1:80",
"name": "Error",
"stack": "Error: connect ECONNREFUSED 127.0.0.1:80\n at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1161:16)\n at TCPConnectWrap.callbackTrampoline (node:internal/async_hooks:130:17)",
"config": {
"transitional": {
"silentJSONParsing": true,
"forcedJSONParsing": true,
"clarifyTimeoutError": false
},
"transformRequest": [null],
"transformResponse": [null],
"timeout": 5000,
"xsrfCookieName": "XSRF-TOKEN",
"xsrfHeaderName": "X-XSRF-TOKEN",
"maxContentLength": -1,
"maxBodyLength": -1,
"headers": {
"Accept": "application/json, text/plain, */*",
"Content-Type": "application/json",
"User-Agent": "axios/0.23.0",
"Content-Length": 121
},
"maxRedirects": 5,
"baseURL": "example.com",
"method": "post",
"url": "http://service1.localhost/users",
"data": "{\"firstname\":\"firstname\",\"lastname\":\"lastname\",\"username\":\"username\",\"email\":\"email@email.com\",\"password\":\"password123!\"}"
},
"code": "ECONNREFUSED",
"status": null
}
从对 service1 的基本 FE 调用 API 一切正常,我也可以使用我为每个 API 生成的 swagger,但我无法在它们之间工作。
会是什么?我需要在容器上配置一些额外的东西吗?
这是我的 docker-compose.yml 个文件:
服务 1:
version: '3.8'
services:
service1:
build: .
env_file:
- .env
labels:
- "traefik.enable=true"
- "traefik.http.routers.service1.rule=Host(`service1.localhost`)"
- "traefik.http.routers.service1.entrypoints=web"
- "traefik.http.middlewares.testheader.headers.accesscontrolallowmethods=GET,OPTIONS,PUT,POST"
- "traefik.http.middlewares.testheader.headers.accesscontrolalloworigin=*"
- "traefik.http.middlewares.testheader.headers.accesscontrolmaxage=100"
- "traefik.http.middlewares.testheader.headers.addvaryheader=true"
# - "--providers.docker.useBindPortIP=true"
networks:
- traefik
- BE
networks:
traefik:
name: traefik_webgatewa
BE:
external: true
服务 2:
version: '3.8'
services:
service2:
build: .
env_file:
- .env.dev
labels:
- "traefik.enable=true"
- "traefik.http.routers.service2.rule=Host(`service2.localhost`)"
- "traefik.http.routers.service2.entrypoints=web"
- "traefik.http.middlewares.testheader.headers.accesscontrolallowmethods=GET,OPTIONS,PUT,POST"
- "traefik.http.middlewares.testheader.headers.accesscontrolalloworigin=*"
- "traefik.http.middlewares.testheader.headers.accesscontrolmaxage=100"
- "traefik.http.middlewares.testheader.headers.addvaryheader=true"
networks:
- traefik
- BE
networks:
traefik:
name: traefik_webgateway
BE:
external: true
traefik:
version: "3.8"
services:
traefik:
image: "traefik:v2.4"
container_name: "traefik"
command:
- "--log.level=DEBUG"
- "--api.insecure=true"
- "--providers.docker=true"
- "--providers.docker.exposedbydefault=false"
- "--entrypoints.web.address=:80"
- "--log.filePath=/traefik.log"
- "--log.format=json"
- "--accesslog.filepath=/access.log"
- "--metrics.prometheus=true"
- "--metrics.prometheus.buckets=0.1,0.3,1.2,5.0"
- "--metrics.prometheus.addEntryPointsLabels=true"
- "--providers.docker.watch"
- "--metrics.prometheus.addServicesLabels=true"
ports:
- "80:80"
- "8080:8080"
volumes:
- "/var/run/docker.sock:/var/run/docker.sock:ro"
networks:
- traefik
- BE
networks:
traefik:
name: traefik_webgatewa
BE:
external: true
找到这个
在 traefik compose yml 文件中,我调整了服务的网络以利用来自 docker 的 DNS。
networks:
traefik:
aliases:
- fozzie-auth.localhost
- fozzie-label.localhost
- fozzie-gateway.localhost