Backup server configuration

Hello, everyone!

I wanted to finally start using traefik as there is a cool opportunity for me to test it.
I was struggling 3 days already with it and reading all documentation few times.

The problem is:

I have one service which is my instance of some important backend. It's free and I want to use it primarily. But there is a paid version, which I only want to use when my instance is down.

I tried 20+ different configurations already, still can't make the right one.

I want traefik to send all requests to one service, and only if it's unavailable redirect to other.

The most similar behaviour in documentation is CircuitBraker middleware, but you can't setup fallback service in there.

Thank you in advance for helping!

I have done this while moving a file provider service to a docker service. This may not end up being a solution for what you are asking for.

Create the same router rule for the service. But for your preferred service add a priority label.
While the Prioritsed container is running that is where the requests are routed.

I am sure the same thing should be possible with file provider only.

Test it out and let me know how you get on.

Example Config
version: "3.7"
services:
  traefik:
    image: traefik:2.2
    ports:
      - '80:80'
      - '443:443'
      - '8080:8080'
    command:
      - --entrypoints.http.address=:80
      - --entrypoints.https.address=:443
      - --entrypoints.http8080.address=:8080
      - --api=true
      - --providers.docker=true
    labels:
      - "traefik.http.routers.api.entrypoints=http8080"
      - "traefik.http.routers.api.rule=HostRegexp(`{any:.*}`)"
      - "traefik.http.routers.api.service=api@internal"
    volumes:
      - "/var/run/docker.sock:/var/run/docker.sock:ro"
  whoami:
    image: whoami
    labels:
      - "traefik.http.routers.who.rule=PathPrefix(`/whoami`)"
      - "traefik.http.routers.who.middlewares=who"
      - "traefik.http.middlewares.who.stripprefix.prefixes=/whoami"
  primary:
    image: nginx
    labels:
      - "traefik.http.routers.pri.rule=PathPrefix(`/failback`)"
      - "traefik.http.routers.pri.priority=300"
    volumes:
      - "./pri:/usr/share/nginx/html"
  secondary:
    image: nginx
    labels:
      - "traefik.http.routers.sec.rule=PathPrefix(`/failback`)"
    volumes:
      - "./sec:/usr/share/nginx/html"

I use file provider so it's ok for me

I tried to set priority to fallback mechanism, maybe I made wrong rules, I'll check your file know and let you know.

@cakiwi

This is my configuration file with priorities idea

If first service is down, then Bad Gateway is returned instead of request being served by second service

http:
  routers:
    routerMain:
      entryPoints:
        - jsonrpc
      service: service-whoami-one
      rule: HostRegexp(`{any:.*}`)
    routerFallback:
      entryPoints:
        - jsonrpc
      service: service-whoami-two
      rule: HostRegexp(`{any:.*}`)
      priority: 1
  services:
    service-whoami-one:
      loadBalancer:
        servers:
        - url: http://whoami-one/
        passHostHeader: true
    service-whoami-two:
      loadBalancer:
        servers:
        - url: http://whoami-two/
        passHostHeader: true

@Alirun

Yah. I could only replicate this with full docker provider or 1 docker 1 file provider.
With both services on file provider, I get the same as you.

Could someone else take a look please? I really don't want to switch to nginx :smile:

you cannot fallback on router, you have to add your servers in one service and add a health check.

http:
  routers:
    routerMain:
      entryPoints:
        - jsonrpc
      service: service-whoami-one
      rule: HostRegexp(`{any:.*}`)

  services:
    service-whoami-one:
      loadBalancer:
        healthCheck:
          path: /health
          interval: "10s"
          timeout: "3s"
        servers:
        - url: http://whoami-one/
          passHostHeader: true
  
        - url: http://whoami-two/
          passHostHeader: true
 

https://docs.traefik.io/v2.2/routing/services/#health-check

or play with the WRR: https://docs.traefik.io/v2.2/routing/services/#weighted-round-robin-service

Hello, thank you! But I don't want to load balance between servers. Because backup server is paid API which I'm willing to pay only in case I have fault in my primary server.