» 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 frontend services that can handle these requests. This guide will show 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

» Estimated Time to Complete

20 minutes

» Prerequisites

To perform the tasks described in this guide, you need to have a Nomad environment with Consul installed. You can use this repo to easily provision a sandbox environment. This guide will assume a cluster with one server node and three client nodes.

» Steps

» Step 1: Create a Job for Demo Web App

Create a job for a demo web application and name the file webapp.nomad:

job "demo-webapp" {
  datacenters = ["dc1"]

  group "demo" {
    count = 3

    task "server" {
      env {
        PORT    = "${NOMAD_PORT_http}"
        NODE_IP = "${NOMAD_IP_http}"
      }

      driver = "docker"

      config {
        image = "hashicorp/demo-webapp-lb-guide"
      }

      resources {
        network {
          mbits = 10
          port  "http" {}
        }
      }

      service {
        name = "demo-webapp"
        port = "http"
        tags = [
          "traefik.tags=service",
          "traefik.frontend.rule=PathPrefixStrip:/myapp",
        ]

        check {
          type     = "http"
          path     = "/"
          interval = "2s"
          timeout  = "2s"
        }
      }
    }
  }
}
  • Note that this job deploys 3 instances of our demo web application which we will load balance with Traefik in the next few steps.
  • We are using tags to configure routing to our web app. Even though our application listens on /, it is possible to define /myapp as the route because of the PathPrefixStrip option.

» Step 2: Deploy the Demo Web App

We can now deploy our demo web application:

$ nomad run webapp.nomad 
==> Monitoring evaluation "a2061ab7"
    Evaluation triggered by job "demo-webapp"
    Evaluation within deployment: "8ca6d358"
    Allocation "1d14babe" created: node "2d6eea6e", group "demo"
    Allocation "3abb950d" created: node "a62fa99d", group "demo"
    Allocation "c65e14bf" created: node "a209a662", group "demo"
    Evaluation status changed: "pending" -> "complete"
==> Evaluation "a2061ab7" finished with status "complete"

» Step 3: Create a Job for Traefik

Create a job for Traefik and name it traefik.nomad. This will be our load balancer that will balance requests to the deployed instances of our web application.

job "traefik" {
  region      = "global"
  datacenters = ["dc1"]
  type        = "service"

  group "traefik" {
    count = 1

    task "traefik" {
      driver = "docker"

      config {
        image        = "traefik:1.7"
        network_mode = "host"

        volumes = [
          "local/traefik.toml:/etc/traefik/traefik.toml",
        ]
      }

      template {
        data = <<EOF
[entryPoints]
    [entryPoints.http]
    address = ":8080"
    [entryPoints.traefik]
    address = ":8081"

[api]

    dashboard = true

# Enable Consul Catalog configuration backend.
[consulCatalog]

endpoint = "127.0.0.1:8500"

domain = "consul.localhost"

prefix = "traefik"

constraints = ["tag==service"]
EOF

        destination = "local/traefik.toml"
      }

      resources {
        cpu    = 100
        memory = 128

        network {
          mbits = 10

          port "http" {
            static = 8080
          }

          port "api" {
            static = 8081
          }
        }
      }

      service {
        name = "traefik"
        check {
          name     = "alive"
          type     = "tcp"
          port     = "http"
          interval = "10s"
          timeout  = "2s"
        }
      }
    }
  }
}
  • We have statically set the port of our load balancer to 8080. This will allow us to query traefik.service.consul:8080 at the appropriate paths (as configured in the tags section of webapp.nomad from anywhere inside our cluster so we can reach our web application.
  • The Traefik dashboard is configured at port 8081.
  • Please note that although we have defined the template inline, we 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.

» Step 4: Run the Traefik Job

We can now register our Traefik job:

$ nomad run traefik.nomad 
==> Monitoring evaluation "e22ce276"
    Evaluation triggered by job "traefik"
    Evaluation within deployment: "c6466497"
    Allocation "695c5632" created: node "a62fa99d", group "traefik"
    Evaluation status changed: "pending" -> "complete"
==> Evaluation "e22ce276" finished with status "complete"

» Step 5: 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.

Home Page

» Step 6: 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):

$ curl http://traefik.service.consul:8080/myapp
Welcome! You are on node 172.31.28.103:28893

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, you will see that the IP address changes.

  • 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.