9  Week 7/8 (20/27-Nov-2025)


9.1 Create a Nginx Cluster

  • Using the contents explained in section Seção 6.1.2 create two different containers of nginx, called nginx1and nginx2, each, using a different external port.

  • Make sure each container map a volume to different directories and containing a different index.html file. Change the contents of this file to identify the nginx node. This is needed to clearly distinguish the contents of each nginx node.

  • Verification: Open http://localhost:port in a web browser to see the content of the mounted directory on each cluster node, where the port parameter is the value of each node.

  • Since nginx1 and nginx2 are supposed to have the same application contents, how can we distribute the traffic between the two nodes ?

    • The answer is: Using a Reverse Proxy to perform load balancing. A Reverse Proxy like Traefik.

9.2 Traefik Installaltion

  • Please refer to section Capítulo 4 to install Traefik.

9.3 Load Balancing (using Traefik)

  • Implement a load balancing mechanism using Traefik to distribute the traffic between each nginx cluster nodes.

  • On your hard-drive, create a directory called dynamic inside /traefik. On Windows use C:/traefik. Use any other location if you wish.

  • Change the traefik.yml file to include the following contents:

## traefik.yml

global:
  checkNewVersion: true
  sendAnonymousUsage: false

log:
  level: DEBUG

accessLog: {}

#tracing:
#  elastic: {}

metrics:
  prometheus: {}

ping: {}

# API and dashboard configuration
api:
  dashboard: true
  insecure: true

# Docker entrypoints backend
entryPoints:
  web:
    address: :80

  web-secure:
    address: :443
    
# Docker configuration backend
providers:
  docker:
    defaultRule: 'Host(`{{ if index .Labels "com.docker.compose.service" }}{{ index .Labels "com.docker.compose.service" }}.aritlab.com{{ else }}{{ trimPrefix `/` .Name }}.aritlab.com{{ end }}`)'
    endpoint: unix:///var/run/docker.sock
    # For Windows
    # endpoint: "npipe:////./pipe/docker_engine"
    watch: true
    exposedByDefault: true
    
  file:
    directory: "/etc/traefik/dynamic"
    watch: true
  • Inside directory /traefik/dynamic create the file dynamic.yml with the following contents:

  • From Docker, get the ip addresses from each nginxcluster node.

## DYNAMIC CONFIGURATION
http:
  routers:
    labcluster:
      rule: "Host(`www.aritlab.com`)"
      service: labcluster
      
  services:
    labcluster:
      loadBalancer:
        servers:
            - url: "http://ip_address_nginx1:port1/"
            - url: "http://ip_address_nginx2:port2/"
  • Use again the following Docker command to install and run Traefik:
docker run -d --restart always -p 80:80 -p 8080:8080 -v /var/run/docker.sock:/var/run/docker.sock -v /traefik:/etc/traefik --network traefik_network --name traefik traefik:latest
  • In your hosts file, map the name www.aritlab.com to your own host hostname/ip address.

Open a web browser and navigate to http://www.aritlab.com. You should see now that Traefik is redirecting you either to nginx1 or nginx2.