How to Setup and Configure Docker Swarm Cluster

docker swarm

Docker Swarm official image is available on Docker hub. In this tutorial, you will learn how to set up a three node docker swarm cluster.

Prerequisites

  • Four nodes with Docker 1.4 or later. ( Docker client, swarm master and two nodes)
  • All the nodes should be able to talk to each other using public or private IP addresses.

In this example, we will use a Docker client to remotely install and set up the swarm cluster. Docker client can be your laptop or a host with Docker installed. If you are using your laptop as Docker client, make sure that other nodes are reachable from your laptop.

Now let’s get started with the installation.

1. Install Docker on all the nodes. Stop and start the Docker daemon in the background on all the three nodes (master and two nodes) using the following command.

$ sudo docker -H tcp://0.0.0.0:2375 -d &

2. In this setup, docker swarm will be downloaded as a docker container in the master node. The below command tells the container to use the cluster id create command for the swarm setup. From Docker client machine, Install Docker swarm on the master node and create a swarm token using the following command.

Syntax:

$ sudo docker  -H tcp://<master_ip>:2375 run --rm swarm create

In the above syntax change master_ip with the IP of your swarm master node as shown in the command below.

$ sudo docker  -H tcp://10.0.1.61:2375 run --rm swarm create

3. Note down the swarm token generated by the above command as you need it for the entire cluster set up.

READ  Cloud Based Docker Container Monitoring Using Datadog

4. From the Docker client node, execute the following Docker command to join it to the cluster.

Syntax:

$ docker -H tcp://<node_1_ip>:2375 run -d swarm join --addr=<node1_ip>:2375 token://<cluster_token>

In the above syntax, replace node_1_ip with your node1’s IP and cluster_token with the token created in step 2 as shown below.

$ sudo docker -H tcp://10.0.1.72:2375 run -d swarm join --addr=10.0.1.72:2375 token://9aa97d79935c7b6e0216b1ca3072039

5.  Verify the swarm setup on node1 using the following command.

$ sudo docker -H tcp://10.0.1.72:2375 ps

6. Repeat the above steps for Node2 by replacing node1’s IP with node2’s IP.

7. Once you have joined all the nodes to the cluster, set up a swarm manager on the swarm master node using the following command.

Syntax:

$ sudo docker -H tcp://<master_ip>:2375 run -d -p 5000:5000 swarm manage token://<cluster_token>

Replace master_ip with the master nodes IP and cluster_token with the cluster token as shown below.

$ sudo docker -H tcp://10.0.1.61:2375 run -d -p 5001:2375 swarm manage token://9aa97d79935c7b6e0216b1ca3072039

8.  All the components needed for the cluster set up is in place now. You can start managing your swarm cluster using swarm manager residing in the master node. To list all the nodes in the cluster, execute the following Docker command from the docker client node.

$ sudo docker -H tcp://<master_ip>:2375 run --rm swarm list token://<cluster_token>

Replace master_ip and cluster_token with relevant values as shown below.

$ sudo docker -H tcp://10.0.1.61:2375 run --rm swarm list \ token://9aa97d79935c7b6e0216b1ca3072039

Execute the following command from the client to get the information about the cluster.

 sudo docker -H tcp://10.0.1.61:5001 info

9. Next you need  to test the cluster set up by deploying a container onto the cluster. Run a test busybox container fro the docker client  using the following command.

 sudo docker -H tcp://10.0.1.61:5001 run -dt --name swarm-test busybox /bin/sh

10. Once the container is deployed, you can list the running docker container using the following command.

sudo docker -H tcp://10.0.1.61:5001 ps

 

Free DevOps Resources

Get DevOps news, tutorials and resources in your inbox. A perfect way If you want to get started with devops. Like you, we dont like spam.

5 comments

  1. Pingback: Docker Tutorial : Getting Started With Docker Swarm

  2. Nguyễn Văn Tân Reply

    #sudo docker -H tcp://10.0.1.61:5001 info

    –> Please tell me how can we use port 5001, instead should we use 2375?
    Because I didn’t see port 5001 opened.
    Thanks

    • CoolDarthVader Reply

      It was opened when you ran …
      sudo docker -H tcp://10.0.1.61:2375 run -d -p 5001:2375 swarm manage token…

      Took me a while to figure out that by doing:
      docker -H tcp://10.0.1.61:2375 ps -a ==> you see containers spawned only on swarm master.

      but by doing:
      docker -H tcp://10.0.1.61:5001 ps -a ==> you see containers spawned on the swarm nodes (not master).
      .

Leave a Reply

Your email address will not be published. Required fields are marked *