Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Traefik 2.0+ TCP and postgres

Tags:

traefik

I am trying to setup traefik for routing postgres instances. I have figured I have to use the new TCP feature in treafik 2.0. However I am struggling to figure it out.

Anyone got any tips or working examples?

My staring point is the "getting started" section, and tried to include a postgres db. I am able to reach the whoami instance, but not the postgres instance

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"

Testing my postgres connection with:

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

This works if I set HostSNI('*'), but not a real solution. Also testet with "Host" instead of "HostSNI"

like image 521
marfagerland Avatar asked Apr 03 '19 12:04

marfagerland


1 Answers

So I checked up about TCP, and leaned that routing on hostname is a http feature. Bummer!! So then decided to have a common hostname and then give expose random ports for the different DB's

So my take on DB as a service is Rundeck for deploying postgres and mongodb docker containers, and letting docker choose random publish port. No Traefik needed for this. Only using Traefik for my frontend's and api's

Example of how to map postgres port to a random port:

docker run -d -p 5432 postgres

Since I don't want to use ssh and run docker ps to check my db ports every time I forget them, I found a nifty docker monitor, DockWatch.

It displays ports, logs, etc for my docker containers. So very handy for solving my DB as a service situation.

Both Rundeck and DockWatch in docker containers as well.

like image 179
marfagerland Avatar answered Jan 02 '23 09:01

marfagerland