Skip to content

Instantly share code, notes, and snippets.

@twang2218
Last active June 14, 2020 03:18
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save twang2218/def4097648deac398a949b58e2a31610 to your computer and use it in GitHub Desktop.
Save twang2218/def4097648deac398a949b58e2a31610 to your computer and use it in GitHub Desktop.
Docker Multi-host Example
#!/bin/bash
#set -xe
SIZE=3
# or 'digitalocean'
DM_DRIVER=virtualbox
function usage {
NAME=$1
echo "Usage: ${NAME} {create|destroy}"
}
function create_store {
NAME=$1
docker-machine create -d ${DM_DRIVER} ${NAME}
eval $(docker-machine env ${NAME})
HostIP=$(docker-machine ip ${NAME})
export KVSTORE="etcd://${HostIP}:2379"
docker run -d \
-p 4001:4001 -p 2380:2380 -p 2379:2379 \
--restart=always \
--name etcd \
quay.io/coreos/etcd:v2.3.7 \
--initial-advertise-peer-urls http://${HostIP}:2380 \
--initial-cluster default=http://${HostIP}:2380 \
--advertise-client-urls http://${HostIP}:2379,http://${HostIP}:4001 \
--listen-client-urls http://0.0.0.0:2379,http://0.0.0.0:4001 \
--listen-peer-urls http://0.0.0.0:2380
}
function create_master {
NAME=$1
echo "kvstore is ${KVSTORE}"
# eth1 on virtualbox, eth0 on digitalocean
docker-machine create -d ${DM_DRIVER} \
--swarm \
--swarm-discovery=${KVSTORE} \
--swarm-master \
--engine-opt="cluster-store=${KVSTORE}" \
--engine-opt="cluster-advertise=eth1:2376" \
${NAME}
}
function create_node {
NAME=$1
echo "kvstore is ${KVSTORE}"
# eth1 on virtualbox, eth0 on digitalocean
docker-machine create -d ${DM_DRIVER} \
--swarm \
--swarm-discovery=${KVSTORE} \
--engine-opt="cluster-store=${KVSTORE}" \
--engine-opt="cluster-advertise=eth1:2376" \
${NAME}
}
function create {
create_store kvstore
create_master master
for i in $(seq ${SIZE})
do
create_node node${i} &
done
wait
}
function destroy {
for i in $(seq ${SIZE})
do
docker-machine rm -y node${i} || true
done
docker-machine rm -y master || true
docker-machine rm -y kvstore || true
}
SUBCMD=$1
case "${SUBCMD}" in
create) create ;;
destroy) destroy ;;
*) usage $0 && exit 1
esac
#!/bin/bash
#set -xe
SIZE=3
# or 'digitalocean'
DM_DRIVER=virtualbox
function usage {
NAME=$1
echo "Usage: ${NAME} {create|destroy}"
}
function create_store {
NAME=$1
docker-machine create -d ${DM_DRIVER} ${NAME}
eval $(docker-machine env ${NAME})
HostIP=$(docker-machine ip ${NAME})
export KVSTORE="etcd://${HostIP}:2379"
docker run -d \
-p 4001:4001 -p 2380:2380 -p 2379:2379 \
--restart=always \
--name etcd \
quay.io/coreos/etcd:v2.3.7 \
--initial-advertise-peer-urls http://${HostIP}:2380 \
--initial-cluster default=http://${HostIP}:2380 \
--advertise-client-urls http://${HostIP}:2379,http://${HostIP}:4001 \
--listen-client-urls http://0.0.0.0:2379,http://0.0.0.0:4001 \
--listen-peer-urls http://0.0.0.0:2380
}
function create_node {
NAME=$1
echo "kvstore is ${KVSTORE}"
# eth1 on virtualbox, eth0 on digitalocean
docker-machine create -d ${DM_DRIVER} \
--engine-opt="cluster-store=${KVSTORE}" \
--engine-opt="cluster-advertise=eth1:2376" \
${NAME}
}
function create {
create_store kvstore
for i in $(seq ${SIZE})
do
create_node node${i} &
done
wait
}
function destroy {
for i in $(seq ${SIZE})
do
docker-machine rm -y node${i} || true
done
docker-machine rm -y kvstore || true
}
SUBCMD=$1
case "${SUBCMD}" in
create) create ;;
destroy) destroy ;;
*) usage $0 && exit 1
esac
#!/bin/bash
function swarm {
eval $(docker-machine env --swarm master)
set -xe
docker network create -d overlay mynet
docker run -itd --net=mynet --name bb1 busybox
docker run -itd --net=mynet --name bb2 busybox
docker run -itd --net=mynet --name bb3 busybox
}
function nonswarm {
eval $(docker-machine env node1)
set -xe
docker network create -d overlay mynet
docker run -itd --net=mynet --name bb1 busybox
set +xe
eval $(docker-machine env node2)
set -xe
docker run -itd --net=mynet --name bb2 busybox
set +xe
eval $(docker-machine env node3)
set -xe
docker run -itd --net=mynet --name bb3 busybox
set +xe
}
function usage {
echo "Usage: $1 {swarm|nonswarm}"
}
case "$1" in
swarm) swarm ;;
nonswarm) nonswarm ;;
*) usage ;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment