Last active
May 29, 2018 09:01
-
-
Save dkdndes/ff3b9f9239600b5f81456634389bb4ab to your computer and use it in GitHub Desktop.
Start a Docker Swarm Clustern within "Docker in Docker (doid)"
This file contains hidden or 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 | |
# Init Swarm master | |
docker swarm init | |
# Get join token: | |
SWARM_TOKEN=$(docker swarm join-token -q worker) | |
echo $SWARM_TOKEN | |
# Get Swarm master IP (Docker for Mac xhyve VM IP) | |
SWARM_MASTER_IP=$(docker info | grep -w 'Node Address' | awk '{print $3}') | |
echo $SWARM_MASTER_IP | |
# Next, setup environment variables for what Docker version to use and how many worker nodes you want: | |
# Docker version or change it to dind and use the latest version | |
DOCKER_VERSION=18.05.0-ce-dind | |
# Number of workers | |
NUM_WORKERS=3 | |
# Now, we can launch the worker nodes as containers and ask them to join our Docker Swarm cluster: | |
# Run NUM_WORKERS workers with SWARM_TOKEN | |
for i in $(seq "${NUM_WORKERS}"); do | |
docker run -d --privileged --name worker-${i} --hostname=worker-${i} -p ${i}2375:2375 docker:${DOCKER_VERSION} | |
docker --host=localhost:${i}2375 swarm join --token ${SWARM_TOKEN} ${SWARM_MASTER_IP}:2377 | |
done | |
# The first docker run command might take some time due to the required Docker image needs to be downloaded, otherwise | |
# it should only take a few seconds to setup the Swarm cluster! | |
# Check the docker swarm node cluster is around | |
docker node ls |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For more information, like "how to tear down a node, rebalancing of instances, etc. check out the blog