Learn Docker Swarm and Implementing Scalability on Multiple Hosts

0
1774
docker-swarm

docker-swarm

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:

pull-swarm
To create the cluster, we run the command below: –

run-swarm
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:

get-ip
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.

connect-host
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.

manage
We can then use the docker ps command to check that the container is running. See the output below:
ps
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.

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.

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.

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

mac-version
We also need to install Virtualbox using the command below:

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

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

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

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

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

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.

LEAVE A REPLY

Please enter your comment!
Please enter your name here