Skip to content

Instantly share code, notes, and snippets.

@byrnedo
Last active February 5, 2017 22:08
Show Gist options
  • Save byrnedo/9e3038696303c8f3a147 to your computer and use it in GitHub Desktop.
Save byrnedo/9e3038696303c8f3a147 to your computer and use it in GitHub Desktop.
Start 3 Machine Local Docker Swarm
#!/bin/bash
set -euo pipefail
function useDockerEnv {
eval "$(docker-machine env $1)"
}
function getIfaceIP {
local machine="$1"
local iface="$2"
docker-machine ssh "$machine" "ifconfig $iface | grep 'inet addr:' | cut -d: -f2 | awk '{ print \$1}'"
}
function machineExists {
set +e
docker-machine ls -q --filter "name=$1"
set -e
}
function clearup {
activeMachine="$(docker-machine active 2 > /dev/null)"
if [[ $activeMachine ]]
then
eval "$(docker-machine env -u $activeMachine)" > /dev/null 2>&1
fi
docker rm -f consul-local 2>/dev/null
}
set +e
clearup
set -e
echo
echo
echo
echo Creating Consul node \(swarm-node-01\)
echo
echo
echo
if [[ ! $(machineExists swarm-node-01) ]]
then
#10.0.2.2 is the host ip in virtualbox
docker-machine create -d virtualbox swarm-node-01
fi
CONSUL_IP="$(docker-machine ip swarm-node-01)"
echo "Consul (swarm-node-01) IP: ${CONSUL_IP}"
CONSUL_DOCKER_BRIDGE_IP=$(getIfaceIP swarm-node-01 docker0)
echo "Docker Bridge IP on Consul node (swarm-node-01): ${CONSUL_DOCKER_BRIDGE_IP}"
useDockerEnv swarm-node-01
# run consul locally first
docker run -d \
--name consul-agent \
-p ${CONSUL_IP}:8300:8300 \
-p ${CONSUL_IP}:8301:8301 \
-p ${CONSUL_IP}:8301:8301/udp \
-p ${CONSUL_IP}:8302:8302 \
-p ${CONSUL_IP}:8302:8302/udp \
-p ${CONSUL_IP}:8400:8400 \
-p ${CONSUL_IP}:8500:8500 \
-p ${CONSUL_DOCKER_BRIDGE_IP}:53:53 \
progrium/consul -server -bootstrap -advertise $CONSUL_IP -ui-dir /ui
echo
echo
echo
echo Creating master node
echo
echo
echo
if [[ ! $(machineExists swarm-master) ]]
then
#10.0.2.2 is the host ip in virtualbox
docker-machine create -d virtualbox \
--swarm \
--swarm-master \
--swarm-discovery="consul://${CONSUL_IP}:8500" \
--engine-opt="cluster-store=consul://${CONSUL_IP}:8500" \
--engine-opt="cluster-advertise=eth1:2376" \
swarm-master
fi
eval "$(docker-machine env --swarm swarm-master)"
masterIp=$(getIfaceIP swarm-master eth1)
echo "Master IP: ${masterIp}"
masterDockerIp=$(getIfaceIP swarm-master docker0)
echo "Docker Bridge IP on Master: ${masterDockerIp}"
for node in 02
do
echo
echo
echo Creating Node $node
echo
echo
if [[ ! $(machineExists "swarm-node-$node") ]]
then
docker-machine create -d virtualbox \
--swarm \
--swarm-discovery="consul://${CONSUL_IP}:8500" \
--engine-opt="cluster-store=consul://${CONSUL_IP}:8500" \
--engine-opt="cluster-advertise=eth1:2376" \
swarm-node-${node}
fi
nodeIp=$(getIfaceIP swarm-node-$node eth0)
echo "Node ${node} IP: ${nodeIp}"
nodeDockerIp=$(getIfaceIP swarm-node-$node docker0)
echo "Docker Bridge IP on Node ${node}: ${nodeDockerIp}"
done
eval "$(docker-machine env swarm-master)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment