-
Notifications
You must be signed in to change notification settings - Fork 0
TP8 cmd
- VPS Principal (Manager) : 54.36.181.95
- VPS Secondaire (Worker) : 54.36.183.49
- Username Docker Hub : pasrptheoo
Sur VPS Principal (54.36.181.95) :
sudo hostnamectl set-hostname swarm-managerSur VPS Secondaire (54.36.183.49) :
sudo hostnamectl set-hostname swarm-workermkdir ~/docker-web-app
cd ~/docker-web-appCréez index.html :
nano index.htmlContenu :
<!DOCTYPE html>
<html>
<head>
<title>HA Docker Swarm - 2 VPS</title>
<style>
body {
font-family: Arial, sans-serif;
text-align: center;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
margin: 0;
padding: 50px;
}
.container {
background: rgba(255,255,255,0.1);
padding: 40px;
border-radius: 15px;
max-width: 600px;
margin: 0 auto;
}
.server-info {
background: rgba(255,255,255,0.2);
padding: 20px;
border-radius: 10px;
margin: 20px 0;
}
</style>
</head>
<body>
<div class="container">
<h1>🐳 Docker Swarm HA - 2 VPS</h1>
<p>Cluster haute disponibilité avec 2 serveurs</p>
<div class="server-info">
<p><strong>Serveur actuel :</strong> <span id="hostname"></span></p>
<p><strong>Heure de chargement :</strong> <span id="time"></span></p>
<p><strong>VPS Principal :</strong> 54.36.181.95</p>
<p><strong>VPS Secondaire :</strong> 54.36.183.49</p>
</div>
<p>🚀 Service déployé avec Docker Swarm</p>
</div>
<script>
document.getElementById('hostname').textContent = window.location.hostname;
document.getElementById('time').textContent = new Date().toLocaleString();
</script>
</body>
</html>Créez le Dockerfile :
nano DockerfileContenu :
FROM nginx:1.25.4
COPY ./index.html /usr/share/nginx/html/
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]# Build de l'image
docker build -t pasrptheoo/swarm-web-ha:v1.0 .
# Connexion Docker Hub avec votre token
docker login -u pasrptheoo
# Mot de passe : dckr_pat__qNuhqj21A4HFR2FlHMiKTYa3DY
# Publication
docker push pasrptheoo/swarm-web-ha:v1.0# Test local
docker run -d -p 8080:80 --name test-web pasrptheoo/swarm-web-ha:v1.0
# Vérifier : http://54.36.181.95:8080
# Nettoyage
docker stop test-web && docker rm test-webSur 54.36.181.95 :
# Initialisation du swarm
docker swarm init --advertise-addr 54.36.181.95
# La commande affichera quelque chose comme :
# docker swarm join --token SWMTKN-1-xxxxx 54.36.181.95:2377docker swarm join qui s'affiche !
Sur 54.36.183.49 :
# Utilisez la commande fournie par l'étape précédente
# Exemple :
docker swarm join --token SWMTKN-1-xxxxxx 54.36.181.95:2377Sur VPS Principal (54.36.181.95) :
# Vérifier les nodes
docker node ls
# Vous devriez voir :
# ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS
# xxxxx swarm-manager Ready Active Leader
# yyyyy swarm-worker Ready ActiveSur VPS Principal :
# Création du service avec 2 réplicas
docker service create \
--name web-ha \
--publish published=8080,target=80 \
--replicas 2 \
pasrptheoo/swarm-web-ha:v1.0# État du service
docker service ls
# Détails des tâches
docker service ps web-ha
# Vous devriez voir 2 tâches réparties sur vos 2 VPS# Test depuis le VPS Principal
curl http://54.36.181.95:8080
# Test depuis le VPS Secondaire
curl http://54.36.183.49:8080
# Les deux devraient répondre !Arrêter Docker sur le VPS Secondaire :
# Sur 54.36.183.49
sudo systemctl stop dockerVérifier que le service reste accessible :
# Sur VPS Principal
docker service ps web-ha
curl http://54.36.181.95:8080 # Doit toujours fonctionnerRedémarrer Docker :
# Sur 54.36.183.49
sudo systemctl start docker# Augmenter le nombre de réplicas
docker service scale web-ha=3
# Vérifier la répartition
docker service ps web-ha
# Réduire à nouveau
docker service scale web-ha=2Ajoutez dans votre fichier de zone DNS :
; Entrées pour le round robin
web-ha.l2-2. 300 IN A 54.36.181.95
web-ha.l2-2. 300 IN A 54.36.183.49
# Test de résolution
nslookup web-ha.l2-2
dig web-ha.l2-2
# Test d'accès
curl http://web-ha.l2-2:8080# Modifier index.html (changer le titre par exemple)
nano index.html
# Changez : <title>HA Docker Swarm - 2 VPS v2.0</title>
# Build nouvelle version
docker build -t pasrptheoo/swarm-web-ha:v2.0 .
# Publication
docker push pasrptheoo/swarm-web-ha:v2.0# Mise à jour du service
docker service update --image pasrptheoo/swarm-web-ha:v2.0 web-ha
# Suivre le progress
docker service ps web-ha# État des nodes
docker node ls
# Services actifs
docker service ls
# Détails du service
docker service inspect --pretty web-ha
# Logs du service
docker service logs web-ha# Utilisation des ressources
docker stats
# Espace disque
docker system df
# Conteneurs en cours sur chaque VPS
docker ps- Avec 2 VPS : Si le manager tombe, le cluster s'arrête (pas de quorum)
- Solution : Promouvoir le worker en manager pour avoir 2 managers
# Promouvoir le worker en manager
docker node promote swarm-worker
# Vérifier
docker node ls
# Les deux nodes devraient maintenant être "Reachable"- Backup régulier de la configuration Swarm
- Monitoring des deux VPS
- Plan de récupération en cas de panne
# Supprimer le service
docker service rm web-ha
# Quitter le swarm (sur worker)
docker swarm leave
# Détruire le swarm (sur manager)
docker swarm leave --forceVotre configuration 2 VPS Docker Swarm offre :
✅ Haute disponibilité avec load balancing automatique
✅ Répartition des charges entre les 2 serveurs
✅ Mises à jour sans interruption (rolling updates)
✅ Accès depuis les 2 VPS grâce au load balancer intégré
✅ DNS Round Robin pour la redondance d'accès
Note : Avec 2 VPS, vous avez une résilience limitée. Pour une production critique, 3 VPS seraient recommandés.