[英]Setting Traefik to intercept traffic between Docker containers
我有 Traefik 容器(traefik v. 2.8)作為本地開發的反向代理運行。 我還使用 docker-compose.yml 文件來定義我的服務。
此外,我通過在我的/etc/hosts
文件中設置這些行,通過host.docker.internal
公開了docker-compose.yml
中存在的服務:
127.0.0.1 host.docker.internal
localhost host.docker.internal
我的設置是這樣的:
我已經設置了從 a.localhost 到 host.docker.internal:8443 的 Traefik 路由。 我可以很好地從容器外部的主機訪問a.localhost
,並且 Traefik 確實根據我的需要將流量路由到host.docker.internal:8443
。
問題是我有理由讓服務 B (host.docker.internal:8453) 通過a.localhost
主機名調用服務 A。 這不起作用,因為在服務 B 中,我在嘗試訪問a.localhost
時得到未知主機
這是我的docker-compose.yml
文件的摘錄:
version: '3'
services:
reverse_proxy:
image: traefik:v2.8
# Enables the web UI and tells Traefik to listen to docker
command: --api.insecure=true --providers.docker
ports:
- "80:80"
- "443:443"
# The Web UI (enabled by --api.insecure=true)
- "9000:8080"
volumes:
# So that Traefik can listen to the Docker events
- /var/run/docker.sock:/var/run/docker.sock
- ./dev-traefik/traefik.yml:/etc/traefik/traefik.yml
- ./dev-traefik:/configurations
service_a:
ports:
- "8443:8443"
service_b:
ports:
- "8453:8453"
我還在為 Traefik 使用基於 yml 的配置,存在於dynamic-config.yml
:
http:
routers:
service-a-router:
service: service-a
rule: "Host(`a.localhost`)"
tls: "true" # using tls
services:
service-a:
loadBalancer:
servers:
- url: "https://host.docker.internal:8443" # service A
tls:
certificates:
- certFile: "/etc/https/tls.crt"
keyFile: "/etc/https/tls.key"
stores:
default:
defaultCertificate:
certFile: "/etc/https/tls.crt"
keyFile: "/etc/https/tls.key"
Traefik 似乎能夠監聽來自主機網絡的請求,因為從容器網絡外部的瀏覽器訪問https://a.localhost
工作正常。 另一方面,service_a 容器發出的請求似乎沒有被 Traefik 捕獲。
我還嘗試將a.localhost
添加到運行容器的主機中的/etc/hosts
中,如下所示:
127.0.0.1 a.localhost
localhost a.localhost
然后在服務B容器內使用curl
訪問服務A。
這導致connection refused
,而不是Could not resolve host: a.localhost
。 這導致我建議traefik
無法攔截來自服務 b 容器的流量
我究竟做錯了什么? 有沒有辦法使這樣的設置工作? 我確實有一個正當的理由,這與在本地開發中盡可能接近部署到雲的其他環境中的設置有關。
我無法按照我最初指定的方式讓 Traefik 攔截容器 -> 容器流量,但實際上仍然能夠使類似的設置正常工作。
這是場景:
我有兩個通過 HTTP 和 TLS 訪問的服務:
I want to be able to use host.docker.internal
special DNS name to my advantage and actually have Traefik to proxy traffic from https://host.docker.internal/service_a
to service_a
port 8443 both outside Docker container network (from the host運行 Docker 的機器)和來自service_b
的事實是,運行 Docker 的主機都可以訪問host.docker.internal
以及 Docker 網絡內的主機。 利用這一事實,我剛剛在 Traefik 的 YML 配置文件中定義了service_a
的路徑:
http:
routers:
service-a-router:
service: service-a
rule: "Host(`host.docker.internal`) && PathPrefix(`/service_a`)"
tls: "true" # using tls
services:
service-a:
loadBalancer:
servers:
- url: "https://host.docker.internal:8443" # service A
tls:
certificates:
- certFile: "/etc/https/tls.crt"
keyFile: "/etc/https/tls.key"
stores:
default:
defaultCertificate:
certFile: "/etc/https/tls.crt"
keyFile: "/etc/https/tls.key"
docker-compose.yml
看起來像這樣:
version: '3'
services:
reverse_proxy:
image: traefik:v2.8
# Enables the web UI and tells Traefik to listen to docker
command: --api.insecure=true --providers.docker
ports:
- "80:80"
- "443:443"
# The Web UI (enabled by --api.insecure=true)
- "9000:8080"
volumes:
# So that Traefik can listen to the Docker events
- /var/run/docker.sock:/var/run/docker.sock
- ./dev-traefik/traefik.yml:/etc/traefik/traefik.yml
- ./dev-traefik:/configurations
service_a:
ports:
- "8443:8443"
service_b:
ports:
- "8453:8453"
現在,當我在我的 Traefik YML 配置的Host
規則中使用host.docker.internal
時,Traefik 實際上能夠攔截來自 Docker 主機和來自service_b
容器服務_b 02 的流量。
service_b
只需要配置 https https://host.docker.internal/service_a
的 URL 即可通過 Traefik 訪問service_a
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.