Skip to content

Instantly share code, notes, and snippets.

@pecigonzalo
Last active June 24, 2019 12:04
Show Gist options
  • Save pecigonzalo/88808bb3eeeecdf83127bcc18cf9829f to your computer and use it in GitHub Desktop.
Save pecigonzalo/88808bb3eeeecdf83127bcc18cf9829f to your computer and use it in GitHub Desktop.
Docker swarm creation script
#!/bin/bash
MANAGER=${1:-2}
WORKER=${2:-3}
#=========================
# Creating cluster members
#=========================
echo "### Creating $MANAGER managers"
echo "### Creating $WORKER workers"
for i in $(seq 1 $MANAGER); do
docker-machine create --driver virtualbox swarm-manager$i
done
for i in $(seq 1 $WORKER); do
docker-machine create --driver virtualbox swarm-worker$i
done
#===============
# Starting swarm
#===============
MANAGER_IP=$(docker-machine ip swarm-manager1)
echo "### Initializing main master: swarm-manager$i"
docker-machine ssh swarm-manager1 docker swarm init \
--advertise-addr $MANAGER_IP
#===============
# Adding members
#===============
MANAGER_TOKEN=$(docker-machine ssh swarm-manager1 docker swarm join-token --quiet manager)
WORKER_TOKEN=$(docker-machine ssh swarm-manager1 docker swarm join-token --quiet worker)
for i in $(seq 2 $MANAGER); do
echo "### Joining manager: swarm-manager$i"
docker-machine ssh swarm-manager$i docker swarm join \
--token $MANAGER_TOKEN \
$MANAGER_IP:2377
done
for i in $(seq 1 $WORKER); do
echo "### Joining worker: swarm-manager$i"
docker-machine ssh swarm-worker$i docker swarm join \
--token $WORKER_TOKEN \
$MANAGER_IP:2377
done
docker-machine ssh swarm-manager1 docker node ls
echo "### Join your client to the cluster using:"
echo " eval \$(docker-machine env swarm-manager1)"
echo "### or run using:"
echo " docker-machine ssh swarm-manager1 docker node ls"
@paradoja
Copy link

paradoja commented Jan 5, 2017

Maybe the docker command and the driver should be overridable with env variables?

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