How to set up the Traefik Load Balancer with Docker in Ubuntu 20.04

s2 0

Traefik is a modern reverse proxy and load balancing server that supports Layer 4 (TCP) and Layer 7 (HTTP) load balancing. Its configuration can be defined in JSON, YML or TOML format. It consists of an entry point (frontend), service (backend), router (rules), middleware (optional functions).

This article shows how to use the Traefik Load Balancer in Layer 7 (HTTP) mode.

requirements

  1. Installed Docker and docker-compose
  2. Internet connection for downloading Docker images.
  3. DNS A record that assigns your domain to receive the Let’s Encrypt certificate (e.g. traefik.yourdomain.com)

construction

I will be running two backend servers (nginx containers) and a traefik container in the same Docker network zone. I am using traefik.yourdomain.com for the explanation. You need your own domain.

Let’s start by creating a directory in your home town.

$ mkdir traefik && cd traefik

Now create a Docker network using the command below. This helps to reach the container by name.

$ docker network create web_zone

Traefik.yaml configuration

First, create a file called traefik.yaml:

$ vim traefik.yaml

and paste the following content.

# Static configuration
entryPoints:
    unsecure:
        address: :80
    secure:
        address: :443

certificatesResolvers:
    myresolver:
        acme:
            email: [email protected]
            storage: acme.json
            httpChallenge:
                entryPoint: unsecure
            
providers:
      file:
      filename: tls.yaml
      watch: true

Explanation

  • Entry points are like front-end listing services and ports.
  • CertificatesResolvers is to use a Letsencrypt certificate on demand.
  • Providers are the files for defining routers / middleware and services

Configuration of the file provider

Now create another file in the same directory, which we defined in the Provider section:

$ vim tls.yaml

and paste the following yaml configuration.

http:
    routers:
        http_router:
            rule: "Host(`traefik.yourdomain.com`)"
            service: allbackend
        https_router:
            rule: "Host(`traefik.yourdomain.com`)"
            service: allbackend
            tls:
                certResolver: myresolver
                options: tlsoptions
    services:
        allbackend:
            loadBalancer:
                servers:
                    - url: "https://myserver1/"
                    - url: "https://myserver2/"
           
tls:
    options:
        tlsoptions:
            minVersion: VersionTLS12

Explanation

  • The router section is intended to define the route. We have two routes for http and https
  • Backends are defined in services, you can also specify load balancing algorithms.
  • tls to define the TLS configuration and options.

As defined in the file, create the following file to save the Let’s Encrypt certificate.

$ touch acme.json
$ chmod 600 acme.json

Docker-Compose for traefik

I’m going to create a container with Docker Compose and Map 80, 443 Port. You define your domain name. Create a docker-compse.yml file:

$ vim docker-compose.yml

and paste the following configuration:

version: '3'

services:

  traefik:
    image: traefik:latest
    command: --docker --docker.domain=yourdomain.com
    ports:
      - 80:80
      - 443:443
    networks:
      - web_zone
    volumes:
      - /run/docker.sock:/run/docker.sock
      - ./traefik.yaml:/traefik.yaml
      - ./tls.yaml:/tls.yaml
      - ./acme.json:/acme.json
    container_name: traefik
    restart: always
networks:
  web_zone:
      external: true

Backend server

Now let’s run two backend servers with nginx image. First create a directoryadvertising

$ mkdir ~/traefik/backend && cd ~/traefik/backend/

Create two index files as described below.

echo "<h1> Hello server 1</h1>" > index-server1.html
echo "<h1> Hello server 2</h1>" > index-server2.html

Docker Compose file to run two Nginx backend servers

The following is the simple compose file that creates two nginx containers. Create the docker-compse.yml file:

$ vim docker-compose.yml

and paste the following configuration:

version: '3'
services:
  myserver1:
    image: nginx
    container_name: nginx1
    restart: always
    volumes:
      - ./index-server1.html:/usr/share/nginx/html/index.html
    networks:
      - web_zone
  myserver2:
    image: nginx
    container_name: nginx2
    restart: always
    volumes:
      - ./index-server2.html:/usr/share/nginx/html/index.html
    networks:
      - web_zone
networks:
  web_zone:
        external: true

Start the Docker container

Now run the container. First, start the nginx backend container with the following command.

$:~/traefik/backend$ docker compose up -d

Two containers must be running. Confirm this by running the command below.

[email protected]:~/traefik/backend$ docker ps

Now go back to the directory and run the following command to run the traefik load balancer.

$:~/traefik$ docker compose up -d

Make sure that the Traefik container is ready for use.

$:~/traefik$ docker ps

Search the website

Open a browser and enter your domain name https://traefik.yourdomain.com. You will get the answer below.

Trafik load balancer works

When you refresh the page you will also be redirected to the second backend. This is the standard routing algorithm in traefik.

Second server

You can also check whether the certificate was issued by letsencrypt while the container is active. Just browse https://traefik.yourdomain.com

SSL encryption

diploma

You will learn how to use traefik as a load balancer for your Docker container. You can explore more by visiting the official website https://doc.traefik.io/traefik/ . Thanks very much.

How to set up the Traefik Load Balancer with Docker in Ubuntu 20.04