Skip to content

Instantly share code, notes, and snippets.

@cizixs
Last active August 16, 2024 15:50
Show Gist options
  • Save cizixs/eee61a0ae65c6be30b74b80a9753efb3 to your computer and use it in GitHub Desktop.
Save cizixs/eee61a0ae65c6be30b74b80a9753efb3 to your computer and use it in GitHub Desktop.
Create swarm cluster in one command.
#!/bin/bash
# This script helps to create a develop docker swarm cluster in one command
# For explanations and details, please refer to https://docs.docker.com/engine/userguide/networking/get-started-overlay/
set -e
create_kv() {
echo Creating kvstore machine.
docker-machine create -d virtualbox \
kvstore
docker $(docker-machine config kvstore) run -d \
-p "8500:8500" \
progrium/consul --server -bootstrap-expect 1
}
create_master() {
echo Creating cluster master
kvip=$(docker-machine ip kvstore)
docker-machine create -d virtualbox \
--swarm --swarm-master \
--swarm-discovery="consul://${kvip}:8500" \
--engine-opt="cluster-store=consul://${kvip}:8500" \
--engine-opt="cluster-advertise=eth1:2376" \
swarm-manager
}
create_nodes(){
kvip=$(docker-machine ip kvstore)
echo Creating cluster nodes
for i in 1 2; do
docker-machine create -d virtualbox \
--swarm \
--swarm-discovery="consul://${kvip}:8500" \
--engine-opt="cluster-store=consul://${kvip}:8500" \
--engine-opt="cluster-advertise=eth1:2376" \
swarm-node${i}
done
}
create_overlay(){
docker $(docker-machine config swarm-manager) network create \
--driver overlay \
--subnet=10.0.9.0/24 \
multi
}
teardown(){
docker-machine rm kvstore -y
docker-machine rm -y swarm-manager
for i in 1 2; do
docker-machine rm -y swarm-node${i}
done
}
case $1 in
up)
create_kv
create_master
create_nodes
;;
down)
teardown
;;
*)
echo "Unknow command..."
exit 1
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment