Optimizing Docker Compose Load Balancing with Traefik: Scenario and Configuration

In this scenario, let's consider a Docker Compose setup with Traefik for load balancing across two frontend containers, frontend1 and frontend2. The objective is to configure Traefik such that subsequent requests from a client within a specific time frame are directed to the same container, thereby enhancing resource utilization and optimizing performance.

frontend1:
  labels:
    - traefik.enable=true
    - traefik.http.routers.frontendsite.rule=Host(`foobar.com`) || Host(`www.foobar.com`)
    - traefik.http.services.frontendsite.loadbalancer.server.port=3001
    - traefik.docker.network=supabase

frontend2:
  labels:
     - traefik.enable=true
     - traefik.http.services.frontendsite.loadbalancer.server.port=3001
     - traefik.docker.network=supabase

Now, let's consider a scenario where a client makes a series of requests to the frontend containers. We want Traefik to ensure that subsequent requests from the same client within a specified time frame are routed to the same container.

Scenario:

  1. Client A makes an initial request to the load balancer and is routed to frontend1.
  2. Subsequent requests from Client A within the next hour should be directed to frontend1 to maintain session persistence and optimize resource usage.
  3. After the specified time frame (e.g., one hour), if Client A makes a request again, Traefik may route it to either frontend1 or frontend2 based on load balancing rules.

Configuration Objective:

  • Implement session persistence for subsequent requests from the same client within a specified time frame to the same container.
  • Enhance performance and resource utilization by optimizing container usage based on client sessions.

Your insights on achieving this configuration effectively are appreciated. Thank you.

@bluepuma77

Check "stick sessions" (link).

Hello @bluepuma77



frontend1:
  labels:
    - traefik.enable=true
    - traefik.http.routers.frontendsite.rule=Host(`foobar.com`) || Host(`www.foobar.com`)
    - traefik.http.services.frontendsite.loadbalancer.server.port=3001
    - traefik.docker.network=supabase
    - "traefik.http.services.frontendsite.loadbalancer.sticky.cookie.name=sticky-session-test"
    - "traefik.http.services.frontendsite.loadbalancer.sticky.cookie.secure=true"
     - "traefik.http.services.frontendsite.loadbalancer.sticky.cookie.httpOnly=true"
     - "traefik.http.services.frontendsite.loadbalancer.sticky.cookie.sameSite=none"

frontend2:
  labels:
     - traefik.enable=true
     - traefik.http.services.frontendsite.loadbalancer.server.port=3001
     - traefik.docker.network=supabase

i did like that is that correct for you ?

Spacing doesn't look consistent.

There is a label ”example” in the Traefik Docker dynamic reference:

  - "traefik.http.services.service02.loadbalancer.sticky=true"
  - "traefik.http.services.service02.loadbalancer.sticky.cookie=true"
  - "traefik.http.services.service02.loadbalancer.sticky.cookie.httponly=true"
  - "traefik.http.services.service02.loadbalancer.sticky.cookie.name=foobar"
  - "traefik.http.services.service02.loadbalancer.sticky.cookie.samesite=foobar"
  - "traefik.http.services.service02.loadbalancer.sticky.cookie.secure=true"

You might need to assign the service to the router. But usually this is not necessary when a single router and service is used on a service/container.

Hello @bluepuma77

I make like that

              - traefik.enable=true
              - "traefik.http.routers.frontendsitetest.rule=(Host(`test.foobar.com`)) && !PathRegexp(`^/(sitemap?.+|rss?.+)`)"
              - traefik.http.routers.frontendsitetest.entrypoints=websecure
              - traefik.http.routers.frontendsitetest.tls=true
              - traefik.http.routers.frontendsitetest.tls.certresolver=myresolver
              - traefik.http.routers.frontendsitetest.service=frontendsitetest
              - traefik.http.routers.frontendsitetest.priority=800
              - traefik.http.services.frontendsitetest.loadbalancer.server.port=3001
              - traefik.http.routers.frontendsitetest.middlewares=mywwwredirect
              - traefik.docker.network=supabase ## overlay network
              - traefik.http.services.frontendsitetest.loadbalancer.sticky=true
              - traefik.http.services.frontendsitetest.loadbalancer.sticky.cookie=true
              - traefik.http.services.frontendsitetest.loadbalancer.sticky.cookie.name=sticky-session-test
              - traefik.http.services.frontendsitetest.loadbalancer.sticky.cookie.secure=true
              - traefik.http.services.frontendsitetest.loadbalancer.sticky.cookie.httpOnly=false
              - traefik.http.services.frontendsitetest.loadbalancer.sticky.cookie.sameSite=none

But I still getting 404 page.
And I take my domain from cloudflare for this reason I make httponly false option. I try true option to.
But I couldnt figure out.
Which logs should I look at ?

after many hours research I found it

app-test-1:
    build:
      context: /opt/my-app
      dockerfile: ~/scripts/docker/Dockerfile
    image: myapp:build1
    container_name: app-test-1
    networks:
      - backend
    environment:
      - PORT=3000
      - METEOR_SETTINGS
    ports:
      - 3000:3000
    labels:
      - traefik.enable=true
      - traefik.docker.network=backend
      - traefik.http.routers.app-test.rule=Host(`test.mydomain.com`)
      - traefik.http.routers.app-test.entrypoints=web
      - traefik.http.services.app-test.loadbalancer.server.port=3000
      - traefik.http.services.app-test.loadbalancer.sticky=true

  app-test-2:
    build:
      context: /opt/my-app
      dockerfile: ~/scripts/docker/Dockerfile
    image: myapp:build1
    container_name: app-test-2
    networks:
      - backend
    environment:
      - PORT=3002
      - METEOR_SETTINGS
    ports:
      - 3002:3002
    labels:
      - traefik.enable=true
      - traefik.docker.network=backend
      - traefik.http.routers.app-test.rule=Host(`test.mydomain.com`)
      - traefik.http.routers.app-test.entrypoints=web
      - traefik.http.services.app-test.loadbalancer.server.port=3002
      - traefik.http.services.app-test.loadbalancer.sticky=true

if you using you need define every container

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.