Skip to content

Instantly share code, notes, and snippets.

@ivansharamok
Last active May 24, 2018 22:46
Show Gist options
  • Save ivansharamok/3777d59ecbd9801bd4d728ada68d20df to your computer and use it in GitHub Desktop.
Save ivansharamok/3777d59ecbd9801bd4d728ada68d20df to your computer and use it in GitHub Desktop.
3managers-2workers-docker-swarmmode
#!/bin/bash -e
# script uses docker-machine to create VMs in VirtualBox.
vm_man_prefix=${1:-"man-node"}
vm_work_prefix=${2:-"work-node"}
disk_size_mb=${3:-4000}
vm_memory=${4:-1024}
vm_driver=virtualbox
work_node_count=2
dm_exists=$(which docker-machine)
if [[ -z $dm_exists ]]; then
echo "could not locate docker-machine. Install the program and add it to the PATH."
exit 1
fi
echo "creating manager nodes named with prefix '$vm_man_prefix'..."
for i in {1..3}
do
vm_name="$vm_man_prefix$i"
# check whether vm with such name already exists
if [[ ! -z $(docker-machine ls --filter name=$vm_name -q) ]]; then
echo "vm with name $vm_name already exists skipping creation to next vm"
continue
fi
docker-machine create --driver=$vm_driver --virtualbox-disk-size=$disk_size_mb --virtualbox-memory=$vm_memory $vm_name
echo "machine created: $vm_name"
done
echo "creation of manager nodes completed"
echo "creating worker nodes named with prefix '$vm_work_prefix'..."
for i in `seq 1 $work_node_count`
do
vm_name="$vm_work_prefix$i"
# check whether vm with such name already exists
if [[ ! -z $(docker-machine ls --filter name=$vm_name -q) ]]; then
echo "vm with name $vm_name already exists skipping creation to next vm"
continue
fi
docker-machine create --driver=$vm_driver --virtualbox-disk-size=$disk_size_mb --virtualbox-memory=$vm_memory --engine-label nodetype=worker $vm_name
echo "machine created: $vm_name"
done
echo "creation of worker nodes completed"
manager_port=2377
# configure swarm managers
echo "initializing swarm and joining managers..."
swarm_initialized=false
for vm in $(docker-machine ls --filter name=$vm_man_prefix -q);
do
vm_ip=$(docker-machine ip $vm)
if [[ "$swarm_initialized" = false ]]; then
echo "initializing swarm on node '$vm'"
docker-machine ssh $vm "docker swarm init --advertise-addr $vm_ip"
manager_vm=$vm
manager_ip=$vm_ip
manager_token=$(docker-machine ssh $vm 'docker swarm join-token manager -q')
echo "manager join token: $manager_token"
swarm_initialized=true
else
echo "joining node '$vm' as manager..."
docker-machine ssh $vm "docker swarm join --advertise-addr $vm_ip --token $manager_token $manager_ip:$manager_port"
echo "node '$vm' joined cluster as manager"
fi
echo "Swarm has \"$(docker-machine ssh $vm 'docker info --format={{.Swarm.Managers}}')\" manager nodes and \"$(docker-machine ssh $vm 'docker info -f={{.Swarm.Nodes}}')\" nodes total"
done
# join swarm workers
echo "joining workers..."
worker_token=$(docker-machine ssh $manager_vm 'docker swarm join-token worker -q')
echo "worker token: $worker_token"
for vm in $(docker-machine ls --filter name=$vm_work_prefix -q);
do
echo "joining node '$vm' as worker..."
docker-machine ssh $vm "docker swarm join --token $worker_token $manager_ip:$manager_port"
echo "node '$vm' joined cluster as worker"
echo "Swarm has \"$(docker-machine ssh $manager_vm 'docker info --format={{.Swarm.Managers}}')\" manager nodes and \"$(docker-machine ssh $manager_vm 'docker info -f={{.Swarm.Nodes}}')\" nodes total"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment