How to Setup and Configure Docker Swarm Cluster

docker swarm

In this tutorial, you will learn how to set up a four node docker swarm cluster.

Prerequisites

  • Minimum two nodes with Docker installed ( swarm manager and cluster nodes)
  • All the nodes should be able to talk to each other using public or private IP addresses.

In this setup, I have total 4 nodes. 1 Swarm manager node and three other nodes to join the cluster with the following private IP addresses

Manager - 10.140.0.2
Node1   - 10.140.0.6
Node2   - 10.140.0.4
Mode3   - 10.140.0.5

Follow the steps given below for a multi node swarm cluster.

On Manager Node

1. Execute the following command with the manager nodes IP for initializing the swarm cluster.

docker swarm init --advertise-addr <MANAGER-IP>

For example,

docker swarm init --advertise-addr 10.140.0.2

You will get the following output once swarm is initialized.

[devopscube@manager ~]$ docker swarm init --advertise-addr 10.140.0.2
Swarm initialized: current node (0qjtv7kblhbrbub4yyk1tq1e7) is now a manager.

To add a worker to this swarm, run the following command:

    docker swarm join \
    --token SWMTKN-1-3l50pb6ko5ci23134wzt17gqkb4nsbv4e52ciwdwq80hmmx1si-d3kv6s6qtgf0crqmz9dxix2oc \
    10.140.0.2:2377

To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.

[devopscube@manager ~]$

As you can see, the output has the steps to join other nodes to this swarm manager node.

[irp posts=”647″ name=”Jenkins Tutorial For Beginners – Getting Started Guide”]

2. To know the swarm cluster info, execute the following command.

docker info

In the output, you will see the following.

Swarm: active
 NodeID: 0qjtv7kblhbrbub4yyk1tq1e7
 Is Manager: true
 ClusterID: zj0ioxrvl0tsyxasbhwyu947l
 Managers: 1
 Nodes: 1

3. To know the information about all the nodes in the cluster, you can run the following command.

docker node ls

On Node 1, 2 and 3

Now that we have the swarm manager ready, we can add our other nodes to the manager to form a multi node cluster.

1. Execute the swarm join command from the manager output on all the extra nodes as shown below. The swarm token and IP will change based on your environment.

Note: You can get the swarm token with the command by executing docker swarm join-token worker on the manager node.

sudo docker swarm join \    
--token SWMTKN-1-3l50pb6ko5ci23134wzt17gqkb4nsbv4e52ciwdwq80hmmx1si-d3kv6s6qtgf0crqmz9dxix2oc  \
10.140.0.2:2377

Swarm Configuration and Testing

1. Once you joined all the extra nodes, you can list the swarm node information by executing the following command on the manager node.

sudo docker node ls

You will get the output will the swarm cluster info as shown below.

[devopscube@manager ~]$ sudo docker node ls
ID                           HOSTNAME     STATUS  AVAILABILITY  MANAGER STATUS
0qjtv7kblhbrbub4yyk1tq1e7 *  manager  Ready   Active        Leader
adig05zbyuxt4x0oltrbrc523    node2        Ready   Active        
bzupprwwasy6vvwh4hn0s4txz    node1        Ready   Active        
ug20slq1qyk6uzwgfv3a5zd86    node3        Ready   Active

Now our cluster is ready. You can start deploying applications to your swarm cluster.


ONLINE COURSE:  Docker Mastery: The Complete Toolset From a Docker Captain

Check out the docker course which thousands of students have already enrolled.

  • Learn Docker and Compose
  • Best practices for making Dockerfiles
  • Learn to build multi node swarm cluster
  • Rolling application deployments without downtime

 

7 comments
  1. Great tutorial!
    However in order to add worker -nodes to the cluster I had to open following ports:

    On manager-node:
    firewall-cmd –add-port=2376/tcp –permanent
    firewall-cmd –add-port=2377/tcp –permanent
    firewall-cmd –add-port=7946/tcp –permanent
    firewall-cmd –add-port=7946/udp –permanent
    firewall-cmd –add-port=4789/udp –permanent

    On worker-nodes:
    firewall-cmd –add-port=2376/tcp –permanent
    firewall-cmd –add-port=7946/tcp –permanent
    firewall-cmd –add-port=7946/udp –permanent
    firewall-cmd –add-port=4789/udp –permanent

  2. Hi,

    I’m getting the below error when trying to join the worker node.Please help.

    [ishani@workernode1 ~]$ docker swarm join –token SWMTKN-1-4fkura7cfe52b4uy5mhck6m85wf213nop0res0zf23a3rjjmj2-b34cxtaimghs30sor6gbzjqek 10.*.*.*:2377
    Error response from daemon: rpc error: code = 14 desc = grpc: the connection is unavailable

  3. #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

    1. 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).
      .

  4. Pingback: Docker Tutorial : Getting Started With Docker Swarm
Leave a Reply to alias4it test Cancel reply

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

You May Also Like