Træfɪk is a modern HTTP reverse proxy and load balancer made to deploy microservices with ease. It supports several backends (Docker, Swarm, Mesos/Marathon, Consul, Etcd, Zookeeper, BoltDB, Rest API, file...) to manage its configuration automatically and dynamically.
Imagine that you have deployed a bunch of microservices on your infrastructure. You probably used a service registry (like etcd or consul) and/or an orchestrator (swarm, Mesos/Marathon) to manage all these services. If you want your users to access some of your microservices from the Internet, you will have to use a reverse proxy and configure it using virtual hosts or prefix paths:
api.domain.comwill point the microservice
apiin your private network
domain.com/webwill point the microservice
webin your private network
backoffice.domain.comwill point the microservices
backofficein your private network, load-balancing between your multiple instances
But a microservices architecture is dynamic... Services are added, removed, killed or upgraded often, eventually several times a day.
Traditional reverse-proxies are not natively dynamic. You can't change their configuration and hot-reload easily.
Here enters Træfɪk.
Træfɪk can listen to your service registry/orchestrator API, and knows each time a microservice is added, removed, killed or upgraded, and can generate its configuration automatically. Routes to your services will be created instantly.
Run it and forget it!
You can have a quick look at Træfɪk in this Katacoda tutorial that shows how to load balance requests between multiple Docker containers.
./traefik -c traefik.toml
Using the tiny Docker image:
docker run -d -p 8080:8080 -p 80:80 -v $PWD/traefik.toml:/etc/traefik/traefik.toml traefik
You can test Træfɪk easily using Docker compose, with this
traefik: image: traefik command: --web --docker --docker.domain=docker.localhost --logLevel=DEBUG ports: - "80:80" - "8080:8080" volumes: - /var/run/docker.sock:/var/run/docker.sock - /dev/null:/traefik.toml whoami1: image: emilevauge/whoami labels: - "traefik.backend=whoami" - "traefik.frontend.rule=Host:whoami.docker.localhost" whoami2: image: emilevauge/whoami labels: - "traefik.backend=whoami" - "traefik.frontend.rule=Host:whoami.docker.localhost"
Then, start it:
docker-compose up -d
Finally, test load-balancing between the two servers
$ curl -H Host:whoami.docker.localhost http://127.0.0.1 Hostname: ef194d07634a IP: 127.0.0.1 IP: ::1 IP: 172.17.0.4 IP: fe80::42:acff:fe11:4 GET / HTTP/1.1 Host: 172.17.0.4:80 User-Agent: curl/7.35.0 Accept: */* Accept-Encoding: gzip X-Forwarded-For: 172.17.0.1 X-Forwarded-Host: 172.17.0.4:80 X-Forwarded-Proto: http X-Forwarded-Server: dbb60406010d $ curl -H Host:whoami.docker.localhost http://127.0.0.1 Hostname: 6c3c5df0c79a IP: 127.0.0.1 IP: ::1 IP: 172.17.0.3 IP: fe80::42:acff:fe11:3 GET / HTTP/1.1 Host: 172.17.0.3:80 User-Agent: curl/7.35.0 Accept: */* Accept-Encoding: gzip X-Forwarded-For: 172.17.0.1 X-Forwarded-Host: 172.17.0.3:80 X-Forwarded-Proto: http X-Forwarded-Server: dbb60406010d