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
nginx1andnginx2, each, using a different external port.Make sure each container map a volume to different directories and containing a different
index.htmlfile. Change the contents of this file to identify thenginxnode. This is needed to clearly distinguish the contents of eachnginxnode.Verification: Open
http://localhost:portin a web browser to see the content of the mounted directory on each cluster node, where theportparameter is the value of each node.Since
nginx1andnginx2are 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.
- The answer is: Using a Reverse Proxy to perform load balancing. A Reverse Proxy like
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
Traefikto distribute the traffic between eachnginxcluster nodes.On your hard-drive, create a directory called
dynamicinside/traefik. OnWindowsuseC:/traefik. Use any other location if you wish.Change the
traefik.ymlfile 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: trueInside directory
/traefik/dynamiccreate the filedynamic.ymlwith 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.comto 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.