使用Traefik在docker v2.2上进行端口转发

时间:2020-07-29 15:17:40

标签: docker docker-compose traefik dgraph

我有一个在端口8080上运行的服务,该服务同时接受http和gRPC。我了解traefik中的gRPC存在一些限制,因此这是最终目标。

  1. 在端口8080上接受ipWhitelisted的流量
  2. 接受来自:80 / graphql的流量并路由至:8080 / graphql
  • 最终我想在/路线上以及/ graphql路线上接受这一点
  1. 接受来自:80 / admin / schema的ipWhitelisted流量,并路由至:8080 / admin / schema
  • 最终我想在/ admin路由和/ graphql路由上接受此请求

如果我能做到这一点,我相信我可以完成下一部分:

  1. 通过letencrypt启用https,并接受从:443到/ graphql和/ admin端点的流量。

对于配置,我正在使用docker compose文件。

version: "3.2"
services:
  reverse-proxy:
    image: traefik:v2.2
    command:
      - "--providers.docker=true"
      - "--providers.docker.exposedbydefault=false"
      - "--entrypoints.dgraph.address=:8080"
    ports:
      - "8080:8080"
    volumes:
      - "/var/run/docker.sock:/var/run/docker.sock:ro"
  # there is another service here `zero` that is not using traefik so I ommitted its config.  
  alpha:
    image: dgraph/dgraph:master
    volumes:
      - /dgraph/data:/dgraph
    labels:
      - "traefik.enable=true"
      - "traefik.http.middlewares.adminIps.ipwhitelist.sourcerange=1.1.1.1" # my ip address instead of 1.1.1.1
      - "traefik.http.routers.alpha.rule=Host(`api.mydomain.com`) && Path(`/graphql`)"
      - "traefik.http.routers.alpha.entrypoints=dgraph"
      - "traefik.http.routers.schema.rule=Host(`api.mydomain.com`) && Path(`/admin/schema`)"
      - "traefik.http.routers.schema.middlewares=adminIps@docker"
      - "traefik.http.routers.schema.entrypoints=dgraph"
      - "traefik.http.routers.all.rule=Host(`api.mydomain.com`)"
      - "traefik.http.routers.all.middlewares=adminIps@docker"
      - "traefik.http.routers.all.entrypoints=dgraph"
    restart: always
    command: dgraph alpha --my=alpha:7080 --zero=zero:5080 --whitelist 172.0.0.0:172.254.254.254

我尝试在端口80上创建另一个入口点,然后在路由器中使用该入口点,并添加了负载均衡器,但这似乎不起作用。这是修改后的配置:

version: "3.2"
services:
  reverse-proxy:
    image: traefik:v2.2
    command:
      - "--providers.docker=true"
      - "--providers.docker.exposedbydefault=false"
      - "--entrypoints.dgraph.address=:8080"
      - "--entrypoints.web.address=:80"
    ports:
      - "8080:8080"
      - "80:80"
    volumes:
      - "/var/run/docker.sock:/var/run/docker.sock:ro"
  # there is another service here `zero` that is not using traefik so I ommitted its config.  
  alpha:
    image: dgraph/dgraph:master
    volumes:
      - /dgraph/data:/dgraph
    labels:
      - "traefik.enable=true"
      - "traefik.http.middlewares.adminIps.ipwhitelist.sourcerange=1.1.1.1" # my ip address instead of 1.1.1.1
      - "traefik.http.routers.alpha.rule=Host(`api.mydomain.com`) && Path(`/graphql`)"
      - "traefik.http.routers.alpha.entrypoints=web"
      - "traefik.http.services.alpha.loadbalancer.server.port=80"
      - "traefik.http.routers.schema.rule=Host(`api.mydomain.com`) && Path(`/admin/schema`)"
      - "traefik.http.routers.schema.middlewares=adminIps@docker"
      - "traefik.http.routers.schema.entrypoints=dgraph"
      - "traefik.http.routers.all.rule=Host(`api.mydomain.com`)"
      - "traefik.http.routers.all.middlewares=adminIps@docker"
      - "traefik.http.routers.all.entrypoints=dgraph"
    restart: always
    command: dgraph alpha --my=alpha:7080 --zero=zero:5080 --whitelist 172.0.0.0:172.254.254.254

上面的第一个docker-compose.yml文件有效。所谓工作,是指它正确地将路由,规则和中间件全部应用于端口8080。

上面的第二个docker-compose.yml文件我希望打开端口80并应用规则将http api.mydomain.com/graphql路由到alpha:8080 / graphql。但是,这不会发生。当我用docker-compose up -d向上推时没有出现任何错误,但是当我使用yougetsignal.com并检查端口80是否打开时,得到的响应是端口80已关闭,而当我尝试使用端口8080时,在使用路由全部(api.mydomain.com:8080)之前,我在浏览器“ Bad Gateway”中收到响应

1 个答案:

答案 0 :(得分:0)

您说您的服务Alpha正在监听8080,因此您应该在loadbalancer中使用它:

"traefik.http.services.alpha.loadbalancer.server.port=8080"

您可以这样想:entrypoint是传入连接,loadbalancer是Traefik重定向请求的位置。