
If you've worked with Docker containers, you already understand how powerful they can be. But did you know you can exponentially up the power of Docker by creating a cluster of Docker hosts, called a Docker swarm? Believe it or not, this process is really simple. All you need is one machine to serve as a Docker swarm manager and a few Docker hosts to join the swarm as nodes.
I want to walk you through the process of enabling a Docker swarm manager and then joining two hosts as nodes. I will be demonstrating with the Ubuntu Server 16.04 platform for all machines. Do note this process will be very similar on nearly all Linux platforms. I will assume you already have Docker installed on all machines and will demonstrate with my manager at IP address 192.168.1.139 and my nodes at IP address (docker-node-1 at 192.168.1.177, docker-node-2 at 192.168.1.178, and ZOMBIE-KING at 192.168.1.162). Each node must also be running Docker.
Let's get busy with the swarm.
Creating your manager
Creating the swarm on your manager can be done with a single command. Remember, our manager is at host 192.168.1.139, so the command to initialize the manager is:
docker swarm init --advertise-addr 192.168.1.139
When you issue the above command you will be presented with a token that consists of a fairly long string of characters. Copy that token as you will need it to join your nodes to the manager. If you now issue the command docker info, you should see (among the lengthy output):
Swarm: active
Your docker swarm is working and ready to take on nodes.
Joining nodes to your swarm
From each of the nodes, you must issue a command like so:
docker swarm join --token TOKEN 192.168.1.139:2377
Where TOKEN is the string of characters reported when you created the manager. Once the command completes, issue the command docker info from the node and you should see that the Swarm is listed as active, what its ID is, and if it is currently serving as a manager (Figure A).
Figure A
The docker info command run on one of the swarm nodes.
Deploying a service
If you go back to the Docker swarm manager (in my case, 192.168.1.139) and issue the command docker node ls, you should see your nodes listed as active (Figure B).
Figure B
We actually have three active nodes running.
With the active nodes, we can now deploy a service. Go back to the Docker swarm manager and let's create an nginx webserver service with the command:
docker service create -p 80:80 --name webserver nginx
If you issue the command docker service ls you will see your new service listed (Figure C).
Figure C
The new docker service listed.
At the moment, our webserver service is only running on one of our Docker machines. If we have the manager and two other nodes, we can scale the service up for our swarm (currently 3 machines), with the following command:
docker service scale webserver=3
If we issue the command docker service ps webserver, we should see instances of our service running. However, that might not pick up all of our nodes, so let's bump that up (say webserver=5) and catch more of our nodes (Figure D).
Figure D
We've picked up all of our nodes at this point.
If a service goes down, it will automatically be restarted on the same node or on a different node (if the original node is no longer available).
Clustering made easy
That's the gist of creating a Docker swarm and creating a service on your new cluster. To learn more about what Docker swarm can do, issue the command docker swarm —help to see the other commands you can use in conjunction with Docker swarm.
Also see
- How to create a docker image and push it to Docker Hub (TechRepublic)
- How to make working with docker containers easier with Simple Docker UI (TechRepublic)
- How to commit changes to a docker image (TechRepublic)
- 5 tips for securing your Docker containers (TechRepublic)
- Docker LinuxKit: Secure Linux containers for Windows, macOS, and clouds (ZDNet)
Full Bio
Jack Wallen is an award-winning writer for TechRepublic and Linux.com. He’s an avid promoter of open source and the voice of The Android Expert. For more news about Jack Wallen, visit his website jackwallen.com.