Skip to content

TP8 cmd

PasRP-Theo edited this page Jun 5, 2025 · 1 revision

Guide Docker Swarm - Configuration 2 VPS

Configuration VPS

  • VPS Principal (Manager) : 54.36.181.95
  • VPS Secondaire (Worker) : 54.36.183.49
  • Username Docker Hub : pasrptheoo

1. Préparation des VPS

1.1. Configuration des hostnames

Sur VPS Principal (54.36.181.95) :

sudo hostnamectl set-hostname swarm-manager

Sur VPS Secondaire (54.36.183.49) :

sudo hostnamectl set-hostname swarm-worker

2. Publication de l'image Docker

2.1. Création de l'application (sur VPS Principal)

mkdir ~/docker-web-app
cd ~/docker-web-app

Créez index.html :

nano index.html

Contenu :

<!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 Dockerfile

Contenu :

FROM nginx:1.25.4

COPY ./index.html /usr/share/nginx/html/

EXPOSE 80

CMD ["nginx", "-g", "daemon off;"]

2.2. Build et publication

# 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

2.3. Test de l'image

# 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-web

3. Configuration du Docker Swarm

3.1. Initialisation du Swarm (VPS Principal)

Sur 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:2377

⚠️ IMPORTANT : Copiez exactement la commande docker swarm join qui s'affiche !

3.2. Ajout du second VPS (VPS Secondaire)

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:2377

3.3. Vérification du cluster

Sur 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     Active

4. Déploiement du service web

4.1. Création du service

Sur 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

4.2. Vérification du déploiement

# É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

4.3. Tests de fonctionnement

# 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 !

5. Tests de haute disponibilité

5.1. Test de résilience

Arrêter Docker sur le VPS Secondaire :

# Sur 54.36.183.49
sudo systemctl stop docker

Vérifier que le service reste accessible :

# Sur VPS Principal
docker service ps web-ha
curl http://54.36.181.95:8080  # Doit toujours fonctionner

Redémarrer Docker :

# Sur 54.36.183.49
sudo systemctl start docker

5.2. Test de mise à l'échelle

# 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=2

6. Configuration DNS Round Robin

6.1. Configuration dans votre zone DNS

Ajoutez 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

6.2. Test du DNS

# 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

7. Mise à jour du service

7.1. Créer une nouvelle version

# 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

7.2. Mise à jour rolling

# Mise à jour du service
docker service update --image pasrptheoo/swarm-web-ha:v2.0 web-ha

# Suivre le progress
docker service ps web-ha

8. Commandes de monitoring

8.1. Surveillance du cluster

# É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

8.2. Statistiques

# Utilisation des ressources
docker stats

# Espace disque
docker system df

# Conteneurs en cours sur chaque VPS
docker ps

9. Limitations avec 2 VPS

9.1. Quorum et résilience

  • 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"

9.2. Recommandations

  • Backup régulier de la configuration Swarm
  • Monitoring des deux VPS
  • Plan de récupération en cas de panne

10. Nettoyage (si nécessaire)

# 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 --force

Conclusion

Votre 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.

Clone this wiki locally