Skip to content

Instantly share code, notes, and snippets.

@lilithmooncohen
Last active April 1, 2019 22:28
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save lilithmooncohen/65346985c99cdc469d96 to your computer and use it in GitHub Desktop.
Save lilithmooncohen/65346985c99cdc469d96 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Description: This script spins up a multi node Docker Swarm w/ Docker
# Networking and Consul discovery w/ Registrator
# Author: Ryan C Koch
# ryanckoch@gmail.com
# Usage: bash docker-playground.sh usage
PLAYGROUND_NAME="docker-playground"
CONSUL_IMAGE="progrium/consul"
REGISTRATOR_IMAGE="gliderlabs/registrator:master"
usage ()
{
echo "Usage: $0 [argument]"
echo
echo "[argument]: create"
echo " start"
echo " status"
echo " stop"
echo " restart"
echo " destroy"
}
create ()
{
echo "##### STARTING $PLAYGROUND_NAME #####"
echo "### STARTING NODE $PLAYGROUND_NAME-consul ###" && \
docker-machine create -d virtualbox $PLAYGROUND_NAME-consul && \
echo "### STARTING SERVICES ON $PLAYGROUND_NAME-consul ###" && \
provision_consul && \
echo "### STARTING NODE $PLAYGROUND_NAME-swarm1 ###" && \
docker-machine create -d virtualbox \
--swarm --swarm-image="swarm" --swarm-master \
--swarm-discovery="consul://$(docker-machine ip $PLAYGROUND_NAME-consul):8500" \
--engine-opt="cluster-store=consul://$(docker-machine ip $PLAYGROUND_NAME-consul):8500" \
--engine-opt="cluster-advertise=eth1:2376" \
$PLAYGROUND_NAME-swarm1 && \
echo "### STARTING SERVICES ON $PLAYGROUND_NAME-swarm1 ###" && \
provision_swarm1 && \
echo "### STARTING NODE $PLAYGROUND_NAME-swarm2 ###" && \
docker-machine create -d virtualbox \
--swarm --swarm-image="swarm" \
--swarm-discovery="consul://$(docker-machine ip $PLAYGROUND_NAME-consul):8500" \
--engine-opt="cluster-store=consul://$(docker-machine ip $PLAYGROUND_NAME-consul):8500" \
--engine-opt="cluster-advertise=eth1:2376" \
$PLAYGROUND_NAME-swarm2 && \
echo "### STARTING SERVICES ON $PLAYGROUND_NAME-swarm2 ###" && \
provision_swarm2 && \
echo "##### $PLAYGROUND_NAME STARTED #####"
}
provision_consul()
{
docker $(docker-machine config $PLAYGROUND_NAME-consul) rm -f consul-server registrator || true && \
docker $(docker-machine config $PLAYGROUND_NAME-consul) run -d \
--net=host \
--name=consul-server \
-p 8300:8300 \
-p 8301:8301 \
-p 8301:8301/udp \
-p 8302:8302 \
-p 8302:8302/udp \
-p 8400:8400 \
-p 8500:8500 \
-p 8600:53/udp \
$CONSUL_IMAGE \
-dc=local \
-server -advertise $(docker-machine ip $PLAYGROUND_NAME-consul) \
-bootstrap-expect 1 && \
docker $(docker-machine config $PLAYGROUND_NAME-consul) run -d \
--name=registrator \
-e SERVICE_NAME=registrator \
-v /var/run/docker.sock:/tmp/docker.sock \
$REGISTRATOR_IMAGE \
consul://$(docker-machine ip $PLAYGROUND_NAME-consul):8500
}
provision_swarm1()
{
docker $(docker-machine config $PLAYGROUND_NAME-swarm1) rm -f consul-client registrator || true && \
docker $(docker-machine config $PLAYGROUND_NAME-swarm1) run -d \
--name consul-client \
--net=host \
-e SERVICE_NAME=consul-client \
-p 8300:8300 \
-p 8301:8301 \
-p 8301:8301/udp \
-p 8302:8302 \
-p 8302:8302/udp \
-p 8400:8400 \
-p 8500:8500 \
-p 53:53 \
-p 53:53/udp \
$CONSUL_IMAGE \
-dc=local \
-advertise $(docker-machine ip $PLAYGROUND_NAME-swarm1) -join $(docker-machine ip $PLAYGROUND_NAME-consul) && \
docker $(docker-machine config $PLAYGROUND_NAME-swarm1) run -d \
--name=registrator \
-e SERVICE_NAME=registrator \
-v /var/run/docker.sock:/tmp/docker.sock \
$REGISTRATOR_IMAGE \
consul://$(docker-machine ip $PLAYGROUND_NAME-swarm1):8500
}
provision_swarm2()
{
docker $(docker-machine config $PLAYGROUND_NAME-swarm2) rm -f consul-client registrator || true && \
docker $(docker-machine config $PLAYGROUND_NAME-swarm2) run -d \
--name consul-client \
--net=host \
-e SERVICE_NAME=consul-client \
-p 8300:8300 \
-p 8301:8301 \
-p 8301:8301/udp \
-p 8302:8302 \
-p 8302:8302/udp \
-p 8400:8400 \
-p 8500:8500 \
-p 53:53 \
-p 53:53/udp \
$CONSUL_IMAGE \
-dc=local \
-advertise $(docker-machine ip $PLAYGROUND_NAME-swarm2) -join $(docker-machine ip $PLAYGROUND_NAME-consul) && \
docker $(docker-machine config $PLAYGROUND_NAME-swarm2) run -d \
--name=registrator \
-e SERVICE_NAME=registrator \
-v /var/run/docker.sock:/tmp/docker.sock \
$REGISTRATOR_IMAGE \
consul://$(docker-machine ip $PLAYGROUND_NAME-swarm2):8500
}
start ()
{
docker-machine start $PLAYGROUND_NAME-consul && \
provision_consul && \
docker-machine start $PLAYGROUND_NAME-swarm1 && \
provision_swarm1 && \
docker-machine start $PLAYGROUND_NAME-swarm2 && \
provision_swarm2
}
stop ()
{
docker-machine stop $PLAYGROUND_NAME-consul
docker-machine stop $PLAYGROUND_NAME-swarm1
docker-machine stop $PLAYGROUND_NAME-swarm2
}
status ()
{
docker-machine ls | grep $PLAYGROUND_NAME
}
destroy ()
{
stop
docker-machine rm $PLAYGROUND_NAME-consul
docker-machine rm $PLAYGROUND_NAME-swarm1
docker-machine rm $PLAYGROUND_NAME-swarm2
}
details ()
{
echo
echo "consul: http://$(docker-machine ip $PLAYGROUND_NAME-consul):8500" && \
echo "swarm: tcp://$(docker-machine ip $PLAYGROUND_NAME-swarm1):3376" && \
echo && \
echo "To connect to the swarm run:" && \
echo 'eval $(docker-machine env --swarm '$PLAYGROUND_NAME'-swarm1)'
}
case "$1" in
create)
create
details
;;
start)
start
details
;;
stop)
stop
;;
restart)
stop
start
details
;;
status)
status
details
;;
destroy)
stop
destroy
;;
*)
usage
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment