Build a Swarm Cluster on Docker 1.12

Valentin Ouvrard
Jul 16, 2016 · 3 min read
Image for post
Image for post
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.

https://gist.github.com/valentin2105/c0fd4d443bc6031e8557afc67f6cc709#file-install-docker-exp-sh

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 \
IP_Master_Swarm:2377

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

Image for post
Image for post
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.

Image for post
Image for post

After, we can easily scale up our service :

docker service scale nginx=40
Image for post
Image for post
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…).

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store