Last active
September 27, 2020 17:56
-
-
Save nathanleclaire/64be6d12d1ccc37faee6 to your computer and use it in GitHub Desktop.
Script to bootstrap multihost swarm with Docker Machine (DIGITALOCEAN_ACCESS_TOKEN env var must be set, and experimental client binary available as dockerx locally)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -e | |
# Create this many swarm workers | |
export N_WORKERS=1 | |
# Coloring info | |
export bold=$(tput bold) | |
export normal=$(tput sgr0) | |
function neighbor_ip_option() { | |
# Only set the neighbor IP for the nodes created after the first. | |
if [[ "$1" != "swarm-0" ]]; then | |
echo "--engine-label com.docker.network.driver.overlay.neighbor_ip=$(docker-machine ip swarm-0)" | |
fi | |
} | |
# Bring up a machine using the overlay network for every container | |
function create_mcn_on_overlay() { | |
docker-machine --debug create \ | |
-d digitalocean \ | |
--digitalocean-image ubuntu-14-10-x64 \ | |
--digitalocean-region sfo1 \ | |
--engine-install-url experimental.docker.com \ | |
--engine-opt kv-store=consul:$(docker-machine ip consul):8500 \ | |
--engine-opt default-network=overlay:multihost \ | |
--engine-label com.docker.network.driver.overlay.bind_interface=eth0 \ | |
$(neighbor_ip_option "$1") \ | |
"$1" | awk -v prefix="${bold}$1 => ${normal}" '{print prefix $0}' | |
} | |
# Join the created swarm with the machine of a given name | |
function mcn_join_swarm() { | |
dockerx $(docker-machine config "$1") run -d \ | |
--restart always \ | |
--net bridge \ | |
swarm:latest join --addr $(docker-machine ip "$1"):2376 token://$SWARM_TOKEN | |
} | |
# Create droplet used to run Consul | |
docker-machine --debug create \ | |
-d digitalocean \ | |
--digitalocean-region sfo1 \ | |
--engine-install-url experimental.docker.com \ | |
consul | |
# Run Consul container used for service discovery | |
dockerx $(docker-machine config consul) run -d \ | |
-p 8500:8500 \ | |
-h consul \ | |
progrium/consul -server -bootstrap | |
# First create swarm master | |
create_mcn_on_overlay "swarm-0" | |
# Then create swarm workers | |
for i in $(seq 1 "$N_WORKERS"); do | |
mcn_name="swarm-$i" | |
create_mcn_on_overlay "$mcn_name" & | |
done | |
# Wait for creates to finish | |
wait | |
export SWARM_TOKEN=$(dockerx $(docker-machine config consul) run swarm create) | |
# Run Swarm master / manager container | |
dockerx $(docker-machine config swarm-0) run -d \ | |
--restart always \ | |
--net bridge \ | |
-p 3376:3376 \ | |
-v /etc/docker:/etc/docker \ | |
swarm:latest manage \ | |
--tlsverify \ | |
--tlscacert="/etc/docker/ca.pem" \ | |
--tlscert="/etc/docker/server.pem" \ | |
--tlskey="/etc/docker/server-key.pem" \ | |
-H "tcp://0.0.0.0:3376" \ | |
--strategy spread \ | |
"token://$SWARM_TOKEN" | |
for i in $(seq 0 "$N_WORKERS"); do | |
mcn_name="swarm-$i" | |
echo "Joining ${mcn_name} to the Swarm..." | |
mcn_join_swarm ${mcn_name} | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment