Difficulty: beginner
Estimated Time: 10-15 minutes

In this scenario you'll learn how to use Træfɪk to load balance HTTP traffic between different Docker containers.

Træfɪk is a modern HTTP reverse proxy and load balancer designed for deploying microservices. Træfɪk has support for multiple backend providers, including Docker, to automatically configure the environment. As new services, or containers, are added and removed, the load balancer will reshape the traffic to match.

Architecture Image source https://traefik.io/

Congratulations! You've successfully deployed the Traefik load balancer for managing traffic to Docker container. More details on traefik can be found at https://traefik.io/

Don’t stop now! The next scenario will take about 20 minutes to complete. You can pause and come back at any time.

Load Balance Containers using Traefik

Step 1 of 6

Step 1 - Define Traefik Container

This scenario uses Docker Compose to define the architecture, and Docker Container labels to define the metadata and rules for routing.

Create Traefik Container

Start creating the Docker Compose file by first defining the load balancer.

traefik:
  image: traefik
  command: --web --docker --docker.domain=docker.localhost --logLevel=DEBUG
  ports:
    - "80:80"
    - "8080:8080"
    - "443:443"
  volumes:
    - /var/run/docker.sock:/var/run/docker.sock
    - /dev/null:/traefik.toml

The Load Balancer is configured to list on ports 80 and 443 for HTTP traffic. It also has a web dashboard available on port 8080. The dashboard should be considered internal and restricted by a firewall.

The use of docker.sock allows traefik to listen to the Docker Host Daemon Events and reconfigure itself when containers are started/stopped. It also uses this to find additional metadata about the containers, such as which domain name they process.

docker-compose.yml