简体   繁体   English

使用 Traefik 和 docker-compose 的端口转发问题

[英]Port forwarding problem using Traefik and docker-compose

I want the next port forwarding:我想要下一个端口转发:

http://traefik.service.localhost/ -> Traefik UI http://traefik.service.localhost/ -> Traefik UI

http://api.service.localhost/ -> 'Hello-Word' page from api-service http://api.service.localhost/ -> 'Hello-Word' page from api-service

Here is my attempt to create appropriate docker-compose.yml file:这是我尝试创建适当的 docker-compose.yml文件:

version: '3.8'

services:

  reverse-proxy:
    image: traefik:v2.4
    container_name: reverse-proxy
    command: 
      - "--api.insecure=true"
      - "--providers.docker"
    ports:
      - "80:80"
      - "8080:8080"
    labels:
      - traefik.enable=true
      - traefik.docker.network=pred-network
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
    networks:
      - pred-network


  api-service:
    image: x86_64/prediction-service:0.8.1
    container_name: api-service
    environment:
      SERVING_SERVICE: model-service
    expose:
      - 80
    labels:
      - traefik.enable=true
      - traefik.http.routers.api-service.rule=Host(`api.service.localhost`)
    networks:
      - pred-network

networks:
  pred-network:

I am getting the following:我得到以下信息:

http://traefik.service.localhost/ -> HTTP Error 404. The requested resource is not found. http://traefik.service.localhost/ -> HTTP Error 404. The requested resource is not found.

http://api.service.localhost/ -> HTTP Error 404. The requested resource is not found. http://api.service.localhost/ -> HTTP Error 404. The requested resource is not found.

The only link that actually works:唯一有效的链接:

http://api.service.localhost:8080/ -> Traefik UI http://api.service.localhost:8080/ -> Traefik UI

If I would include:如果我包括:

ports:
  - "8070:80"

into api-service part of docker-compose.yml I could access my 'Hello-Word': http://localhost:8070/ -> "Hello-Word" In this case it not get routed through Traefik but directly through api-service .进入docker-compose.ymlapi-service部分,我可以访问我的“Hello-Word”: http://localhost:8070/ -> "Hello-Word"在这种情况下,它不会通过 Traefik 路由,而是直接通过api-service路由。 Is it possible to do it over Traefik?是否可以通过 Traefik 做到这一点?

To have a service routed by traefik with docker, you should have labels.要让 traefik 使用 docker 路由服务,您应该有标签。 Traefik will fetch those labels in order to know how to do routing. Traefik 将获取这些标签以了解如何进行路由。

Thus, to have request of api.service.localhost routed to your api service, you should have the label that define the corresponding rule :因此,要将api.service.localhost的请求路由到您的 api 服务,您应该拥有定义相应规则的 label :

traefik.http.routers.service.rule=Host(`api.service.localhost`)

As for the label of Traefik, you should have two labels:至于Traefik的label,应该有两个标签:

traefik.http.routers.traefik.rule=Host(`traefik.service.localhost`)
traefik.http.routers.traefik.service=api@internal

Beside those traefik labels, you should tell traefik to expose the dashboard using a specific service by adding this configuration: --api.dashboard (More help to expose the dashboard here ).除了这些 traefik 标签之外,您应该告诉 traefik 通过添加以下配置来使用特定服务公开仪表板:-- --api.dashboard (在此处公开仪表板的更多帮助)。

Note, if your api service uses another port than the one by default, you could add this label:注意,如果您的 api 服务默认使用另一个端口,您可以添加此 label:

traefik.http.services.service.loadbalancer.server.port=80 

Ps, do not expose twice the port 80, you only need to expose it for the traefik container. ps,不要暴露两次80端口,只需要为traefik容器暴露即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM