Software DevelopmentLearn Docker Swarm and Implementing Scalability on Multiple Hosts

Learn Docker Swarm and Implementing Scalability on Multiple Hosts


Docker swarm is the native solution that has been developed for setting up Docker clusters. With Docker swarm, you are able to convert multiple hosts into a single virtual host. Docker swarm uses the standard API so you can use it to scale any tool that uses the standard API. Some tools that can be scaled are Jenkins, Docker client and Docker compose among others. We can look at swarm as a component for building complex applications. The implementation of swarm is a container that centrally manages the cluster and runs as an agent on each host.

The process of setting up swarm begins by pulling the container. This is accomplished by the command below:

sudo docker pull swarm

To create the cluster, we run the command below: –

docker run --rm swarm create

When we ran the command above it returned a hash which is the cluster ID. The cluster ID uniquely identifies the Docker cluster.

Because we are dealing with a cluster, Docker swarm needs to know where the hosts and containers are located. There are three approaches of discovering hosts which are etc, consul and a token, and a discovery approach which is the default way of discovering hosts. To connect a host to Docker swarm we run the container and pass the ip address of host and the token to join command. The token is the hash returned when the cluster was created. We also need to include the port when specifying the IP address.

To get the IP address and the port where the Docker host is running, use the command below:

ip addr show docker0

Once the IP address is returned, we can pass it to joining argument together with the hash returned at cluster creation. An example of joining a host is shown below.

sudo docker run -d swarm join --addr = token://4f4ea76d095ff55501d98a6968aa10fb

The command above will launch an agent and return a hash to the agent container. We can use the sudo docker ps command to check that the agent container is running. The role of a swarm agent is to host containers. The agents can also be referred to as nodes or docker nodes. The agents are just regular daemons, so it is possible to remotely connect to them.

The previous steps have demonstrated how to add a single host to the cluster. We need to repeat the process on all the hosts, we would like to add to the cluster.

After creating the agents, we need to create one swarm manager. The swarm manager is responsible for scheduling all cluster containers. The command below shows us how to create a swarm manager. We need to specify a port that is not in use and the hash returned at cluster creation.

sudo docker run -d -p 3356 swarm manage token://4f4ea76d095ff55501d98a6968aa10fb

We can then use the docker ps command to check that the container is running. See the output below:
When you need to return a list of all nodes running on your cluster, you use the list argument and pass the token as shown in the command below.

sudo docker run --rm swarm list token://4f4ea76d095ff55501d98a6968aa10fb

After creating the swarm manger and nodes, we have a complete cluster and we can begin interacting with it. The approach we have so far described of setting up a swarm cluster does not have TLS enabled. We need to disable use of TLS when accessing the swarm port. We can do this by disabling DOCKER_HOST, DOCKER_TLS_VERIFY, DOCKER_TLS and DOCKER_CERT_PATH. The commands below will disable the settings, but it is a good idea to note these settings so that you can restore them after having a feel of Docker swarm.


After clearing the DOCKER_HOST setting and assigning it to the IP address and port of our swarm manager, we will interact with the cluster using the usual commands. The command below sets the IP and port.

export DOCKER_HOST="tcp://"

The approach we discussed in the previous steps is one way of setting up a Docker swarm. Another approach of setting up a Docker swarm is using a docker machine. On ubuntu, you need to install the docker machine using the command below which downloads and extracts the docker machine.

curl -L `uname -s`-`uname -m` >/usr/local/bin/docker-machine && \
chmod +x /usr/local/bin/docker-machine

Check the docker-machine version you are running to confirm the install was successful.

docker-machine version

We also need to install Virtualbox using the command below:

sudo sh -c "echo 'deb '$(lsb_release -cs)' contrib non-free' > /etc/apt/sources.list.d/virtualbox.list" && wget -q -O- | sudo apt-key add - && sudo apt-get update && sudo apt-get install virtualbox-5.0

After installing Virtualbox, we can go ahead and create a virtualbox named Demo. The command below will accomplish that.

Sudo docker-machine create -d virtualbox demo

To add the configuration of the machine, we created to our shell we use the command below.

eval "$(docker-machine env local)"

To create the swarm manager, we use the command below and pass the token as we did in the previous approaches.

docker-machine create -d virtualbox --swarm --swarm-master --swarm-discovery token://fe0cc96a72cf04dba8c1c4aa79536ec3 swarm-master

To create a node, we use the command below with token remaining as previous.

 docker-machine create -d virtualbox --swarm --swarm-discovery token://fe0cc96a72cf04dba8c1c4aa79536ec3 swarm-agent-00

To create the cluster connection, we use the command below.

eval $(docker-machine env --swarm swarm-master)

The cluster is now ready and you can interact with it.

In this tutorial, we introduced the Docker swarm as a technology that is used to create a Docker cluster. We discussed how to pull and run the swarm image. We discussed how to create agents which are the nodes in our cluster. We also discussed how to create the swarm manager. We discussed how to disable TLS and use Docker swarm in the usual way. We introduced an alternative approach of setting up the Docker swarm using Docker machine. We demonstrated how to install the Docker machine and the Oracle Virtualbox. We explained how to create nodes and swarm manager. Finally, we demonstrated how to connect the nodes to the manager.


Please enter your comment!
Please enter your name here

Exclusive content

- Advertisement -

Latest article


More article

- Advertisement -