簡體   English   中英

設置 Traefik 攔截 Docker 容器之間的流量

[英]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

我的設置是這樣的:

  • host.docker.internal:8443 用於 service_a
  • host.docker.internal:8453 用於 service_b

我已經設置了從 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 訪問的服務:

  • 服務_a
  • 服務_b

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM