Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Traefik v2.1.4 - How to create a static route and redirect to a specific host and port

I'm a beginner with Traefik v2.1.4. I'm using in a docker container. I'm trying to set up a static route. I found some examples using the toml configuration file.

[providers]
[providers.file]
[http]
    [http.routers]
        [http.routers.netdata]
            rule = "Host(`netdata.my-domain.com`)"
            service = "netdata"
            entrypoint=["http"]

    [http.services]
          [http.services.netdata.loadbalancer]
            [[http.services.netdata.loadbalancer.servers]]
              url = "https://192.168.0.2:19999"

Following this example I would like to convert it to docker labels of my docker-compose.

My docker-compose file:

version: "3.7"
services:

  traefik:
    image: traefik:v2.1.4
    container_name: traefik
    restart: always
    command:
      - "--log.level=DEBUG"
      - "--api.insecure=false"
      - "--providers.docker=true"
      - "--providers.docker.exposedbydefault=false"
      - "--entrypoints.web.address=:80"
      - "--entrypoints.websecure.address=:443"
      - "--certificatesresolvers.letsresolver.acme.tlschallenge=true"
      - "--certificatesresolvers.letsresolver.acme.email=my-email@domain.com"
      - "--certificatesresolvers.letsresolver.acme.storage=/letsencrypt/acme.json"
    labels:
      - "traefik.enable=true"
      # middleware redirect
      - "traefik.http.middlewares.redirect-to-https.redirectscheme.scheme=https"
      # global redirect to https
      - "traefik.http.routers.redirs.rule=hostregexp(`{host:.+}`)"
      - "traefik.http.routers.redirs.entrypoints=web"
      - "traefik.http.routers.redirs.middlewares=redirect-to-https" 
      # dashboard
      - "traefik.http.routers.traefik.rule=Host(`traefik.my-domain.com`)"
      - "traefik.http.routers.traefik.service=api@internal"
      - "traefik.http.routers.traefik.middlewares=admin"
      - "traefik.http.routers.traefik.tls.certresolver=letsresolver"
      - "traefik.http.routers.traefik.entrypoints=websecure"
      - "traefik.http.middlewares.admin.basicauth.users=user:hash-passwordXXX"

    ports:
      - "80:80"
      - "443:443"
      - "8080:8080"
    volumes:
      - "./letsencrypt:/letsencrypt"
      - "/var/run/docker.sock:/var/run/docker.sock:ro"


 networks:
    default:
        external:
            name: network
like image 722
Edson Junior Avatar asked Oct 16 '22 05:10

Edson Junior


2 Answers

It is possible to use 2 providers together: file and docker.

Your docker-compose.yml:

services:
  traefik:
    image: traefik:2.2.1
    command: traefik --configFile=/etc/traefik/traefik.yml
    ports:
      - "80:80"
      - "8080:8080"
    volumes:
      - ./traefik.yml:/etc/traefik/traefik.yml
      - ./routes.yml:/etc/traefik/routes.yml
      - /var/run/docker.sock:/var/run/docker.sock

   # your services go here ...

Your traefik.yml:

api:
  dashboard: true
  insecure: true

entryPoints:
  web:
    address: :80

providers:
  docker: {}

  file:
    filename: /etc/traefik/routes.yml
    watch: true

Your routes.yml:

http:
  routers:
    hello:
      rule: PathPrefix(`/hello`)
      service: hello@docker
      rule: PathPrefix(`/world`)"
      service: world@docker

These are only examples, don't use them in production environment directly, of course.

like image 140
Nick Roz Avatar answered Nov 20 '22 13:11

Nick Roz


There is no docker label mentioned to specify url (in https://docs.traefik.io/v2.1/routing/providers/docker/#routers). And I tried to use url instead of port, but it does not work.
So I suggest to use the file provider (https://docs.traefik.io/v2.1/providers/file/).

suggeston for implementation:

update your config with:

services:
  ...
  traefik:
    ...
    command:
      ...
      - "--providers.file.directory=/path/to/dynamic/conf"
    config:
    - source: redirect.toml
      target: /path/to/dynamic/conf/redirect.toml
    ...

...
configs:
  redirect.toml:
    file: redirect.toml

and create redirect.toml with your redirection (as in your example).

Of course you can also bindmount the config into the container, or create your own traefik image containing the config, or ...


In case you want to work with labels, you can start a service which redirects with socat

services:
  ...

  netdata:
    image: alpine/socat
    command: tcp-listen:80,fork,reuseaddr tcp-connect:192.168.0.2:19999
    deploy:
      labels:
        traefik.enable: "true"
        traefik.http.routers.netdata.rule: Host(`netdata.my-domain.com`)
        traefik.http.services.netdata_srv.loadbalancer.server.port: 80
        # hm, and probably tell to forward as https, ...
like image 44
simohe Avatar answered Nov 20 '22 14:11

simohe