Skip to content

reseau solution

Terminal window
vi haproxy.cfg
frontend http_frontend
bind *:80
default_backend http_backend
backend http_backend
mode http
balance roundrobin
server server1 web1:80 check
server server2 web2:80 check
Terminal window
docker network create web
docker network ls
NETWORK ID NAME DRIVER SCOPE
215762b5f166 bridge bridge local
d44bd01134bd host host local
cf2a789ecb04 none null local
1b7e609de0f2 web bridge local

Créer 2 images “nginx1“ et “nginx2“ avec des fichiers index.html spécifiques :

Terminal window
vi web1/Dockerfile
FROM nginx:1.19-alpine
COPY index.html /usr/share/nginx/html
Terminal window
vi web1/index.html
Server ONE
Terminal window
docker build -t nginx1 web1
Terminal window
vi web2/Dockerfile
FROM nginx:1.19-alpine
COPY index.html /usr/share/nginx/html
Terminal window
vi web2/index.html
Server TWO TWO
Terminal window
docker build -t nginx2 web2

Créer l’image myhaproxy avec de fichier de configuration fourni :

Terminal window
vi myhaproxy/Dockerfile
Terminal window
FROM haproxy:1.7
COPY haproxy.cfg /usr/local/etc/haproxy/haproxy.cfg
Terminal window
vi myhaproxy/haproxy.cfg
Terminal window
frontend http_frontend
bind *:80
default_backend http_backend
backend http_backend
mode http
balance roundrobin
server server1 web1:80 check
server server2 web2:80 check
Terminal window
docker build -t myhaproxy myhaproxy

Lancer les 3 conteneurs :

Terminal window
docker run -d --network=web --name=web1 nginx1
docker run -d --network=web --name=web2 nginx2
docker run -d --network=web --name=myhaproxy -p 80:80 myhaproxy

Vérifier le haproxy :

Terminal window
curl localhost
Server One
Terminal window
curl localhost
Server Two