Skip to content

Instantly share code, notes, and snippets.

@magiconair
Last active June 5, 2017 21:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save magiconair/7e32b5982a3f58f78381ad017bd7890a to your computer and use it in GitHub Desktop.
Save magiconair/7e32b5982a3f58f78381ad017bd7890a to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# Script for bringing up an N node consul cluster
# on the local machine on different ports.
#
# The first node is listening on the default ports
# so that the command line tool works.
#
# Examples:
#
# 3-node cluster:
#
# $ consul-cluster.bash
# $ consul-cluster.bash 3
#
# 5-node cluster with specific consul version:
#
# $ consul-cluster.bash 5 ~/consul-0.7.5/consul
config() {
local port=${1:-0}
local name="consul${port}"
local nodeid=$(printf "00000000-0000-0000-0000-%012d" $port)
cat << EOF > ${name}/a.json
{
"server" : true,
"node_id" : "${nodeid}",
"node_name" : "${name}",
"data_dir" : "${name}/data",
"pid_file" : "${name}/pid",
"bind_addr" : "127.0.0.1",
"retry_join" : ["127.0.0.1:8301","127.0.0.1:8304","127.0.0.1:8307"],
"bootstrap_expect" : 3,
"ports" : {
"http" : $((8500 + $port)),
"dns" : $((8600 + $port)),
"server" : $((8300 + 3*$port)),
"serf_lan" : $((8301 + 3*$port)),
"serf_wan" : $((8302 + 3*$port)),
"rpc" : $((8400 + $port))
}
}
EOF
}
trap stopall EXIT TERM KILL
jobs=
stopall() {
[ "$jobs" == "" ] || kill $jobs
}
run() {
local port=$1
local name=consul${port}
rm -rf ${name}
mkdir ${name}
config $port
$CONSUL agent -config-dir ${name} 2>&1 | tee ${name}/log &
jobs="$jobs $!"
}
N=${1:-2}
CONSUL=${2:-consul}
for ((i=0 ; i < N ; i++)) ; do run $i ; done
wait
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment