|
|
|
|
# TP Déploiement d'applications sur Swarm
|
|
|
|
|
|
|
|
|
|
Déployer l'application en tant que _services_ sur le cluster.
|
|
|
|
|
|
|
|
|
|
Déployer l'application en tant que _stack_ sur le cluster.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## 1 - Services Swarm
|
|
|
|
|
|
|
|
|
|
Déployer l'application sous forme de service Swarm.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
### Gestion des images
|
|
|
|
|
|
|
|
|
|
* Pour déployer l'application sur tout le cluster il faut que l' image `web:1` soit accessible depuis tous les noeuds du cluster. Une solution serait d'héberger l'image sur un registry.
|
|
|
|
|
|
|
|
|
|
* Ici nous allons simplement utiliser `docker save/load`.
|
|
|
|
|
|
|
|
|
|
* Sur la première VM, archiver l'image `web:1`.
|
|
|
|
|
|
|
|
|
|
* Transférer l'archive sur la seconde VM.
|
|
|
|
|
|
|
|
|
|
* Importer l'image dans docker sur la deuxième VM.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<!-- .slide: data-background="#2E2E2E" -->
|
|
|
|
|
|
|
|
|
|
Sur la machine `docker-1` :
|
|
|
|
|
|
|
|
|
|
```none
|
|
|
|
|
$ docker save -o web.tar web:1
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
```none
|
|
|
|
|
$ scp web.tar docker@<ip-docker-2>:/home/docker/.
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<!-- .slide: data-background="#2E2E2E" -->
|
|
|
|
|
|
|
|
|
|
Sur la machine `docker-2` :
|
|
|
|
|
|
|
|
|
|
```none
|
|
|
|
|
$ docker load -i web.tar
|
|
|
|
|
```
|
|
|
|
|
```none
|
|
|
|
|
$ docker image ls
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
* Créer un réseau distribué nommé `swarm-formation` de type `overlay`.
|
|
|
|
|
|
|
|
|
|
* Vérifier la création du réseau.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<!-- .slide: data-background="#2E2E2E" data-state="medium-code" -->
|
|
|
|
|
Créer le réseau `swarm-formation` :
|
|
|
|
|
```none
|
|
|
|
|
$ docker network create --driver overlay swarm-formation
|
|
|
|
|
xdggd0hncm2srhti5cq9pwoc0
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
```none
|
|
|
|
|
$ docker network ls
|
|
|
|
|
NETWORK ID NAME DRIVER SCOPE
|
|
|
|
|
...
|
|
|
|
|
xdggd0hncm2s swarm-formation overlay swarm
|
|
|
|
|
...
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
* Déployer un volume de données distant de type NFS
|
|
|
|
|
|
|
|
|
|
- Installer un serveur NFS sur la première VM.
|
|
|
|
|
|
|
|
|
|
- Installer un client NFS sur la seconde VM.
|
|
|
|
|
|
|
|
|
|
- Créer un Volume Docker nommé `mariadb-nfs` qui utilise le partage NFS `/var/nfs-export`.
|
|
|
|
|
|
|
|
|
|
- Inspecter le Volume créé.
|
|
|
|
|
|
|
|
|
|
<!--* Pour assurer la persistance des données MariaDB sur la machine `docker-1`, ajouter une contrainte en positionnant le label `type=db` sur `docker-1`.-->
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<!-- .slide: data-background="#2E2E2E" data-state="small-code" -->
|
|
|
|
|
Installer un serveur NFS sur la première machine
|
|
|
|
|
|
|
|
|
|
```none
|
|
|
|
|
$ sudo ~/docker-tp-files/tp-nfs/setup-nfs-server
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
```none
|
|
|
|
|
# apt update
|
|
|
|
|
# apt install nfs-kernel-server
|
|
|
|
|
# systemctl enable nfs-kernel-server
|
|
|
|
|
# mkdir /var/nfs-export
|
|
|
|
|
# echo "/var/nfs-export *(rw,sync,no_subtree_check,no_root_squash)" > /etc/exports
|
|
|
|
|
# exportfs -a
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
Installer un client NFS sur la seconde machine
|
|
|
|
|
|
|
|
|
|
```none
|
|
|
|
|
# apt update
|
|
|
|
|
# apt install nfs-common
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
Cloner le dépôt git : https://gitlab.com/ctritten/docker-tp-files.git
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<!-- .slide: data-background="#2E2E2E" -->
|
|
|
|
|
Créer un Volume Docker
|
|
|
|
|
```none
|
|
|
|
|
$ docker volume create \
|
|
|
|
|
--driver local \
|
|
|
|
|
--opt type=nfs \
|
|
|
|
|
--opt o=addr=192.168.56.102,nolock,soft,rw \
|
|
|
|
|
--opt device=:/var/nfs-export \
|
|
|
|
|
mariadb-nfs
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
_Attention_ : l'adresse IP doit être celle de la machine master !
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
* Lancer MariaDB en tant que service nommé `db`.
|
|
|
|
|
|
|
|
|
|
- Utiliser le volume `mariadb-nfs`.
|
|
|
|
|
|
|
|
|
|
- Utiliser le réseau `swarm-formation`.
|
|
|
|
|
|
|
|
|
|
- Passer les variables de connexion à la base de données en tant que variables d'environnement à l'exécution.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<!-- .slide: data-background="#2E2E2E" -->
|
|
|
|
|
Lancer le service MariaDB :
|
|
|
|
|
```none
|
|
|
|
|
$ docker service create \
|
|
|
|
|
--name db \
|
|
|
|
|
--env MARIADB_ROOT_PASSWORD=docker \
|
|
|
|
|
--env MARIADB_DATABASE=docker \
|
|
|
|
|
--env MARIADB_USER=docker \
|
|
|
|
|
--env MARIADB_PASSWORD=docker \
|
|
|
|
|
--mount source=mariadb-nfs,target=/var/lib/mysql \
|
|
|
|
|
--network swarm-formation \
|
|
|
|
|
mariadb:10.7.1
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<!-- .slide: data-background="#2E2E2E" data-state="small-code" -->
|
|
|
|
|
Vérifier que tout est OK :
|
|
|
|
|
```none
|
|
|
|
|
$ docker service ls
|
|
|
|
|
ID NAME MODE REPLICAS IMAGE PORTS
|
|
|
|
|
v4v78r3e0hfm db replicated 1/1 mariadb:10.7.1
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
```none
|
|
|
|
|
$ docker service ps db
|
|
|
|
|
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE
|
|
|
|
|
wecjdwwu8lql db.1 mariadb:10.7.1 docker-1 Running Running 3 minutes ago
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
```none
|
|
|
|
|
$ docker volume ls
|
|
|
|
|
DRIVER VOLUME NAME
|
|
|
|
|
local mariadb-nfs
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Déployer Apache en tant que service Swarm
|
|
|
|
|
|
|
|
|
|
* Nommer le service `web`.
|
|
|
|
|
|
|
|
|
|
* Utiliser l'image `web:1`.
|
|
|
|
|
|
|
|
|
|
* Utiliser le réseau nommé `swarm-formation`.
|
|
|
|
|
|
|
|
|
|
* Publier le service sur le port `80`.
|
|
|
|
|
|
|
|
|
|
* Passer les variables de connexion à la base de données en tant que variables d'environnement à l'exécution.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<!-- .slide: data-background="#2E2E2E" -->
|
|
|
|
|
Lancer le service Apache :
|
|
|
|
|
```none
|
|
|
|
|
$ docker service create \
|
|
|
|
|
--name web \
|
|
|
|
|
--env DB_HOST=db \
|
|
|
|
|
--env DB_NAME=docker \
|
|
|
|
|
--env DB_USER=docker \
|
|
|
|
|
--env DB_PASSWORD=docker \
|
|
|
|
|
--network swarm-formation \
|
|
|
|
|
--publish 80:80 \
|
|
|
|
|
web:1
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
* Vérifier que les services sont correctement déployés.
|
|
|
|
|
|
|
|
|
|
* Observer le comportement de l'application depuis un navigateur.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<!-- .slide: data-background="#2E2E2E" data-state="small-code" -->
|
|
|
|
|
```none
|
|
|
|
|
$ docker service ls
|
|
|
|
|
ID NAME MODE REPLICAS IMAGE PORTS
|
|
|
|
|
v4v78r3e0hfm db replicated 1/1 mariadb:10.7.1
|
|
|
|
|
lilu659iiha7 web replicated 1/1 web:1 *:8080->80/tcp
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
```none
|
|
|
|
|
$ firefox http://<ip-docker-1>/cgi-bin/index.sh
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Voir les conteneurs déployés à l'aide de l'outil Docker Swarm Visualizer
|
|
|
|
|
|
|
|
|
|
* Récupérer Docker Swarm Visualizer sur le Docker Hub
|
|
|
|
|
(image `dockersamples/visualizer`)
|
|
|
|
|
|
|
|
|
|
* Lancer Docker Swarm Visualizer
|
|
|
|
|
|
|
|
|
|
* Accéder au Docker Swarm Visualizer
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<!-- .slide: data-background="#2E2E2E" data-state="small-code" -->
|
|
|
|
|
Récupérer et lancer Docker Swarm Visualizer
|
|
|
|
|
```none
|
|
|
|
|
$ docker run -d \
|
|
|
|
|
-p 4000:8080 \
|
|
|
|
|
-v /var/run/docker.sock:/var/run/docker.sock \
|
|
|
|
|
dockersamples/visualizer
|
|
|
|
|
0905de50e56eb1efb67fdc803201fcc3cac600f2f7e9a1d46375b50419abd629
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
Accéder au Docker Swarm Visualizer
|
|
|
|
|
```none
|
|
|
|
|
$ firefox http://ip-docker-1:4000
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Mettre à l'échelle le service `web`
|
|
|
|
|
|
|
|
|
|
* Ajouter 2 instances `web` supplémentaires.
|
|
|
|
|
|
|
|
|
|
* Constater les effets dans le Docker Swarm Visualizer.
|
|
|
|
|
|
|
|
|
|
* Ajouter / Retirer d'autres instances.
|
|
|
|
|
|
|
|
|
|
* Constater les effets dans le Docker Swarm Visualizer.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<!-- .slide: data-background="#2E2E2E" data-state="small-code" -->
|
|
|
|
|
Ajouter 2 instances `web` supplémentaires
|
|
|
|
|
```none
|
|
|
|
|
$ docker service scale web=3
|
|
|
|
|
web scaled to 3
|
|
|
|
|
overall progress: 3 out of 3 tasks
|
|
|
|
|
1/3: running [==================================================>]
|
|
|
|
|
2/3: running [==================================================>]
|
|
|
|
|
3/3: running [==================================================>]
|
|
|
|
|
verify: Service converged
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
* Supprimer les services, le réseau overlay et le volume de données.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<!-- .slide: data-background="#2E2E2E" data-state="medium-code" -->
|
|
|
|
|
Supprimer les services
|
|
|
|
|
```none
|
|
|
|
|
$ docker service rm web
|
|
|
|
|
web
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
```none
|
|
|
|
|
$ docker service rm db
|
|
|
|
|
db
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
Supprimer le réseau overlay
|
|
|
|
|
```none
|
|
|
|
|
$ docker network rm swarm-formation
|
|
|
|
|
swarm-formation
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## 2 - Stack Swarm
|
|
|
|
|
|
|
|
|
|
Déployer l'application en tant que stack Swarm.
|
|
|
|
|
|
|
|
|
|
* Adapter le `docker-compose.yml` pour une utilisation avec Swarm.
|
|
|
|
|
|
|
|
|
|
* Ajouter les options pour utiliser le volume NFS.
|
|
|
|
|
|
|
|
|
|
* Passer les variables de connexion à la base de données en tant que variables d'environnement dans le fichier docker-compose.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<!-- .slide: data-background="#2E2E2E" -->
|
|
|
|
|
```yaml
|
|
|
|
|
version: "3"
|
|
|
|
|
services:
|
|
|
|
|
web:
|
|
|
|
|
image: web:1
|
|
|
|
|
depends_on:
|
|
|
|
|
- db
|
|
|
|
|
ports:
|
|
|
|
|
- 8080:80
|
|
|
|
|
environment:
|
|
|
|
|
- DB_HOST=db
|
|
|
|
|
- DB_NAME=docker
|
|
|
|
|
- DB_USER=docker
|
|
|
|
|
- DB_PASSWORD=docker
|
|
|
|
|
db:
|
|
|
|
|
image: mariadb:10.7.1
|
|
|
|
|
environment:
|
|
|
|
|
- MARIADB_ROOT_PASSWORD=docker
|
|
|
|
|
- MARIADB_DATABASE=docker
|
|
|
|
|
- MARIADB_USER=docker
|
|
|
|
|
- MARIADB_PASSWORD=docker
|
|
|
|
|
volumes:
|
|
|
|
|
- mariadb-data:/var/lib/mysql
|
|
|
|
|
volumes:
|
|
|
|
|
mariadb-data:
|
|
|
|
|
driver_opts:
|
|
|
|
|
type: "nfs"
|
|
|
|
|
o: "addr=192.168.56.102,nolock,soft,rw"
|
|
|
|
|
device: ":/var/nfs-export"
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
* Lancer la `stack` dans le cluster.
|
|
|
|
|
|
|
|
|
|
* Vérifier que tout est OK.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<!-- .slide: data-background="#2E2E2E" data-state="small-code" -->
|
|
|
|
|
Déployer la stack :
|
|
|
|
|
```none
|
|
|
|
|
$ docker stack deploy --compose-file docker-compose.yml formation
|
|
|
|
|
Creating network formation_default
|
|
|
|
|
Creating service formation_db
|
|
|
|
|
Creating service formation_web
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
Vérifier la création de la stack :
|
|
|
|
|
```none
|
|
|
|
|
$ docker stack ls
|
|
|
|
|
NAME SERVICES
|
|
|
|
|
formation 2
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
Vérifier la création des services :
|
|
|
|
|
```none
|
|
|
|
|
$ docker service ls
|
|
|
|
|
ID NAME MODE REPLICAS IMAGE PORTS
|
|
|
|
|
x5ywxi4xsytq formation_db replicated 1/1 mariadb:10.7.1
|
|
|
|
|
jxhrhfxwpe3f formation_web replicated 1/1 web:1 *:8080->80/tcp
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<!-- .slide: data-background="#2E2E2E" data-state="small-code" -->
|
|
|
|
|
Vérifier les tâches de la stack :
|
|
|
|
|
```none
|
|
|
|
|
$ docker stack ps formation
|
|
|
|
|
ID NAME IMAGE NODE DESIRED S. CURRENT S.
|
|
|
|
|
tltlowva27vy formation_db.1 mariadb:10.7.1 docker-1 Running Running 2 m...
|
|
|
|
|
wxi9etw1opii formation_web.1 web:1 docker-2 Running Running 2 m...
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
Vérifier la création du réseau :
|
|
|
|
|
```none
|
|
|
|
|
$ docker network ls
|
|
|
|
|
NETWORK ID NAME DRIVER SCOPE
|
|
|
|
|
96ldjt0j2mb0 formation_default overlay swarm
|
|
|
|
|
...
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
Vérifier la création du volume :
|
|
|
|
|
```none
|
|
|
|
|
$ docker volume ls
|
|
|
|
|
DRIVER VOLUME NAME
|
|
|
|
|
local formation_mariadb-data
|
|
|
|
|
...
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
* Mettre à l'échelle l'application. Partie web uniquement.
|
|
|
|
|
|
|
|
|
|
* Constater les effets dans le Docker Swarm Visualizer.
|
|
|
|
|
|
|
|
|
|
* Observer le système d'équilibrage de charge natif de Swarm en se rendant sur la page web de l'application.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<!-- .slide: data-background="#2E2E2E" data-state="small-code" -->
|
|
|
|
|
Augmenter le nombre de réplicas du service web :
|
|
|
|
|
```none
|
|
|
|
|
$ docker service scale formation_web=3
|
|
|
|
|
formation_web scaled to 3
|
|
|
|
|
overall progress: 3 out of 3 tasks
|
|
|
|
|
1/3: running [==================================================>]
|
|
|
|
|
2/3: running [==================================================>]
|
|
|
|
|
3/3: running [==================================================>]
|
|
|
|
|
verify: Service converged
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<!-- .slide: data-background="#2E2E2E" data-state="small-code" -->
|
|
|
|
|
Vérifier la création des services :
|
|
|
|
|
```none
|
|
|
|
|
$ docker service ls
|
|
|
|
|
ID NAME MODE REPLICAS IMAGE PORTS
|
|
|
|
|
x5ywxi4xsytq formation_db replicated 1/1 mariadb:10.7.1
|
|
|
|
|
jxhrhfxwpe3f formation_web replicated 1/1 web:1 *:8080->80/tcp
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
Vérifier les tâches de la stack :
|
|
|
|
|
```none
|
|
|
|
|
$ docker stack ps formation
|
|
|
|
|
ID NAME IMAGE NODE DESIRED S. CURRENT S.
|
|
|
|
|
5s5xh7xte1v8 formation_db.1 mariadb:10.7.1 docker-1 Running Running 4 m...
|
|
|
|
|
gkyrujiuisbf formation_web.1 web:1 docker-2 Running Running 4 m...
|
|
|
|
|
idbv36o0dzgt formation_web.2 web:1 docker-1 Running Running 2 m...
|
|
|
|
|
vh4iufo2s5fl formation_web.3 web:1 docker-2 Running Running 2 m...
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
* Détruire la stack.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Détruire la stack
|
|
|
|
|
<!-- .slide: data-background="#2E2E2E" data-state="medium-code" -->
|
|
|
|
|
```none
|
|
|
|
|
$ docker stack rm formation
|
|
|
|
|
Removing service formation_db
|
|
|
|
|
Removing service formation_web
|
|
|
|
|
Removing network formation_default
|
|
|
|
|
```
|