Les fichiers :
docker-compose.yml
ouvrir
version: '3.8'
services:
wit-blue:
image: xian310/who-is-there:21
ports:
# on indique uniquement le port en écoute dans le conteneur
# et pas un forwarding du genre "80:8080" car sinon le port 80
# est monopolisé par le premier conteneur créé pour le service wit-blue
- "8080"
environment:
- COLOR=lightblue
nginx:
image: nginx:1.21.3
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf:ro
ports:
- "8000:80"
networks:
default:
driver: bridge
name: wit-net
nginx.conf
ouvrir
events {
}
http {
server {
listen 80;
listen [::]:80;
access_log /var/log/nginx/reverse-access.log;
error_log /var/log/nginx/reverse-error.log;
location / {
proxy_pass http://wit-blue:8080;
}
}
}
Mise en place :
$ docker-compose up --scale wit-blue=3 -d
Creating network "wit-net" with driver "bridge"
Creating docker-compose-load-balancing_wit-blue_1 ... done
Creating docker-compose-load-balancing_wit-blue_2 ... done
Creating docker-compose-load-balancing_wit-blue_3 ... done
Creating docker-compose-load-balancing_nginx_1 ... done
Test :
$ for i in {1..10} ; do curl http://127.0.0.1:8000 ; done
wit v21 - Hello from aba3fa1cee89
wit v21 - Hello from 2c6dc415a207
wit v21 - Hello from f69bcc96af12
wit v21 - Hello from aba3fa1cee89
wit v21 - Hello from 2c6dc415a207
wit v21 - Hello from f69bcc96af12
wit v21 - Hello from aba3fa1cee89