Docker Swarm is a clustering system for docker. Docker is easy to setup and easy to use. It is a good way to get started with clustering, the cluster management features are limited in this version, I’m sure it will be improved in future versions. For web services, I would use Kubernetes rather than Docker Swarm, due to its high availability, scalability and rolling update features.

In this post, I’m using:

  • Debian Jessie (bare-metal)
  • Docker 1.9.0
  • Docker Swarm 1.0.0

I configured a cluster of 2 machines and started 2 containers.

Ips:

  • PC1: 172.16.43.199 Master
  • PC2: 172.16.43.201 Node

Steps:

  1. Install latest docker
  2. Configure multi-networking for master and nodes
  3. Install latest swarm
  4. Create a swarm
  5. Create nodes
  6. Start docker manager
  7. Ship containers to the swarm

PC1 runs the swarm manager and PC2 is a node.

You can have as many nodes as you want.

1- Install latest docker

Install docker from dockerproject apt repository:

apt-get update -y ;\
apt-get install -y apt-transport-https ;\
apt-key adv --keyserver hkp://pgp.mit.edu:80 --recv-keys 58118E89F3A912897C070ADBF76221572C52609D ;\
echo "deb https://apt.dockerproject.org/repo debian-jessie main" > /etc/apt/sources.list.d/docker.list ;\
apt-get update -y ;\
apt-get upgrade -y ;\
apt-get install -y docker-engine bridge-utils

Run these commands on all computers, PC1, PC2.

2. Configure multi-networking for master

I use consul for my key-value store and eth0 for networking. The overlay network is called ‘my-net’. I start docker daemon with –cluster-store and –cluster-advertise options to enable multi-networking.

service docker stop
docker daemon --cluster-store=consul://172.16.43.199:8500 --cluster-advertise=eth0:2376
docker run -d -p "8500:8500" -h "consul" progrium/consul -server -bootstrap
docker network create --driver overlay my-net

Run these commands on PC1 (Master).

3- Install latest swarm

docker pull swarm

Run this command on PC1 (Master).

4- Create a swarm

The swarm command generate the swarm ID.

docker run --rm swarm create
26339fcb112379b1f61ba3d54f33ffb7

Run on PC1 (Master).

5- Create nodes

Docker wants to have unique hostnames, I had to change the hostname of PC2:

echo "myhostname" > /etc/hostname
echo "127.0.0.1       myhostname" >> /etc/hosts
hostnamectl set-hostname myhostname

Start the docker daemon:

service docker stop
docker daemon -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock --cluster-store=consul://172.16.43.199:8500 --cluster-advertise=eth0:2376

Join the swarm using the swarm id (from another terminal):

docker run -d swarm join --addr=172.16.43.201:2375 token://26339fcb112379b1f61ba3d54f33ffb7

Run on PC2 (Node).

6- Start docker manager

To start docker manager, you need:

  • choose a swarm port: 3456
  • swarm id: 26339fcb112379b1f61ba3d54f33ffb7
docker run -d -p 3456:2375 swarm manage token://26339fcb112379b1f61ba3d54f33ffb7

Run on PC1 (Master).

7- Ship containers to the swarm

Run nginx and wget:

docker -H tcp://172.16.43.199:3456 run -itd --name=web --net=my-net nginx
docker -H tcp://172.16.43.199:3456 run -it --rm --net=my-net busybox wget -O- http://web
docker -H tcp://172.16.43.199:3456 ps

Run on PC1 or PC2.