Docker Tutorial : Getting Started With Docker Swarm

Docker swarm is a clustering tool for docker. You can form a swarm cluster using one or more docker hosts. Like Kubernetes cluster manager, swarm schedules the containers on to the swarm cluster based on resource availability. You can also specify the resource for a container while deploying it. Swarm is designed to pack containers on to a host by saving other host resources for bigger containers rather than scheduling it randomly to a host in the cluster.

This tutorial is based on old swarm version. Find the latest swarm cluster tutorial here

In this article, we will learn how to set up  docker swarm cluster.

Docker Swarm Setup

In this setup, we will use two docker host (node1 and node2)  and one docker client machine (devopscube) with swarm configured on it.

You Might Like: First Look: Native Docker Clustering Video Course

Note: All the machines should have docker installed on it. You can check out this tutorial to install and configure docker on a host. How to install and configure docker

You need to configure the docker hosts to accept connections from a remote client on port 2375. You can configure this by following the steps given below. You need to perform theses stop on all the hosts.

1. Stop the docker service.

2.  Execute the following command to start and make the docker host listen to connections on 2375

Note: You need to perform the above step in all the nodes which is going to be the part of the swarm cluster. Also, if your server in the public network, do not use instead, use the ip of the docker client machine in which you execute the swarm commands.

3.  Now, leave the connection open. You will need these terminals to see what’s happening in each host.

READ  Getting Started With Docker : Working With Containers

Now we have two nodes and one client machine with Docker installed and the docker daemon listening to 2375. Now in the client machine, we need to setup swarm for creating the cluster. Perform the steps given below on the client machine, not on node1 and node2.

1. Install go using the following command.

2. create a directory named “go”

3. export the go path using the following command.

4. Get the swarm binary using the following command. It will download the swarm files to the gopath (go directory) we set in the 3rd step.

5. Add swarm to the PATH variable using the following command.

6. Verify the swarm setup by executing the swarm version command.

8. You can view all the available swarm options by just executing the swarm command.

Now we have the swarm setup ready. Next is to create a cluster by combining node1 and node2.

READ  Docker Networking Tutorial : Connect Containers Across Hosts Using Weave

Setting Up Swarm Cluster

1. From the client machine (devopscube) execute the following command to create a swarm cluster.

2.  As you can see, the swarm create command created a unique  cluster id

(73f8bc512e94195210fad6e9cd58986f) . Note down unique id because we will be using the unique id to join node1 and 2 to the swarm cluster.

3. Nodes can be joined to the cluster using the following command syntax.

Execute the following command on with relevant parameters to join node1 to the cluster. You will not see any output for the command.

Close the terminal and execute the above command with node2 parameters to join it to the cluster.

3. Now let’s list the nodes in our swarm cluster using the following command.

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.