Load balancing with Traefik
The main use case for Traefik in this scenario is to distribute incoming HTTP(S) and TCP requests from the Internet to front-end services that can handle these requests. This tutorial shows you one such example using a demo web application.
Traefik can natively integrate with Consul using the Consul Catalog Provider and can use tags to route traffic.
Reference material
Prerequisites
To perform the tasks described in this tutorial, you need to have a Nomad environment with Consul installed. You can use this Terraform environment to provision a sandbox environment. This tutorial uses a cluster with one server node and three client nodes.
Note
This tutorial is for demo purposes and only assumes a single server node. Please consult the reference architecture for production configuration.
Create and run a demo web app job
Create a job for a demo web application and name the file webapp.nomad.hcl
:
Note that this job deploys 3 instances of the demo web application which you load balance with Traefik in the next few steps.
The job uses tags to configure routing to the web app. Even though the application listens on
/
, it is possible to define/myapp
as the route because of thePath
option.
You can now deploy the demo web application:
Create and run a Traefik job
Create a job named traefik.nomad.hcl
. This job starts an instance of Traefik and
configures it to discover its configuration from Consul. This Traefik instance
provides routing and load balancing to the sample web application.
This configuration uses a static port for the load balancer to
8080
. This allow you to querytraefik.service.consul:8080
at the appropriate paths (as configured in the tags section ofwebapp.nomad.hcl
from anywhere inside your cluster so you can reach the web application.The Traefik dashboard is configured at port
8081
.Please note that although the job contains an inline template, you could alternatively use the template stanza in conjunction with the artifact stanza to download an input template from a remote source such as an S3 bucket.
Now, run the Traefik job:
Check the Traefik dashboard
You can visit the dashboard for Traefik at
http://<Your-Traefik-IP-address>:8081
. You can use this page to verify your
settings and for basic monitoring.
Make a request to the load balancer
If you query the Traefik load balancer, you should be able to see a response similar to the one shown below (this command should be run from a node inside your cluster):
Note that your request has been forwarded to one of the several deployed instances of the demo web application (which is spread across 3 Nomad clients). The output shows the IP address of the host it is deployed on. If you repeat your requests, the IP address changes based on which backend web server instance received the request.
Note
If you would like to access Traefik from outside your cluster, you
can set up a load balancer in your environment that maps to an active port
8080
on your clients (or whichever port you have configured for Traefik to
listen on). You can then send your requests directly to your external load
balancer.