When Docker released its latest version, Docker Engine v1.12, it included quite a few changes to the capabilities provided by Docker Swarm. In today’s article, we’ll be exploring how to deploy a service using Docker’s Swarm Mode.
Activating Swarm Mode on Ubuntu 16.04
Before we can deploy a service on a Docker Engine Swarm, we will first need to set up a Swarm Cluster. Since we’re showing capabilities added with 1.12, we will also be installing the latest version of Docker Engine.
The following instructions will guide you through installing Docker Engine on Ubuntu 16.04. For other versions and platforms, you can follow Docker’s official installation guide.
Setting Up the Docker Apt Repository
For this installation, we’ll be using the standard installation method for Ubuntu, which relies on the Apt package manager. Because we will be installing the latest version of Docker Engine, we need to configure Apt to pull the
docker-engine package from Docker’s official Apt repository rather than the systems preconfigured repositories.
Add Docker’s Public Key
The first step in configuring Apt to use a new repository is to add that repository’s public key into Apt’s cache with the
apt-key adv --keyserver hkp://p80.pool.sks-keyservers.net:80 --recv-keys 58118E89F3A912897C070ADBF76221572C52609D
apt-key command requests the specified key (
58118E89F3A912897C070ADBF76221572C52609D), from the
p80.pool.sks-keyservers.net key server. This public key will be used to validate all packages downloaded from the new repository.
Specifying Docker’s Repository Location
With Docker’s public key now imported, we can configure Apt to use the Docker Project’s repository server. We will do this by adding an entry within the
echo "deb https://apt.dockerproject.org/repo ubuntu-xenial main" >> /etc/apt/sources.list.d/docker.list
When we refresh Apt’s package cache, Apt will look through all files within the
sources.list.d/ directory to find new package repositories. The command I just showed you creates (if it doesn’t already exist) a new file called
docker.list with an entry that adds the
Updating Apt’s package cache
To refresh Apt’s package cache, we can run the
apt-get command with the
This will cause Apt to repopulate its list of repositories by rereading all of its configuration files, including the one we just added. It will also query those repositories to cache a list of available packages.
Before installing Docker Engine, we need to install a prerequisite package. The
linux-image-extra package is a kernel specific package that’s needed for Ubuntu systems to support the
aufs storage driver. This driver is used by Docker for mounting volumes within containers.
To install this package, we will use the
apt-get command again, but this time with the
apt-get install linux-image-extra-$(uname -r)
apt-get command, the
$(uname -r) portion or the command will return the kernel version for the current running kernel. Any kernel updates to this system should include the installation of the appropriate
linux-image-extra package version that coincides with the new kernel version. If this package is not updated as well, some issues may arise with Docker’s ability to mount volumes.
Install Docker Engine
With Apt configured and the
linux-image-extra prerequisite package installed, we can now move to installing Docker Engine. To do so, we will once again use the
apt-get command with the
install option to install the
apt-get install docker-engine
At this point, we should have
Docker Engine v1.12.0 or newer installed. To verify that we have the correct version, we can execute the
docker command with the
#docker version Client: Version: 1.12.0 API version: 1.24 Go version: go1.6.3 Git commit: 8eab29e Built: Thu Jul 28 22:11:10 2016 OS/Arch: linux/amd64 Server: Version: 1.12.0 API version: 1.24 Go version: go1.6.3 Git commit: 8eab29e Built: Thu Jul 28 22:11:10 2016 OS/Arch: linux/amd64
With this, we can see that both the Server and Client versions are
1.12.0. From here, we can move on to creating our Swarm.
Creating a Docker Swarm
From this point on within this article, we will be executing tasks from several machines. To help make things a bit more clear, I have included the hostname in the command examples.
We will start our Swarm Cluster with two nodes. At this point, both of these nodes should have Docker Engine installed based on the instructions above.
When creating the Swarm Cluster, we will need to designate a node manager. For this example, we’ll be using a host by the name of
swarm-01 as a node manager. To make
swarm-01 a node manager, we need to create our Swarm Cluster by executing a command on
swarm-01 first. The command we will be executing is the
docker command with the
swarm init options.
root@swarm-01:~# docker swarm init --advertise-addr 10.0.0.1 Swarm initialized: current node (awwiap1z5vtxponawdqndl0e7) is now a manager. To add a worker to this swarm, run the following command: docker swarm join \ --token SWMTKN-1-51pzs5ax8dmp3h0ic72m9wq9vtagevp1ncrgik115qwo058ie6-3fokbd3onl2i8r7dowtlwh7kb \ 10.0.0.1:2377 To add a manager to this swarm, run the following command: docker swarm join \ --token SWMTKN-1-51pzs5ax8dmp3h0ic72m9wq9vtagevp1ncrgik115qwo058ie6-bwex7fd4u5aov4naa5trcxs34 \ 10.0.0.1:2377
With the above command, in addition to the
swarm init, we also specified the
--advertise-addr flag with a value of
10.0.0.1. This is the IP that the Swarm node manager will use to advertise the Swarm Cluster Service. While this address can be a private IP, it’s important to note that in order for nodes to join this swarm, those nodes will need to be able to connect to the node manager over this IP on port
After running the
docker swarm init command, we can see that the
swarm-01 was given a node name (
awwiap1z5vtxponawdqndl0e7) and made the manager of this swarm. The output also supplies two commands: one command is to add a node worker to the swarm and the other is to add another node manager to the swarm.
Docker Swarm Mode can support multiple node managers. It will, however, elect one of them to be the primary node manager which will be responsible for orchestration within the Swarm.
Adding a Node Worker to the Swarm Cluster
With the Swarm Cluster created, we can now add a new node worker using the
docker command provided by the output of the Swarm creation.
root@swarm-02:~# docker swarm join \ > --token SWMTKN-1-51pzs5ax8dmp3h0ic72m9wq9vtagevp1ncrgik115qwo058ie6-3fokbd3onl2i8r7dowtlwh7kb \ > 10.0.0.1:2377 This node joined a swarm as a worker.
In this example, we added
swarm-02 to the swarm as a node worker. A node worker is a member of the Swarm Cluster whose role is to run tasks; in this case, tasks are containers. The node manager, on the other hand, has a role of managing the orchestration of tasks (containers) and maintaining the Swarm Cluster itself.
In addition to its node manager duties, a node manager is also a node worker, which means it will also run tasks for our Swarm Cluster.
Viewing the current Swarm nodes
With the previous commands executed, we now have a basic two-node Swarm Cluster. We can verify the status of this cluster by executing the
docker command with the
node ls options.
root@swarm-01:~# docker node ls ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS 13evr7hmiujjanbnu3n92dphk swarm-02.example.com Ready Active awwiap1z5vtxponawdqndl0e7 * swarm-01.example.com Ready Active Leader
From the output of this command, we can see that both
swarm-02 are in a
Active state. With this, we can now move on to deploying services to this Swarm Cluster. We'll cover that next time in part two of this tutorial.