【问题标题】:How to use traefik segments to route request to the right service on the right port of the same container如何使用 traefik 段将请求路由到同一容器正确端口上的正确服务
【发布时间】:2021-02-02 23:41:13
【问题描述】:

我有一个 docker 容器,它暴露了 2 个端口,每个端口用于不同的 Web 服务。 一个是 API,另一个是带有文档的简单网页。

我试图理解 Traefik 的文档。看起来我需要使用细分,但我失败得很惨。

我想要

这就是我现在所拥有的,它可以工作但只服务于端口 4000

  my-service:
    image: myregistry.com:5005/my-service:latest
    container_name: my-service
    labels:
      - traefik.http.routers.my-service-router.tls=true
      - traefik.http.routers.my-service-router.entrypoints=secure
      - traefik.http.routers.my-services-router.rule=Host(`www.mydomain.net`)
    networks:
      - internal

我尝试了以下片段

  my-service:
    image: myregistry.com:5005/my-service:latest
    container_name: my-service
    expose:
      - 4000 # my API
      - 3000 # my doc
    labels:
      # segment myapi
      - traefik.myapi.port=4000
      - traefik.myapi.http.routers.my-service-router.tls=true
      - traefik.myapi.http.routers.my-service-router.entrypoints=secure
      - traefik.myapi.http.routers.my-services-router.rule=Host(`api.mydomain.net`)
      # segment mydoc
      - traefik.mydoc.port=3000
      - traefik.mydoc.http.routers.my-service-router.tls=true
      - traefik.mydoc.http.routers.my-service-router.entrypoints=secure
      - traefik.mydoc.http.routers.my-services-router.rule=Host(`doc.mydomain.net`)
    networks:
      - internal

但这是错误的,不是吗? 所以我根据文档尝试了不同的...

  my-service:
    image: myregistry.com:5005/my-service:latest
    container_name: my-service
    expose:
      - 4000 # my service
      - 3000 # my other service
    labels:
      # segment myapi
      - traefik.myapi.port=4000
      - traefik.myapi.frontend.entrypoints=secure
      - traefik.myapi.frontend.rule=Host(`api.mydomain.net`)
      # segment mydoc
      - traefik.mydoc.port=3000
      - traefik.mydoc.frontend.entrypoints=secure
      - traefik.mydoc.frontend.rule=Host(`doc.mydomain.net`)
    networks:
      - internal

我不理解文档,所以欢迎任何指针。

【问题讨论】:

  • 你能发布你的整个 docker-compose 文件吗?

标签: docker docker-compose traefik


【解决方案1】:

我很确定“mydoc”和“myapi”不应该在那里:

my-service:
  image: myregistry.com:5005/my-service:latest
  container_name: my-service
  expose:
    - 4000 # my service
    - 3000 # my other service
  labels:
    # segment myapi
    - traefik.http.services.myapi.loadbalancer.server.port=4000
    - traefik.http.routers.myapi.entrypoints=secure
    - traefik.http.routers.myapi.rule=Host(`api.mydomain.net`)
    - traefik.http.routers.myapi.service=myapi
    # segment mydoc
    - traefik.http.services.mydoc.loadbalancer.server.port=3000
    - traefik.http.routers.mydoc.entrypoints=secure
    - traefik.http.routers.mydoc.rule=Host(`adoci.mydomain.net`)
    - traefik.http.routers.mydoc.service=mydoc
  networks:
    - internal

还要检查 traefik 容器是否也在 internal 网络中。

【讨论】:

  • 成功了。我需要等 13 个小时才能奖励你,明天会。欢呼
猜你喜欢
  • 2015-03-24
  • 2021-02-13
  • 1970-01-01
  • 2014-05-20
  • 1970-01-01
  • 1970-01-01
  • 2023-03-23
  • 2012-05-01
  • 1970-01-01
相关资源
最近更新 更多