Creating Our First Swarm

To create our first swarm we will be using the spring pet clinic application. The application will be deployed across four(4) virtual machines/nodes created using vagrant. The schematic diagram below shows the architecture of the system:

Initializing the Nodes

To initialize the nodes/virtual machines perform the steps below:

Clone the docker-swarm-mode repository:

git clone https://github.com/denisdbell/docker-swarm-mode.git

2. Navigate to the docker-swarm-mode folder and type the following command:

vagrant up

When this command is executed for the first time it takes several minutes to run. This is primarily because the “boxcutter/ubuntu1610” image is being downloaded. Subsequent executions will be much faster since the image would have already been downloaded.

After the vagrant up command has run successfully, access the Master node by executing the following command:

vagrant ssh Master

Initializing Docker Swarm Mode On The Master Node

Swarm mode is not enabled by default. To enable it we must execute the following command:

docker swarm init --advertise-addr 192.168.99.201

To verify that the swarm is initialized execute the following command:

docker info | grep Swarm

You should see the following displayed:

Swarm: active

The Join Command

When the command to initialize the swarm is executed (docker swarm init) a join command is produced. This join command will allow worker nodes to be added to the swarm. See an example of the join command below:

To add a worker to this swarm, run the following command:
docker swarm join --token SWMTKN-1-5cyt5f348qxs3qye0uy9r8p5oqw7qb2vndzv3rnu7geneskrkz-11mggfs4apy7enmqbnz7706uv 192.168.99.201:2377

Save this command as we will be using it in the next section.

Adding Worker Nodes To The Swarm

To add workers to the swarm is simple, let’s start by adding Worker1 by using the following steps:

1. SSH into the Worker1 node:

vagrant ssh Worker1

2. Execute the join command

Note: The command below is just an example, you will have to use your own join command.

Creating Our First Service

In docker swarm, a service is used to deploy containers. A service is essentially the definition of the desired state of the containers in a swarm. The first service that will be created is the visualizer service. It will be used to view the nodes and containers within our swarm. Follow the steps below to create the visualizer service:

The name of the service is set to viz using the –name=viz parameter. The internal port 8080 is mapped to the external port 9090 using the –publish=9090:8080/tcp parameter. The service will only be created on the Manager node using—constraint=node.role==manager parameter.

To verify that the service has been created type the following command:

You can see that the viz service is on the Master node. We currently have no services on the Worker nodes.

Load Balancing

Even though the viz service is deployed on the Master node it can still be accessed from the worker nodes. This is because of load balancing feature of Docker Swarm. Navigate to http://192.168.99.211:9090 (Worker1 node ip address) and you will be able to access the visualization service.