Traefik 2.0+ TCP 和 postgres

Traefik 2.0+ TCP and postgres

我正在尝试为路由 postgres 实例设置 traefik。 我认为我必须使用 treafik 2.0 中的新 TCP 功能。但是我正在努力弄清楚。

有人有任何提示或工作示例吗?

我的起点是 "getting started" 部分,并试图包含一个 postgres 数据库。我可以访问 whoami 实例,但不能访问 postgres 实例

docker-compose.yaml

version: '2'

services:
  reverse-proxy:
    image: traefik:v2.0.0-alpha3 # The official v2.0 Traefik docker image
    ports:
      - "80:80"     # The HTTP port
      - "8080:8080" # The Web UI (enabled by --api)
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock # So that Traefik can listen to the Docker events
      - /home/mariufa/tmp/traefik.toml:/etc/traefik/traefik.toml

  whoami:
    image: containous/whoami # A container that exposes an API to show its IP address
    labels:
      - "traefik.http.routers.whoami.rule=Host(`whoami.docker.localhost`)"

  postgres:
    image: postgres:latest
    labels:
      - "traefik.tcp.routers.postgres.rule=HostSNI(`postgres.docker.localhost`)"

traefik.toml

[api]

[entrypoints]
  [entrypoints.web]
    address = ":80"

[providers.docker]
endpoint = "unix:///var/run/docker.sock"

正在测试我的 postgres 连接:

psql -h postgres.docker.localhost -U postgres -d postgres -p 80

如果我设置 HostSNI('*'),这会起作用,但不是真正的解决方案。 还用 "Host" 而不是 "HostSNI"

进行测试

所以我检查了 TCP,了解到主机名上的路由是一个 http 功能。无赖!所以然后决定有一个共同的主机名,然后为不同的数据库提供公开的随机端口

所以我对数据库即服务的看法是 Rundeck 用于部署 postgres 和 mongodb docker 容器,并让 docker 选择随机发布端口。这不需要 Traefik。仅将 Traefik 用于我的前端和 api 的

如何将 postgres 端口映射到随机端口的示例:

docker run -d -p 5432 postgres

因为我不想每次忘记使用 ssh 和 运行 docker ps 来检查我的数据库端口,所以我找到了一个漂亮的 docker监控,DockWatch.

它显示我的 docker 容器的端口、日志等。非常方便地解决我的数据库即服务情况。

docker 个集装箱中的 Rundeck 和 DockWatch。