4

I am trying to use Traefik as a reverse proxy for MariaDB so I can connect from my Client.

Currently Traefik is working fine with HTTP and HTTPS for multiple WordPress Container but i am having trouble configuring it for MariaDB.

Here is the current config:

Traefik Compose File:

version: '3.5'

 networks:
  traefik:
    name: traefik


services:
  traefik:
    image: traefik:latest
    restart: always
    container_name: traefik
    volumes:
      - /etc/localtime:/etc/localtime:ro
      - /var/run/docker.sock:/var/run/docker.sock:ro
      - ./traefik.toml:/traefik.toml:ro
      - ./acme.json:/acme.json
    ports:
      - 80:80
      - 443:443
      - 3306:3306
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.traefik.rule=Host(`traefik.local`)"
      - "traefik.http.routers.traefik.entrypoints=websecure"
      - "traefik.http.routers.traefik.service=api@internal"
      - "traefik.http.routers.traefik.middlewares=auth"
      - "traefik.http.middlewares.auth.basicauth.users=username:$$apr1$$j994eiLb$$KmPfiii4e9VkZwTPW2/RF1"
    networks:
      - traefik

Traefik Configuration File (traefik.toml):

# Network traffic will be entering our Docker network on the usual web ports
# (ie, 80 and 443), where Traefik will be listening.
[entyPoints]
  [entryPoints.web]
    address = ":80"

  [entryPoints.websecure]
    address= ":443"
    [entryPoints.websecure.http.tls]
      certResolver = "resolver"

#  [entryPoints.ssh]
#    address = ":2222"

  [entryPoints.mariadb]
    address = ":3306"

    #Redirection from HTTP to HTTPS
    [entryPoints.web.http]
      [entryPoints.web.http.redirections]
        [entryPoints.web.http.redirections.entryPoint]
        to = "websecure"
        scheme = "https"


#Integration with Let's Encrypt
[certificatesResolvers.resolver.acme]
  email = "service@local"
  storage = "acme.json"
  [certificatesResolvers.resolver.acme.tlsChallenge]

#[log]
#  level = "DEBUG"


[api]
  #Defaul=true
  dashboard = true


# Enable retry sending request if network error
[retry]


# These options are for Traefik's integration with Docker.
[providers.docker]
  endpoint = "unix:///var/run/docker.sock"
  exposedByDefault = false
  network = "traefik"

MariaDB Compose File: version: '3.5'

networks:
  traefik:
    external:
      name: traefik


services:
  dbtest:
    image: mariadb:latest
    restart: always
    container_name: dbtest
    environment:
      - MYSQL_DATABASE=admin
      - MYSQL_USER=admin
      - MYSQL_PASSWORD=admin
      - MYSQL_ROOT_PASSWORD=admin
    networks:
      - traefik
    labels:
      - "traefik.enable=true"
      - "traefik.docker.network=traefik"
      - "traefik.tcp.routers.mariadb.entrypoints=mariadb"
      - "traefik.tcp.routers.mariadb.rule=HostSNI(`test.local`)"
      - "traefik.tcp.routers.mariadb.tls=true"
#      - "traefik.tcp.routers.mariadb.service=dbtest"
#      - "traefik.tcp.services.mariadb.loadbalancer.server.port=3306"

When I try to connect to the database from my Client it doesn't work

Anyone having experience or a good example for that?

2 Answers 2

9

Looks like it is not possible to specify a Hostname like test.local. Instead you need to use a catchall *.

The labels I used for MariaDB are:

labels:
  - "traefik.enable=true"
  - "traefik.tcp.routers.mariadb.rule=HostSNI(`*`)"
  - "traefik.tcp.routers.mariadb.entrypoints=mariadb"
  - "traefik.tcp.routers.mariadb.service=mariadb-svc"
  - "traefik.tcp.services.mariadb-svc.loadbalancer.server.port=3306"
2
  • Yes, seems only supports "*" in HostSNI and cannot specify the host name Commented Oct 13, 2020 at 13:05
  • I followed your steps, but how do I connect from for example MySQL workbench? Do I simple type my VPS IP with :3306 ?
    – Broump
    Commented Mar 18, 2023 at 7:22
1

I use the following in a docker-compose file. Of course you can adjust port number to whatever you want.

Static Configuration:

      traefik:
        ports:    
          # db - postgres
          - 5432:5432
        
    
        # This override command section REPLACES the one in the docker-compose file.  
        command:
          - --providers.docker
          - --providers.docker.exposedbydefault=false
    
          - --accesslog
          - --log
          - --api
    
          # These create named entry points for later use in routers.
          # You don't need to specify an entrypoint if the in port = out port.  It will
          # automatically figure that out.
          - --entryPoints.postgres.address=:5432

Dynamic Configuration:

    db:
      labels:
        - traefik.enable=true
        - traefik.docker.network=traefik-public
        - traefik.tcp.routers.db-tcp.rule=HostSNI(`*`)
        - traefik.tcp.routers.db-tcp.entrypoints=postgres
        - traefik.tcp.routers.db-tcp.service=db-proxy
        - traefik.tcp.services.db-proxy.loadbalancer.server.port=5432

Not the answer you're looking for? Browse other questions tagged or ask your own question.