Docker Swarm Cluster Setup With Vagrant

Gaurav Talele
3 min readDec 3, 2019

--

GOAL: in this article we will start two node Docker Swarm Cluster and go over commands to manage multiple nodes. We will put a detailed glance on topics 1) starting the Swarm 2) viewing Swarm status 3) joining the Swarm 4) obtaining join-tokens 5) Run Containers with replication 6) Response to fail-over

System Requirements

Considering we will do this LAB on Windows System. On this System we will Install Chocolatey + Vagrant + Install Virtualbox.

Open PowerShell as a Administrator:

Set-ExecutionPolicy Bypass -Scope Process -Force; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))

Restart The Powershell with administrator privileges.

choco install virtualbox vagrant --yes
mkdir DockerSwarmCluster && cd DockerSwarmCluster

Open Command Prompt and navigate to DockerSwarmCluster directory.

create a new file named Vagrantfile. and paste following code.

$install_docker_script = <<SCRIPT
echo Installing Docker...
curl -sSL https://get.docker.com/ | sh
sudo usermod -aG docker ubuntu
SCRIPT
$manager_script = <<SCRIPT
echo Swarm Init...
sudo docker swarm init --listen-addr 10.100.199.200:2377 --advertise-addr 10.100.199.200:2377
sudo docker swarm join-token --quiet worker > /vagrant/worker_token
SCRIPT
$worker_script = <<SCRIPT
echo Swarm Join...
sudo docker swarm join --token $(cat /vagrant/worker_token) 10.100.199.200:2377
SCRIPT
Vagrant.configure('2') do |config|vm_box = 'ubuntu/xenial64'config.vm.define :manager, primary: true do |manager|
manager.vm.box = vm_box
manager.vm.box_check_update = true
manager.vm.network :private_network, ip: "10.100.199.200"
manager.vm.network :forwarded_port, guest: 8080, host: 8080
manager.vm.network :forwarded_port, guest: 5000, host: 5000
manager.vm.hostname = "manager"
manager.vm.synced_folder ".", "/vagrant"
manager.vm.provision "shell", inline: $install_docker_script, privileged: true
manager.vm.provision "shell", inline: $manager_script, privileged: true
manager.vm.provider "virtualbox" do |vb|
vb.name = "manager"
vb.memory = "1024"
end
end
(1..2).each do |i|
config.vm.define "worker0#{i}" do |worker|
worker.vm.box = vm_box
worker.vm.box_check_update = true
worker.vm.network :private_network, ip: "10.100.199.20#{i}"
worker.vm.hostname = "worker0#{i}"
worker.vm.synced_folder ".", "/vagrant"
worker.vm.provision "shell", inline: $install_docker_script, privileged: true
worker.vm.provision "shell", inline: $worker_script, privileged: true
worker.vm.provider "virtualbox" do |vb|
vb.name = "worker0#{i}"
vb.memory = "1024"
end
end
end
end

About The Vagrantfile:

This makes two different virtualboxes, one named: manager, another named: worker01 and worker02.

With the way the Vagrantfile configuration, connects each virtualbox to a private network with IP Addresses 10.100.199.200 (manager), 10.100.199.201 (worker01) and 10.100.199.202 (worker02).

All actions taken on manager, worker01,worker02 are equivalent to working on separate computers.

vagrant up

Now this will fetch the ubuntu/xenial64 box from vagrant cloud and make the necessary configuration including installation of docker engine and docker compose.

Once the process exited.

Command Prompt 1 > vagrant ssh manager
Command Prompt 2 > vagrant ssh worker01
Command Prompt 3 > vagrant ssh worker02

Create a service At Manager Node

docker service create --name my_web 
--replicas 7
--publish published=8080,target=80
nginx

now, See all the docker containers from the nodes.

vagrant@manager  > docker container ls
vagrant@worker01 > docker container ls
vagrant@worker02 > docker container ls

Why Should we use Vagrant To Setup Docker Swarm

Vagrant is a tool for building and managing virtual machine environments in a single workflow. With an easy-to-use workflow and focus on automation, Vagrant lowers development environment setup time, increases production parity, and makes the “works on my machine” excuse a relic of the past. Vagrant is convenient to share virtual environment setup and configurations.

Vagrant allows us very easily to share setups between team members allowing very easy spin up of a work environment.

Conclusion

Vagrant provides an easy way to define and share a different application or environment setup in a single text file called Vagrantfile. Vagrant uses virtualization engines like VirtualBox, VMWare or Hyper-V and builds on top of them

--

--

Gaurav Talele
Gaurav Talele

Written by Gaurav Talele

An ambitious Full Stack evangelist in Angular, Typescript, Spring Boot Node JS, C#, Dot Net Core WEB API, MS SQL, Redis, MongoDB, RabbitMQ, Docker and AWS.

No responses yet