Tools Docker Swarm Lead image: Lead Image © Author, 123RF.com
Lead Image © Author, 123RF.com
 

Scale your Docker containers with Docker Swarm

Extending the Hive

If you've experienced the power of a single Docker container, you're in for a treat. Thanks to Docker Swarm, you can scale that container with the help of as many nodes as you need. By Jack Wallen

Swarm is a clustering and scheduling tool for Docker containers. Not only does it make the clustering of containers possible, it makes it quite easy. Once deployed, that swarm will behave as if it were a single, virtual system.

Why is this important? In a word, redundancy. Should you deploy a single container for a system and that container were to go down, so, too, would follow the system. By making use of Docker Swarm, if one container goes down, the other nodes will pick up the slack. That redundancy is crucial for business uptime.

Installing Docker

Before I get into showing you how to launch a Docker swarm, I'll first install Docker and deploy a single container. I'll be demonstrating by deploying the ever-popular Nginx container on Ubuntu Server 18.04.

To begin, install Docker on Ubuntu Server. I'll be setting up three Ubuntu Server machines: one to serve as the manager and two to serve as nodes. The following steps should take place on all three machines.

Because it's always wise to update and upgrade, you can take care of that first. Remember, however, should the kernel be upgraded in the process, you'll want to reboot the server, so make sure you run the update/upgrade at a time when a reboot is possible.

Log in to Ubuntu Server and issue:

sudo apt-get update
sudo apt-get upgrade -y

Once the upgrade completes, reboot (if necessary). Now you can move on to installing Docker, which is incredibly easy because Docker is found in the standard repositories. From the terminal window, issue the command

sudo apt-get install docker.io -y

which will pick up all the necessary dependencies. When installation finishes, you then need to add your user to the docker group; deploying containers with sudo can lead to security issues, so it's best to run the docker command as a standard user.

To add your user to the docker group, enter:

sudo usermod -aG docker $USER

With the user added, log out and log back in. You should now be able to run the docker command as that user – no sudo required.

Once you've taken care of these first steps on the manager and both nodes, you're ready to deploy.

Deploying a Container

Before I get into deploying a service on the cluster, it's best to review how to deploy a container with the docker command. Because I'm focusing on Nginx, I'll deploy that as a single container. The first thing to do is pull down the latest official version of the Nginx image from Docker Hub:

docker pull nginx

From this image, you can then create as many containers as you like. To deploy Nginx as a container on your network (exposing it on both internal and external port 80), run

docker run --name NGINX -p 80:80 nginx

where is NGINX the name of the created Nginx container. If you're already using port 80 for another web server on your network, you could expose the container's internal port 80 to network port 8080:

docker run --name NGINX -p 8080:80 nginx

This command would then direct all network traffic to the server's IP address on port 8080 to the container on port 80. The problem with running the command, as illustrated above, is that you won't get your prompt back. The only way you can get back to the command line is to kill the container (Ctrl+C).

To deploy the container and get your command prompt back, you must deploy the container in detached mode, which means a container will run in the background of your terminal,

docker run --name NGINX -p 8080:80 -d nginx

so you can continue working, without having to open another terminal. This command would return your prompt back to you, ready for more work.

Now that you know how to deploy a container, you'll want to kill the Nginx deployment, so the port is available for the swarm. When you deploy a container, it will have an associated ID (a random string of characters). To discover the ID, issue the command:

docker ps -a

You will see the ID listed in the first column. If the ID in question is f9756e10a9d1, enter

docker stop f9756e10a9d1
docker rm f9756e10a9d1

to kill and delete that container. Note that you don't have to type the entire ID; the first four characters will work.

Now you are free to begin the process of creating your first Docker swarm.

Creating the Manager

The first postinstall step is to create the manager. The swarm manager is charged with receiving commands on behalf of the cluster and assigning containers to nodes. The manager uses the Raft consensus algorithm [1] to keep track of swarm states. Without this algorithm, nodes would fall out of sync, and failover wouldn't be possible. The manager is also where containers are deployed into the swarm, so once the nodes are connected, you will spend most of your Docker time working from the manager.

Before I continue, I'll take a look at the IP addresses I'll be creating:

Make sure to alter the hostnames and IP addresses to suit your configurations. If you're testing this on an internal network, with the same network scheme as above, you might want to start out by assigning the machines the same hostnames and IP addresses that I have for the sake of simplicity.

On the dockermanager machine, the swarm is created with a single command. Remember, the manager is at IP address 192.168.1.71, so the command for initialization is:

docker swarm init --advertise-addr 192.168.1.71

When this command completes, you'll be presented with a token (Figure 1) to be used when joining nodes. Make sure to copy that token into a file, because you will need it to join the nodes. To ensure your swarm is active, enter:

docker info
The swarm token used to join nodes.
Figure 1: The swarm token used to join nodes.

You should see, among a long list of information (Figure 2), the line Swarm: active.

The swarm manager is now active and ready for connections.
Figure 2: The swarm manager is now active and ready for connections.

Joining a Node to the Swarm

Now it's time to join nodes to the swarm. Remember the token you were given after initializing the manager? You'll need that now. The command to join a node to the swarm is

docker swarm join --token TOKEN 192.168.1.71:2377

where TOKEN is the token generated by the swarm initialization.

When you run the command, on success, you will see that the node has been joined to the swarm (Figure 3). Run that same join command on all the nodes you want in the swarm. Once you're done, head back to the manager and, again, issue the command:

docker info
The first node has been joined to the swarm.
Figure 3: The first node has been joined to the swarm.

You should now see that the swarm is active, the machine is serving as a manager, and a total of three machines are in the swarm (Figure 4).

A functioning swarm.
Figure 4: A functioning swarm.

To see the nodes listed, go to the manager and enter:

docker node ls

The three nodes are listed (Figure 5), with dockermanager as the Leader.

The list of nodes.
Figure 5: The list of nodes.

Deploying a Service

With an active swarm, you can now deploy your first Nginx container; go back to the manager and issue the command:

docker service create -p 80:80 --name webserver nginx

The breakdown of the above command is simple:

To make sure the new service is listed, enter:

docker service ls

You should now see that the webserver service is running on port 80 (Figure 6).

The service is good to go.
Figure 6: The service is good to go.

Scaling Your Service

At the moment, your service is only running on one node. Because you have three nodes available, why not scale it up to use all three? To do so, you instruct Docker to scale the webserver service to three nodes with the command (run on the manager):

docker service scale webserver=3

Docker will start deploying the service to all three nodes (Figure 7). To verify the service has been scaled to all nodes, enter:

docker service ps webserver
Docker is scaling the service to all three nodes.
Figure 7: Docker is scaling the service to all three nodes.

You should now see that the three nodes have the service running (Figure 8).

The webserver has been scaled to all three nodes.
Figure 8: The webserver has been scaled to all three nodes.

If the service goes down on any one node, it will automatically be restarted on the same node or, if the original node is no longer available, a different node.

Promoting and Demoting Nodes

Docker allows you to promote and demote nodes, which comes in handy if a manager node becomes unavailable. For example, say your dockermanager needs to go down (for maintenance or some other reason), and you need to promote dockernode1 to a manager role. To do that, issue the command (on the manager):

docker node promote dockernode1

The command will inform you the node has been promoted (Figure 9).

A node has been promoted to manager.
Figure 9: A node has been promoted to manager.

If you want to demote a node (e.g., because you have the original dockermanager back up and functioning), you can demote dockernode1 back to regular node status (Figure 10) with the command (again, run from the manager):

docker node demote dockernode1
A node has been demoted back to standard node status.
Figure 10: A node has been demoted back to standard node status.

You can enter

docker node promote dockernode1 dockernode2
docker node demote dockernode1 dockernode2

to promote/demote more than one node at a time, as well.

Leaving the Swarm

If, for whatever reason you have a node you want to pull out of the swarm, Docker has you covered. Head over to the node you want removed from the swarm and issue the command:

docker swarm leave

Once the node has left the swarm, enter (on the manager)

docker node rm dockernode2
docker node ls

to remove the node from the node list and verify that dockernode2 has, in fact, been removed (Figure 11). You can rejoin that node to the swarm with the same command you used before,

docker swarm join --token TOKEN 192.168.1.71:2377
dockernode2 is no longer on the list.
Figure 11: dockernode2 is no longer on the list.

where TOKEN is the token issued when the manager was originally initialized.

At some point, you might need to force a manager to leave the swarm. To do this, enter:

docker swarm leave --force

At this point, your swarm is no longer functioning. To get it started again, start from the beginning to recreate your swarm.

Conclusion

With a few quick commands run on your Linux servers, you can scale containers with Docker Swarm to match the needs of your project or your business. Make use of three, four, five, 10, 20, … however many nodes it takes to give your containers the necessary horsepower and failover you need. With the help of a cluster of containers, your business can become more agile and enjoy serious uptime for services and apps.