Using Traefik As a Load Balancer / HTTP Reverse Proxy For Micro-Services

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading...

Traefik as an HTTP reverse proxy / load balancer for Micro-Services



Below, I am going to show you how to configure Traefikas an HTTP reverse proxy / Load Balancer for your micro-services.

There are a number of Load Balancer options to choose from. Commercial hardware load balancers, like F5 LTM, Netscaller, A10, etc… Or software, like Nginx, HAporxy, etc… Most of those load balancers are designed primarily to handle legacy traffic work-loads and are not the best fit for a micro-services architecture for many reasons.

In a micro-services architecture (using Docker, Kubernetes and such) where services are dynamic, come and go all the time, we need a load balancer that re-acts dynamically to these changes. auto-detects new, removed services, without any user intervention. for those reasons (and more) Traefik was created.

Note: While Traefik (today) might not be the fastest load balancer (it is defiantly getting faster by the day), it still is (I believe) one of the best options in a micro-services architecture.

To name just a few of the many features Traefik supports.

  1. Auto Service-Discovery connections like Consul, etcd, Zookeeper, etc..
  2. Fully integrated – auto generate an ACME SSL signed certificate
  3. Works well with Docker Swarm, Kubernetes, Mesos/Marathon
  4. Full Web Sockets support
  5. HTTP/2
  6. Updates with a ResetAPIs

tip: Traefik is little application written in the Go language, therefor is well integrated in the rest of the Go applications like Docker, etc…

Now that we know what benefits Traefik brings to the table, lets jump right into the configuration.

Installing Traefik – Getting Traefik to work

For the most part, installing Traefik is rely simple.

First, lets create a working directory.

Next, lets download the Traefik binary.

Make the binary executable

Tip: Use the traefik.sample.toml as a reference for many of the traefik options available.

Configuring an initial Traefik test template

Create a template file, like the below.
cat traefik.toml

Next, create the rules file
cat rules.toml

Now, Run traefik in test mode on a regular system (not from a container).

To test just try accessing the traefik web dashboard.
The dashboard will be available on port 8095 and the application port is on port 8090.
For example.

To test Traefik with docker, use the below configuration.

Using Traefik with Docker-Compose or Docker-Stack

The example below shows how to use Traefik with Docker-Compose, with the scaling feature, at the end of the setup, I will also show you how to scale manually.

Creating a Docker-Compose Traefik configuration

The below docker-compose is using Traefik as the proxy, and emilevauge/whoami Docker image as the web application. the emilevauge/whoami will return the host and header information connected to.
cat docker-compose.yml

Now, just run the below to bring up the containers

To access the dashboard, just connect to port 7080, like the below.

Notes on the above configuration:

  • The above configuration will pull the Docker Traefik image (if not local)
  • Configures and starts Traefik on port 7080 – forward to port 8080
  • Creates a Docker switch called traefiktest_net1
  • Pulls the emilevauge/whoami image (if not already local)
  • Configures and starts 1 instance of whoami on port 80 – forward to port 90 with Traefik
  • Last, it sets the sessions to be sticky(this can be removed if its not needed)

Now to test from commend line, you can just run the below.

You can also test this by going with a web browser to http://traefik.domain.com:90

To bring up anther instance just run the below.
Tip: Change the 2 to whatever number you like to scale to.

Now, lets check docker-compose status, just run the below.

Of course you can also check the great traefik Web-UI.
I included a screen shot below.


To generate traffic (for a test), just ran the below.
Note: i know you can use Apache ab for the test, but this was a quick form of a test, Note: keep in mind it will try to run full speed (the fasts it can on a single core – being its not parallelized test).

Below is a screen shot of the health monitor in Traefik

Note: To see the load balancing between containers, use the CURL option and not the web access, since the sessions are set to sticky it will always go to the same host.

To clean-up the environment – remove the environment, just run the below.

To manual scale the environment

Above, I used docker-compose scale option to scale the web application, below I will show you the manual option to scale the environment.

To scale and add anther Docker container, just run the below.
Note: You might need to update the name with an unused name in the below configuration.

Using in a Docker Swarm environment

If you are trying to configure traefik in a Docker Swarm environment, you will need to add the options below to your docker-compose.yml file.

Traefik REST API capability

Traefik can also be manipulated with an API.
Below is a simple example of returning the health status.

Below is another API example, returning all running services.

For a full list of API capability an options, check out the Traefik API pages

Last, I would like to mention. out of the many other great features in Traefik is they fully integrate with ACME SSL certificates, for full details visit the Traefik web site.

What do you use as a proxy for micro services? let me know in the comments below.

You might also like:
Other articles related to Docker Kubernetes / micro-services.

Leave a Reply

avatar
  Subscribe  
Notify of