如何使用 traefik 段将请求路由到同一容器的正确端口上的正确服务

How to use traefik segments to route request to the right service on the right port of the same container

我有一个 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

我不理解该文档,因此欢迎任何指点。

我很确定“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 网络中。