7 min read

In this article by Russ McKendrick, the author of the book Docker Bootcamp, we will cover following topics:

  • Creating a Swarm manually
  • Launching a service

(For more resources related to this topic, see here.)

Creating a Swarm manually

To start off with we need to launch the hosts and to do this, run the following commands, remembering to replace the digital ocean API access token with your own:

docker-machine create 
    --driver digitalocean 
    --digitalocean-access-token 57e4aeaff8d7d1a8a8e46132969c2149117081536d50741191c79d8bc083ae73 
    swarm01

docker-machine create 
    --driver digitalocean 
    --digitalocean-access-token 57e4aeaff8d7d1a8a8e46132969c2149117081536d50741191c79d8bc083ae73 
    swarm02

docker-machine create 
    --driver digitalocean 
    --digitalocean-access-token 57e4aeaff8d7d1a8a8e46132969c2149117081536d50741191c79d8bc083ae73 
    swarm03

Once launched, running docker-machine ls should show you a list of your images. Also, this should be reflected in your digital ocean control panel:

Now we have our Docker hosts and we need to assign a role to each of the nodes within the cluster. Docker Swarm has two node roles:

  • Manager: A manager is a node which dispatches tasks to the workers, all your interaction with the Swarm cluster will be targeted against a manager node.You can have more than one manger node, however in this example we will be using just one.

Worker: Worker nodes accept the tasks dispatched by the manager node, these are where all your services are launched. We will go in to services in more detail once we have our cluster configured.

In our cluster, swarm01 will be the manager node with swarm02 and swarm03 being our two worker nodes. We are going to use the docker-machine ssh command to execute commands directly on our three nodes, starting with configuring our manager node.

The commands in the walk through will only work with Mac and Linux, commands to run on Windows will be covered at the end of this section.

Before we initialize the manager node, we need to capture the IP address of swarm01 as a command-line variable:

managerIP=$(docker-machine ip swarm01)

Now that we have the IP address, run the following command to check if it is correct:

echo $managerIP

And then to configure the manager node to run:

docker-machine ssh swarm01 docker swarm init --advertise-addr $managerIP

You will then receive confirmation that swarm01 is now a manager along with instructions on what to run to add a worker to the cluster:

You don’t have to a make a note of the instructions as we will be running the command in a slightly different way.

To add our two workers, we need to capture the join token in a similar way we captured the IP address of our manager node using the $managerIP variable, to do this run:

joinToken=$(docker-machine ssh swarm01 docker swarm join-token -q worker)

Again, you echo the variable out to check that it is valid:

echo $joinToken

Now it’s time to add our two worker nodes into the cluster by running:

docker-machine ssh swarm02 docker swarm join --token $joinToken $managerIP:2377
docker-machine ssh swarm03 docker swarm join --token $joinToken $managerIP:2377

You should see something same as the following terminal output:

Connecting your local Docker client to the manager node using:

eval $(docker-machine env swarm01)

and then running a docker-machine ls again shows. As you can see from the list of hosts,swarm01 is now active but there is nothing in the SWARM column, why is that?

Confusingly, there are two different types of Docker Swarm cluster, there is the Legacy Docker Swarm which was managed by Docker machine, and then there is the new Docker Swarm mode which is managed by the Docker engine itself.

We have a launched a Docker Swarm mode cluster, this is now the preferred way of launching Swarm, the legacy Docker Swarm is slowly being retired.

To get a list of the nodes within our Swarm cluster we need to run the following command:

For information on each node you can run the following command (the –pretty flag renders the JSON output from the Docker API):

docker node inspect swarm01--pretty

You are given a wealth of information about the host, including the fact that it is a manager and it has been launched in digital ocean. Running the same command, but for a worker node shows using similar information:

docker node inspect swarm02 --pretty

However, as the node is not a manager that section is missing.

Before we look at launching services into our cluster we should look at how to launch our cluster using Docker machine on Windows as there are a few differences in the commands used due differences between powershell and bash.

First, we need to launch the three hosts:

docker-machine.exe create --driver digitalocean --digitalocean-access-token 57e4aeaff8d7d1a8a8e46132969c2149117081536d50741191c79d8bc083ae73 swarm01
docker-machine.exe create --driver digitalocean --digitalocean-access-token 57e4aeaff8d7d1a8a8e46132969c2149117081536d50741191c79d8bc083ae73 swarm02
docker-machine.exe create --driver digitalocean --digitalocean-access-token 57e4aeaff8d7d1a8a8e46132969c2149117081536d50741191c79d8bc083ae73 swarm03

Once the three hosts are up and running:

You can create the manager node by running:

$managerIP = $(docker-machine.exe ip swarm01)
echo $managerIP
docker-machine.exe ssh swarm01 docker swarm init --advertise-addr $managerIP

Once you have your manager you can add the two worker nodes:

$joinIP = “$(docker-machine.exe ip swarm01):2377”
echo $joinIP
$joinToken = $(docker-machine.exe ssh swarm01 docker swarm join-token -q worker)
echo $joinToken
docker-machine.exe ssh swarm02 docker swarm join --token $joinToken $joinIP
docker-machine.exe ssh swarm03 docker swarm join --token $joinToken $joinIP

and then configure your local Docker client to use your manager node and check the cluster status:

docker-machine.exe env --shell powershell swarm01 | Invoke-Expression
docker-machine.exe ls
docker node ls

At this stage, no matter which operating system you are using, you should have a three node Docker Swarm cluster in digital ocean, we can now look at a launching service into our cluster.

Launching a service

Rather than launching containers using the docker container run command you need to create a service A service defines a task which the manager then passes to one of the workers and then a container is launched:

docker service create --name cluster -p:80:80/tcp russmckendrick/cluster

That’s it, we should now have a single container running on one of our three nodes. To check that the service is running and get a little more information about the service, run the following commands:

docker service ls
docker service inspect cluster --pretty

Now that we have confirmed that our service is running, you will be able to open your browser and enter the IP address of one of your three nodes (which you can get by running docker-machine ls).One of the features of Docker Swarm is it’s routing mesh:

A routing mesh? When we exposed the port using the -p:80:80/tcp flag, we did a little more than map port 80 on the host to port 80 on the container, we actually created a Swarm load balancer on port 80 across all of the hosts within the cluster. The Swarm load balancer then directs requests to containers within our cluster.

Running the commands as shown following, should show you which tasks are running on which nodes, remember tasks are containers which have been launched by the service:

docker node ps swarm01
docker node ps swarm02
docker node ps swarm03

Like me, you probably have your single task running on swarm01:

We can make things more interesting by scaling our service to add more tasks, to do this simply run the following commands to scale and check our service:

docker service scale cluster=6
docker service ls
docker service inspect cluster --pretty

As you should see, we now have 6 tasks running within our cluster service.

Checking the nodes should show that the tasks are evenly distributed between our three nodes:

docker node ps swarm01
docker node ps swarm02
docker node ps swarm03

Hitting refresh in your browser should also update the hostname under the Docker image change, another way of seeing this on Mac and Linux is to run the following command:

curl -s http://$(docker-machine ip swarm01)/ | grep class=

As you can see from the terminal in following output, our requests are being load balanced between the running tasks:

Before we terminate our Docker Swarm cluster let’s look at another way we can launch services, before we do we need to remove the currently running service, to do this simply run:

docker service rm cluster

Summary

In this article we have learned how to create a Swarm manually, and how to launch a service.

Resources for Article:


Further resources on this subject:


Packt

Share
Published by
Packt

Recent Posts

Top life hacks for prepping for your IT certification exam

I remember deciding to pursue my first IT certification, the CompTIA A+. I had signed…

3 years ago

Learn Transformers for Natural Language Processing with Denis Rothman

Key takeaways The transformer architecture has proved to be revolutionary in outperforming the classical RNN…

3 years ago

Learning Essential Linux Commands for Navigating the Shell Effectively

Once we learn how to deploy an Ubuntu server, how to manage users, and how…

3 years ago

Clean Coding in Python with Mariano Anaya

Key-takeaways:   Clean code isn’t just a nice thing to have or a luxury in software projects; it's a necessity. If we…

3 years ago

Exploring Forms in Angular – types, benefits and differences   

While developing a web application, or setting dynamic pages and meta tags we need to deal with…

3 years ago

Gain Practical Expertise with the Latest Edition of Software Architecture with C# 9 and .NET 5

Software architecture is one of the most discussed topics in the software industry today, and…

3 years ago