Swarm is the native Docker Clustering Engine for managing and deploying Docker Containers.

Preamble:

The Docker ecosystem is currently under heavy development, and new orchestration tools are being developped in order to make Docker, the main container based technology for cloud computing. At the time of writing, the Docker Development Team has released the following orchestration tools:

Due to its pluggable architecture, Docker enables components to be easily added or removed from the Docker Core as needed. In this post we will learn how to install and use Docker Swarm, the native Docker Clustering Engine for managing and deploying Docker Containers, in a clustered environment.

alt text

Requirements:

Docker Swarm is the component that is needed in order to transform a Docker Host into a Docker Swarm Node. The Swarm Node will then be added into a cluster to create a Docker Clustered Environment. The major requirements needed in order to build a Docker Cluster are:

  • Servers: at least 02 Virtual Machines or Bare Metal hosts with an x86 Operating System.
  • Docker: Docker 1.4.1 or higher is required.
  • Go (aka Golang): Google’s Go programming language. At least version 1.3 or higher is required.
  • Development Tools: Certain development tools packages will be needed.

Note: Vmware can be used to create virtual machines if you plan on using Docker Cluster in a testing or learning environment.

Deployment Plan:

It is important to mention that Docker Swarm runs on top of a Docker host.
A Docker Swarm node = A Docker Host + Docker Swarm Installed.

The process of creating a Docker Swarm Cluster will be as followed:

  1. Create a Docker Host running Docker 1.5 (or current version).
  2. Install Go, and the Development Tools
  3. Set the $GOPATH environment variable
  4. Download and Install Docker Swarm
  5. Verify and test the Swarm Nodes

Note: In order to create a Docker Cluster, we will need at least 02 servers or 02 virtual machines. In this tutorial we will use 03 Virtual Machines, running CentOS 7.0 Operating System. The 05 steps stated in the deployment plan above must be done on each server.

alt text

Installing Docker on our Servers

Installing Docker 1.5 in CentOS 7.0 is fairly simple:

  • First we need to install the EPEL repository:
1
sudo yum install epel-release
  • Next run the following command to update the system:
1
sudo yum update
  • Then, Install Docker and the Docker registry:
1
sudo yum install docker docker-registry
  • Next, we need to enable Docker to start at boot:
1
sudo systemctl enable docker.service
1
sudo systemctl start docker.service
  • Now it is time to check if Docker is up and running, and working:
1
sudo systemctl status docker.service
1
sudo docker version
1
sudo docker -v
1
sudo docker info
  • Updating the Docker Engine:
    We currently have Docker 1.3 installed in CentOS 7. To update Docker to the latest version, run the following commands:
1
cd /etc/yum.repos.d/
1
sudo vim virt7-testing.repo

Then copy and paste the following code snippet:


[virt7-testing]
name=virt7-testing
baseurl=http://cbs.centos.org/repos/virt7-testing/x86_64/os/
enabled=1
gpgcheck=0

Save and Exit, then run the following command to update the system:

1
sudo yum update
  • Now let’s check if we have Docker 1.5 Installed:
1
sudo docker version

At this point, we should have a Docker Host running Docker 1.5 Engine.

Please do not forget to repeat these same steps on the 02 remaining servers.

Installing Go and the Development Tools:

The Go language and certain Development Tools will be needed in order to download and install Docker Swarm:

1
sudo yum install golang
1
sudo yum groupinstall 'Development Tools'

At this point, we should have the Go language installed as well as the Development Tools.

Please do not forget to repeat these same steps on the 02 remaining servers.

Setting up the $GOPATH:

To properly set the $GOPATH as an environment variable, please do the following:

1
mkdir ~/go
1
export GOPATH=~/go

Please do not forget to repeat these same steps on the 02 remaining servers.

Installing Docker Swarm:

To transform our Docker Host into a Docker Swarm Node, we will need to download and install Docker Swarm. To do so run the following commands:

1
go get -u github.com/docker/swarm
1
PATH=$PATH:~/go/bin

At this point, we should have Docker Swarm installed and properly configured.

Please do not forget to repeat these same steps on the 02 remaining servers.

Verify and test the Swarm Nodes:

A Docker Swarm Node or Docker Swarm Host is just a Docker Host with Docker Swarm installed. To ensure that our Docker Swarm Nodes are working as expected, run the following commands:

1
swarm
1
swarm --version

Please do not forget to repeat these same steps on the 02 remaining servers.

At this point, we should have Docker Swarm well installed and configured in all the 03 servers. Next we will see how to create and manage a Docker Cluster.

Cluster Roles & Architecture:

Now that we have created our Docker Swarm Hosts, it is time create a Swarm Cluster where Server 01, Server 02, and Server 03 will be the members. We will use Server 01 as a Cluster Member and as a Management Console. The Management Console will receive the cluster command, then distributed it to all the members in the Cluster.
In summary our 03 Servers will have the following role:

  • Server 01 (192.168.1.101): Cluster member and Cluster Management Interface.
  • Server 02 (192.168.1.102): Cluster member only,
  • Server 03 (192.168.1.102): Cluster member only.

alt text

Cluster Management:

In this section we will create a Swarm Cluster on Server 01 (192.168.1.101), then invite Server 01, Server 02, and Server 03 to join the Cluster as members.

  • On Server 01 (192.168.1.101), run the following command to create a Swarm Cluster:
1
swarm create

The output should be the Cluster Unique ID. For example:

6856663cdefdec325839a4b7e1de38e8

Note: The Cluster Unique ID will be used by the Cluster Members in order to specify the Cluster they want to join. Do not forget to use your generated Cluster Unique ID.

  • On Server 01 (192.168.1.101), run the following command to enable the Cluster Management Service:
1
swarm manage token://6856663cdefdec325839a4b7e1de38e8 -H 192.168.1.101:2374 &
1
swarm manage --help

Note: Do not forget to replace the token and the IP address with your own settings.

  • On Server 01 (192.168.1.101), run the following commands to join the Swarm Cluster as a member:
1
swarm join token://6856663cdefdec325839a4b7e1de38e8 --addr=192.168.1.101:2375
1
export DOCKER_HOST=192.168.1.101:2374

Note: Do not forget to replace the token and the IP address with your own settings.

  • On Server 02 (192.168.1.102), run the following commands to join the Swarm Cluster as a member:
1
swarm join token://6856663cdefdec325839a4b7e1de38e8 --addr=192.168.1.102:2375
1
export DOCKER_HOST=192.168.1.101:2374

Note: Do not forget to replace the token and the IP address with your own settings.

  • On Server 03 (192.168.1.103), run the following command to join the Swarm Cluster as a member:
1
swarm join token://6856663cdefdec325839a4b7e1de38e8 --addr=192.168.1.103:2375
1
export DOCKER_HOST=192.168.1.101:2374

Note: Do not forget to replace the token and the IP address with your own settings.

  • Now we should have 03 Nodes in our Docker Swarm Cluster:
1
swarm list token://6856663cdefdec325839a4b7e1de38e8

The above command should list all the nodes registered in our Swarm Cluster.

Basic Docker Swarm Commands:

If you know how to run Docker commands, you already know how to run Swarm Commands.

  • docker run --> Lets you create and run Docker containers.
  • docker ps --> Lets you list all running docker containers.
  • docker rm -f $(docker ps -aq) --> Lets you delete all Docker containers.

What is next:

Docker Swarm is still a bleeding edge technology that is currently under active development. It is not production ready yet, but has some interesting features that a Cloud Enthusiast or Docker Fan must be able to play with. To follow the evolution of this project, here are some important websites to visit:

Comments