Docker Swarm: Introduction and basics

docker

A docker instance is good to run task in a small scale. However as the traffic increases, or when more processing power is required, when we want to scale-up the infrastructure, the way to move is Docker Swarm, which is a group of Docker nodes joined into a cluster.

The Docker Swam cluster is managed by one node, that will be designated as the Swarm Manager.

In this tutorial we will create a Docker Swarm with 3 nodes.

Prerequisites for the lab setup:

  • Deploy three Docker nodes with IPs 192.168.1.101, 192.168.1.102 and 192.168.1.103 respectively. You may follow the example posted earlier by clicking here.
  • Set hostnames of the nodes as docker1, docker2 and docker3 respectively.
  • Ensure Selinux is disabled in all nodes
  • Ensure firewall such as firewalld is disabled in all three nodes.

Let us make the node “docker1” as the master/manager node. Run the following command and copy the line in the output that you will need to run on the other two nodes to make then “worker” nodes.
[root@docker1 ~]# docker swarm init –advertise-addr 192.168.1.101

Run the following command in the Manager node, and take a note of the lines that shows the number of “Managers” and “Nodes
[root@docker1 ~]# docker info

Now let us list the nodes in the cluster using the following command. As we add mode nodes, the entries in the list too will increase:
[root@docker1 ~]# docker node ls

Let us join docker2 and docker3 to the cluster by running the following command in both nodes. This is a command that was displayed when we created the Manager node. This will defer from cluster to cluster.

[root@docker3 ~]# docker swarm join –token SWMTKN-1-4r2573r9xchcpeldaare4wv14yp3ropmwse2celwfpxyoenmmg-c9851tevz3xbhn3ctg7m2di8v 192.168.1.101:2377

[root@docker2 ~]# docker swarm join –token SWMTKN-1-4r2573r9xchcpeldaare4wv14yp3ropmwse2celwfpxyoenmmg-c9851tevz3xbhn3ctg7m2di8v 192.168.1.101:2377

Now let us list the nodes in the cluster using the following command in the Manager node:
[root@docker1 ~]# docker node ls

Before proceeding, ensure you have as http server Docker image “centos_http” in your Manager node following the tutorial “Docker container: CentOS with Apache“, which can be accessed by clicking here. Ensure the web page can be viewed by accessing the URL “http://192.168.1.101:1337“. Once this test is completed, ensure the container is stopped and no images are running, nor anything listening to the port 1337 that was used earlier.

Let us start the above http application as a service in the cluster by running the following command in the Master node:
[root@docker1 docker]# docker service create –replicas 1 –name myWebpage1 -p 1337:80 centos_http

Now try accessing the following URLs:

  • URL http://192.168.1.101:1337
  • URL http://192.168.2.101:1337
  • URL http://192.168.3.101:1337

You can list the serives running on the cluster by running the following command from the Master node. Notice the column “REPLICAS” and “ID“.
[root@docker1 docker]# docker service ls

Now let us get more details of the service referenced by the ID listed above:
[root@docker1 docker]# docker service inspect –pretty <service ID>

Identify the node where the service is running. Notice the column “NODE”.
[root@docker1 docker]# docker service ps <Node name>

In the above example you are running the service only in one of the node in the cluster. You may come to a need of scaling it to two or more. The command to scale the setup to use two nodes is as follows:

[root@docker1 docker]# docker service ls
[root@docker1 docker]# docker service scale <Node ID>=2

Verify if the service is running in two nodes now by using the command:
[root@docker1 docker]# docker service ps myWebpage1

Once you are done with this lab, you may delete the service by running the command:
[root@docker1 docker]# docker service rm myWebpage1