Skip to content

Instantly share code, notes, and snippets.

@markwylde
Last active December 10, 2021 13:06
Show Gist options
  • Save markwylde/daddd5129dec3a513835193230446764 to your computer and use it in GitHub Desktop.
Save markwylde/daddd5129dec3a513835193230446764 to your computer and use it in GitHub Desktop.
Create a Docker Swarm cluster with a configuration amount of workers on Digital Ocean or using Virtual Box
# How many swarm workers do you want?
WORKER_COUNT="${WORKER_COUNT:-5}"
# What would you like to prefix all your VM's with?
CLUSTER_NAME="${CLUSTER_NAME:-mycluster}"
# Set this to true to use virtualbox instead of digitalocean driver
LOCAL_VIRTUALBOX="${LOCAL_VIRTUALBOX:-true}"
# Virtualbox Configuration
DIGITAL_OCEAN_TOKEN="${DIGITAL_OCEAN_TOKEN:-enter-digital-ocean-token-here}"
DIGITAL_OCEAN_REGION="${DIGITAL_OCEAN_REGION:-lon1}"
DIGITAL_OCEAN_SIZE="${DIGITAL_OCEAN_SIZE:-s-2vcpu-2gb}"
DIGITAL_OCEAN_IMAGE="${DIGITAL_OCEAN_IMAGE:-ubuntu-18-10-x64}"
# PRINT FUNCTION
function pretty_print {
lightblue="\033[0;36m"
end="\033[0m"
echo -e "${lightblue}${1}${end}"
}
# CREATE MASTER NODE
pretty_print "\n----- Bringing up ${CLUSTER_NAME}-master1"
if [ "$LOCAL_VIRTUALBOX" = true ] ; then
docker-machine create \
--driver virtualbox \
${CLUSTER_NAME}-master1
else
docker-machine create \
--driver digitalocean \
--digitalocean-size=$DIGITAL_OCEAN_SIZE \
--digitalocean-region=$DIGITAL_OCEAN_REGION \
--digitalocean-image=$DIGITAL_OCEAN_IMAGE \
--digitalocean-access-token=$DIGITAL_OCEAN_TOKEN \
${CLUSTER_NAME}-master1
fi
pretty_print "\n----- Connecting to ${CLUSTER_NAME}-master1"
docker-machine env ${CLUSTER_NAME}-master1
eval $(docker-machine env ${CLUSTER_NAME}-master1)
pretty_print "\n----- Initialising swarm cluster"
docker swarm init --advertise-addr $(docker-machine ip ${CLUSTER_NAME}-master1)
pretty_print "\n----- Generating join token"
SWARM_TOKEN=$(docker-machine ssh ${CLUSTER_NAME}-master1 "docker swarm join-token worker -q")
echo "SWARM_TOKEN=${SWARM_TOKEN}"
# CREATE WORKER NODES
pretty_print "\n----- Bringing up workers"
for (( i=1; i<=$WORKER_COUNT; i++ ))
do
(
echo "Bringing up ${CLUSTER_NAME}-worker$i"
if [ "$LOCAL_VIRTUALBOX" = true ] ; then
docker-machine create \
--driver virtualbox \
${CLUSTER_NAME}-worker$i
else
docker-machine create \
--driver digitalocean \
--digitalocean-size=$DIGITAL_OCEAN_SIZE \
--digitalocean-region=$DIGITAL_OCEAN_REGION \
--digitalocean-image=$DIGITAL_OCEAN_IMAGE \
--digitalocean-access-token=$DIGITAL_OCEAN_TOKEN \
${CLUSTER_NAME}-worker$i
fi
join_cmd="docker swarm join \
--token=${SWARM_TOKEN} \
--listen-addr $(docker-machine ip ${CLUSTER_NAME}-worker$i) \
--advertise-addr $(docker-machine ip ${CLUSTER_NAME}-worker$i) \
$(docker-machine ip ${CLUSTER_NAME}-master1)"
docker-machine ssh ${CLUSTER_NAME}-worker$i $join_cmd
) | sed "s/^/[${CLUSTER_NAME}-worker$i] /" &
done
wait
pretty_print "\n----- Finish provising swarm cluster"
echo -e ""
echo -e "To connect to the swarm master you can run:"
echo -e ""
echo -e " eval \$(docker-machine env ${CLUSTER_NAME}-master1)"
echo -e ""
@markwylde
Copy link
Author

markwylde commented Jul 2, 2019

You can run the above directly without any modification, and it will spin up a 5 worker swarm in virtualbox.

Alternatively you can configure it by either editing the file or setting environment variables.

export WORKER_COUNT="10"
export CLUSTER_NAME="example"
export LOCAL_VIRTUALBOX="false"
export DIGITAL_OCEAN_TOKEN="enter-digital-ocean-token-here"
export DIGITAL_OCEAN_REGION="lon1"
export DIGITAL_OCEAN_SIZE="s-2vcpu-2gb"
export DIGITAL_OCEAN_IMAGE="ubuntu-18-10-x64"
./create-swarm-cluster.sh

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment