Create a multiple-node Swarm cluster using Docker Machine

If you want to try out Docker Swarm in your Mac, you can use Docker Machine to create a multiple node cluster. This is handy when you don't have access to additional servers at home. Docker machine is included when you download the latest version of Docker. Here is the version I have in my Mac.

Screenshot-2017-11-16-19.56.42

Before we start, you also need Virtualbox. Once you've downloaded and installed Vitrualbox, you can proceed with creating our initial node.

Creating nodes is very straightforward! Just execute the ff. command:

docker-machine create node1

Screenshot-2017-11-16-20.00.16

Create 2 additional nodes since we need 3 nodes

docker-machine create node2
docker-machine create node3

Screenshot-2017-11-16-20.03.47

You can check the 3 nodes created in Virtualbox. Docker Machine creates Linux VMs with docker installed.

Screenshot-2017-11-16-20.04.46

Now we have our 3 nodes, you can ssh to node1 so we can initialise our docker swarm cluster.

docker-machine ssh node1

Once you're inside the node, execute. Initially, it will complain because there are 2 network interfaces in the node.

docker swarm init

Choose a subnet to use and pass the --advertise-addr argument with the IP address of the interface you chose. For this example, I chose the 192* interface.

docker swarm init --advertise-addr 192.168.99.106

Screenshot-2017-11-16-20.11.21

Now, we have 1 node in our cluster and it is a manager. Meaning, we can schedule tasks from this node. Copy the docker swarm join command since we'll use it later. Don't worry if you missed copying this, you can retrieve a new token again.

Before we continue, we need to add node2 and node3 as workers to the cluster. ssh to these nodes and execute.

docker swarm join --token SWMTKN-1-2nq2xutkd2jqqtac3d39ibmvjxdif3jl09empxo7gceh7jlhmy-8mfi7mxcv6hfn1wu0thyv3t97 192.168.99.106:2377

Screenshot-2017-11-16-20.12.10

Screenshot-2017-11-16-20.12.35

After joining node2 and node3 as workers, you can check from node1 if those nodes are available from the manager.

Screenshot-2017-11-16-20.12.51

Let's now create services in our new cluster!

For convenience, I've created a docker stack for this demo. Just clone my repo from node1.

git clone https://github.com/donvito/dockerstack.git

Screenshot-2017-11-16-20.17.31

Let's run our demo stack!

cd dockerstack
docker stack deploy -c demo-docker-machine-stack.yml demostack

Screenshot-2017-11-16-20.29.45-1

Execute the command below to check if the services are running

docker service ls

That's it! You now have your own swarm cluster which you can play with! Let's connect in LinkedIn, Twitter and github!

If you still have spare time, you can continue to my next blog post Configure Portainer with a Docker Swarm Cluster.