DEV Community

Ankit Kumar
Ankit Kumar

Posted on

Deploying Scalable Applications with Docker Swarm and Docker Compose

Part 1: Building the cluster (The Foundation)

1. Go to the Play-With-Docker and Create Instances
You will create 5 instances. These will be your 5 servers.

2. Initialize the Swarm (Nominate the First Leader)

docker swarm init --advertise-addr [ManagerNodeIP]
Enter fullscreen mode Exit fullscreen mode

You pick one of your 5 instances to be the first Manager Node. This command "starts" the swarm and declares this node as the leader. The --advertise-addr tells other nodes which IP address they should use to find and connect to this manager.

3. Add Worker Nodes (Recruit the Workers)

docker swarm join --token SWMTKN-1-17xx0ratormliuuff545oiotqjjgdrlfvbmji5xny007bhdqdx-98ny7h91d36bcy4dgfd8zujm9 192.168.0.29:2377
Enter fullscreen mode Exit fullscreen mode

You copy the join command from the manager and paste it into 3 of your other instances. The long token is like a secret password that proves they are allowed to join your swarm. These nodes become Worker Nodes. Their only job is to run containers as instructed by the manager.

4. Add a Second Manager (Get a Backup Leader)

Commands:

1.On the first manager:

docker swarm join-token manager
Enter fullscreen mode Exit fullscreen mode

2.On the last remaining instance: Paste command which appears after join-token manager command into the remained instance:

docker swarm join --token SWMTKN-1-17xx0ratormliuuff545oiotqjjgdrlfvbmji5xny007bhdqdx-1hahfhs9mrrgsb0nsyzk8vpyp 192.168.0.29:2377
Enter fullscreen mode Exit fullscreen mode

5. Verify the Cluster

docker node ls
Enter fullscreen mode Exit fullscreen mode

Part 2: Defining and Deploying Your Application

This is the docker-compose.yml file

version: "3.8"

services:
  mydatabase:
    image: mysql:5.7
    restart: always
    volumes: 
      - mydata:/var/lib/mysql
    environment: 
      MYSQL_ROOT_PASSWORD: somewordpress
      MYSQL_DATABASE: wordpress
      MYSQL_USER: wordpress
      MYSQL_PASSWORD: wordpress
    networks:
      - mynet
  mywordpress:
    image: wordpress:latest
    deploy:
      replicas: 3
      update_config:
        parallelism: 2
        delay: 5s
        order: stop-first
    depends_on: 
      - mydatabase
    restart: always
    ports:
      - "80:80"
      - "443:443"
    environment: 
      WORDPRESS_DB_HOST: mydatabase:3306
      WORDPRESS_DB_USER: wordpress
      WORDPRESS_DB_PASSWORD: wordpress
      WORDPRESS_DB_NAME: wordpress
    networks:
      - mynet

volumes:
  mydata:

networks:
  mynet:
    driver: overlay
Enter fullscreen mode Exit fullscreen mode

7. Deploy the Stack

docker stack deploy --compose-file docker-compose.yml firststack
Enter fullscreen mode Exit fullscreen mode
  • docker stack: The command for managing multi-service applications in Swarm.

  • deploy: The action to create or update the application.

  • firststack: The name you are giving your application stack.

The manager will now read your file and start creating containers on the worker nodes according to your specifications (1 MySQL container and 3 WordPress containers).


Part 3: Verifying and Testing the Application

** 8. Inspect the Stack

docker stack ls
docker stack services firststack
docker stack ps firststack
Enter fullscreen mode Exit fullscreen mode
  • docker stack ls: Lists all the stacks running on your swarm.

  • docker stack services firststack: Shows the services in your stack (firststack_mydatabase and firststack_mywordpress) and how many replicas are running.

  • docker stack ps firststack: This is the most detailed view. It shows every individual container (called a "task" in Swarm), which node it's running on, and its current state.

9. Test the Self-Healing Feature

  • Action: You'll go to one of the worker nodes and manually stop a WordPress container using docker container stop [containerID].

  • What's Happening Here? You are simulating a crash.

  • The Result: The Swarm manager is constantly monitoring the cluster. It will see that one of the mywordpress replicas is gone (the desired state is 3, but the current state is 2). It will immediately command a worker node to start a new WordPress container to bring the count back up to 3.

  • Verification: If you run docker stack ps firststack on the manager again, you will see the stopped container and a new one that was recently started to replace it. This proves the "self-healing" power of Swarm.

docker stack ps firststack
Enter fullscreen mode Exit fullscreen mode

Top comments (0)