Skip to content

Setup

Docker Swarm is a container orchestration tool that allows you to manage a cluster of Docker Engines. With Swarm, you can deploy, scale, and manage services across multiple Docker hosts seamlessly.

Prerequisites

Before you begin, ensure you have the following:

  • Docker Engine: Installed on all the nodes that will participate in the Swarm.
  • Network Configuration: Properly configured to allow communication between the nodes.
  • Access: Root or sudo privileges on the nodes.

Setting Up the Swarm Cluster

Step 1: Initialize the Swarm Manager

  1. Choose a Manager Node: Select a node to be the manager. This node will control the Swarm cluster.
  2. Initialize Swarm: Run the following command on the manager node:
    docker swarm init --advertise-addr <MANAGER-IP>
    
    Replace <MANAGER-IP> with the IP address of the manager node.

Step 2: Join Worker Nodes to the Swarm

  1. Get Join Token: On the manager node, run:
    docker swarm join-token worker
    
    This command provides a join token and command to run on worker nodes.
  2. Join Workers: Run the provided command on each worker node:
    docker swarm join --token <TOKEN> <MANAGER-IP>:2377
    
    Replace <TOKEN> with the token from the previous step and <MANAGER-IP> with the manager's IP address.

Step 3: Verify the Swarm

  • Check Nodes: On the manager node, verify the cluster status by running:
    docker node ls
    
    This command lists all nodes in the Swarm and their status.

Deploying Services

To deploy a service in the Swarm, use the following command:

docker service create --name <SERVICE-NAME> --replicas <NUMBER> <IMAGE>
Replace <SERVICE-NAME>, <NUMBER>, and <IMAGE> with the desired service name, number of replicas, and Docker image, respectively.

Managing the Swarm

Scaling Services

To scale a service, use:

docker service scale <SERVICE-NAME>=<NUMBER>
Replace <SERVICE-NAME> with the service name and <NUMBER> with the desired number of replicas.

Updating Services

To update a service, use:

docker service update --image <NEW-IMAGE> <SERVICE-NAME>
Replace <NEW-IMAGE> with the new image and <SERVICE-NAME> with the service name.

Removing Services

To remove a service, use:

docker service rm <SERVICE-NAME>
Replace <SERVICE-NAME> with the service name.