Although Kubernetes has become the de facto standard for container orchestration we still find environments that use Docker Swarm mode. This repo provides the configuration to spin up a Docker Swarm mode cluster using Vagrant.
This repository was created for the purpose of being a platform for tonyskidmore/docker-swarm-monitoring.
The configuration in this repository is designed and tested to run on a Windows 10 system (tested on an Intel i7 laptop with 8GB RAM) using Vagrant, Docker Toolbox and VirtualBox.
Note: Docker Desktop is now the preferred method to install Docker support on WIndows 10 so it should work with that too but for my requirements, in this particular example, I needed to work with the older Docker Toolbox.
Tested versions of products (may work with different versions):
- Windows 10 20H2
- Docker Toolbox 19.03.1
- VirtualBox 6.1.16 (updated after installing Docker Toolbox)
- Vagrant - Windows 64-bit 2.2.13
- Git for Windows 64-bit 2.29.2
In the above diagram we can see that after deployment we will have a Docker Swarm mode 3-node cluster with a single manager and 2 x worker nodes. The design is as simple as possible to demonstrate some higher level concepts of working with Swarm and purposely avoids production level aspects such as TLS certificates, security and high availability. All that we want for our purposes is a simple 3-node cluster.
The following steps will bring up the Docker swarm mode cluster once you have installed all of the products detailed in the Testing environment section. Depending on the speed of your system and Internet connection the environment should hopefully be up and running in around an hour or so.
mkdir \vagrant
cd \vagrant
git clone https://github.com/tonyskidmore/docker-swarm.git
cd docker-swarm
vagrant up
It can be useful to take snapshots of the cluster nodes pre-deployment of services as a quicker method to rolling back to a pristine state, rather than having to vagrant destroy
and then vagrant up
. I generally prefer to take offline snapshots and to do this you could perform the following commands:
cd \vagrant\docker-swarm
# shutdown cluster nodes
vagrant halt
# snapshot all nodes
1..3 | % { & vagrant snapshot save "docker-swarm-0$_" "pre-deployment" }
# bring up cluster again
vagrant up
To restore the snapshots perform that same actions above but replace the second step with:
# snapshot restore all nodes
1..3 | % { & vagrant snapshot restore "docker-swarm-0$_" "pre-deployment" --no-start }
Now that the environment has been deployed the Docker Swarm mode can be accessed from the Windows host system. Run Windows PowerShell and enter the following commands:
$env:DOCKER_HOST="192.168.217.133:2375"
$env:DOCKER_TLS_VERIFY=""
docker node ls
Note: The DOCKER_HOST
and DOCKER_TLS_VERIFY
environment variable commands will need to be set on each new PowerShell session that is opened. These commands tell the docker
command to connect to our manager node and not to use TLS.
Example expected output:
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION
ty3mfoy14ldrpj4q5vsf5rpjp * docker-swarm-01 Ready Active Leader 19.03.13
lrqp5wvpf978iq94du4ccr83i docker-swarm-02 Ready Active 19.03.13
qwcz1i9xg5v3f9cav9ba39zvf docker-swarm-03 Ready Active 19.03.13
Should you wish to SSH into any of the Docker Swarm mode nodes you can use the vagrant ssh <nodename>
command. For example, to SSH into our manager node:
cd \vagrant\docker-swarm
vagrant ssh docker-swarm-01
[vagrant@docker-swarm-01 ~]$
When the test cluster is not being actively used, the nodes can be suspended and resumed at will. This can be done by moving to the location of the vagrant file and issuing the suspend
and resume
commands:
cd \vagrant\docker-swarm
# suspend our docker swarm nodes
vagrant suspend
# resume our docker swarm nodes
vagrant resume
Alternatively the VMs can be stopped and started:
cd \vagrant\docker-swarm
# shutdown our docker swarm nodes
vagrant halt
# start up docker swarm nodes
vagrant up
Once testing has been completed the Docker swarm mode cluster can be destroyed by running the following commands:
cd \vagrant\docker-swarm
vagrant destroy -force
Swarm mode overview
Getting Started with Docker Swarm Mode by Wes Higbee