Automatic Docker Swarm cluster setup with Vagrant and Ansible
The following software should be installed:
- Vagrant
- Vagrant Virtualbox plugin:
vagrant plugin install virtualbox
Just run:
$ vagrant up
and wait for setup to complete
apt-proxy
- utility host with apt-cacher-ng to reduce network trafficmanager-1
- first Docker Swarm managermanager-2
, etc... - additional Docker Swarm managersworker-
, etc... - Docker Swarm workers
Vagrant file contains following constants:
IMAGE_NAME = "ubuntu/focal64"
- Vagrant box image."ubuntu/focal64"
- the only one is supportedWORKERS_COUNT = 2
- Workers countADDITIONAL_MANAGERS_COUNT = 2
- Additional Docker Swarm managers countSUBNET_BASE = "192.168.200."
- Private subnet addressAPT_PROXY_IP = SUBNET_BASE + "2"
-apt-proxy
ip addressFIRST_MANAGER_IP = SUBNET_BASE + "3"
-manager-
ip addressDOMAIN = 'local'
- domain
The following roles are used:
apt-proxy
- setup apt-cacher-ng with docker repo proxyapt-setup
- Vagrant related. Disable unattended updatesdeploy-portainer
- Deploy portainer stack on clusterdeploy-traefik
- Deploy traefik reverse-proxy stackdocker-install
- Install and configure Docker CEswarm-first-manager
- Initialize Docker Swarm cluster and copy join tokens to ansible hostswarm-join-node
- join swarm node to previously configured cluster using join token
apt-proxy-playbook.yml
- setupapt-proxy
first-manager-playbook.yml
- setupmanager-1
swarm-node-playbook.yml
- setup swarm nodes
Redirect ports to host:80 and host:443 Add the following lines to /etc/hosts
127.0.0.1 portainer.<DOMAIN_NAME>
127.0.0.1 traefik.<DOMAIN_NAME>
Add port redirection to services to be available on 80 and 443 ports:
sudo iptables -t nat -I OUTPUT -p tcp -d 127.0.0.1 --dport 80 -j REDIRECT --to-ports 8080
sudo iptables -t nat -I OUTPUT -p tcp -d 127.0.0.1 --dport 443 -j REDIRECT --to-ports 8443
Remove port redirection:
sudo iptables -t nat -D OUTPUT -p tcp -d 127.0.0.1 --dport 80 -j REDIRECT --to-ports 8080
sudo iptables -t nat -D OUTPUT -p tcp -d 127.0.0.1 --dport 443 -j REDIRECT --to-ports 8443
Create docker context:
$ docker context create vagrant --docker host=ssh://vagrant@localhost:2222
Add vagrant ssh key:
$ eval $(ssh-agent)
$ ssh-add ~/.vagrant.d/insecure_private_key
Invoke docker command on remove host
docker --context vagrant ps