Build a Swarm Cluster on Docker 1.12

Docker everywhere

The version 1.12 of Docker has been released few days ago. 
Among the changes, Docker-Swarm get embedded directly into the Engine that allow easier Swarm deployment. I’m going to show that to you.

Docker Swarm is native clustering for Docker. It turns a pool of Docker hosts into a single, virtual Docker host.

Earlier stages, it was quite hard to deploy a Swarm cluster, you had to generate some certs, use a service discovery, configure each node… 
This time is over ! 
Today, we are going to make a Swarm cluster in two commands !

In this post, I’ll show you how to make a small cluster between two virtual machines but you can easily repeat operations to make a larger cluster.
I’ll use two Ubuntu 16.04 Xenial VM and install Docker 1.12 on each.

When your VMs have Docker 1.12 installed on it, we are going to create our Swarm cluster. Launch this command on the VM you want use for Master :

docker swarm init

This command will init the cluster and give you, at end, the command to launch on your nodes to join us to the cluster. Launch it on other nodes :

docker swarm join --secret b4imgvm35f5g3ecz4zb8onaac \
--ca-hash sha256:0477ebe823aec35703073c6j6529e62d3cb3g2c28d6d9e2bdfddcc69ebcad5c6 \

With this second command, your node joins directly your Swarm cluster (in two commands, no jokes). You have definitely a Docker Swarm Cluster !

Docker Swarm is too easy since 1.12

For going a bit deeper in Swarm, we’ll deploy a service and expose it on our nodes (port 8001/tcp) who will be load-balanced in the cluster.
In our example, we’re going to use Nginx, but you can use any other image of your choice.

We launch these commands on the Master :

docker network create -d overlay nginx-network
docker service create --name nginx --network nginx-network --replicas 5 -p 8001:80/tcp nginx

With this command, we have created a private network for Nginx and a service (new Docker concept) who expose the port 8001 off our Swarm’s nodes and load-balance it to 5 Nginx containers.

After, we can easily scale up our service :

docker service scale nginx=40
Nginx scaled at 40 containers

I you want to read more about the new Swarm, let’s RTFM.

For me, this update makes really easier the deployment of a Swarm Cluster from scratch. This is a good point for Docker Inc in the big war or container orchestration (Kubernetes, Mesos, Rancher…).