Hello. I have an issue with Traefik's load balancer.
I did docker-compose with a Traefik container and three replicas of whoami containers, both of which are from official Traefik's Docker images. Traefik uses file provider. It is configured to reverse-proxy requests to the url of whoami service (Here, the service is in the context of Docker Compose, not of Traefik). In this case, url: "http://whoami"
.
Since Docker has a build-in functionality of load-balancing requests between replicas of the same service, the request which is reverse-proxied by Traefik to whoami should be load-balanced between its replicas, even though I specified only single url in Traefik's dynamic configuration (dynamic.yml in the following code).
However, when I send a request to whoami through Traefik, it always returns same responses from the same container, instead of one randomly chosen from replicas. Looks like load balancer isn't working.
It can do failover though. When I turned off one of the replicas of whoami container, Traefik switched the destination of requests and forwarded to one of the remaining replicas.
At first, I thought this was a problem of Docker, so I entered inside Traefik's container and ran the command: curl http://whoami
. But it successfully returned random responses, which means Docker's load balancer was working fine.
I guess Traefik internaly caches the actual ip address of the container instead of Docker's service name and thus ends up always forwarding requests to the same container.
Does anyone know anything?
How to replicate (picture)
How to replicate (code)
docker-compose.yml
services:
reverse-proxy:
image: traefik:v2.9
container_name: reverse-proxy
hostname: reverse-proxy
entrypoint: traefik
command: --configfile /space/static.yml
ports:
- "80:80"
volumes:
- ./traefik/:/space/
whoami:
image: traefik/whoami
deploy:
replicas: 3
static.yml
providers:
file:
directory: /space/
watch: true
entryPoints:
web:
address: :80
dynamic.yml
http:
routers:
whoamiRouter:
entryPoints:
- web
rule: PathPrefix(`/`)
service: whoami
services:
whoami:
loadBalancer:
servers:
- url: "http://whoami"