Skip to content

Instantly share code, notes, and snippets.

@neilellis
Last active August 29, 2015 14:06
Show Gist options
  • Save neilellis/13e341b2d0242f1f89eb to your computer and use it in GitHub Desktop.
Save neilellis/13e341b2d0242f1f89eb to your computer and use it in GitHub Desktop.
Create a subnet for this host to use in zettio/weave
#!/bin/bash -eu
#apt-get install -y jq
host_ip=$1
#Check to see if we have a cached session for this machine and if it is still valid
if [ -f ~/.consul_subnet_session ] && [[ -n $(< ~/.consul_subnet_session) ]] && [[ $(curl "http://localhost:8500/v1/session/info/$(< ~/.consul_subnet_session)") != null ]]
then
session=$(< ~/.consul_subnet_session)
else
session=$(curl -q -X PUT http://localhost:8500/v1/session/create | jq -r .ID)
fi
#Save the session for next time
echo $session > ~/.consul_subnet_session
for i in $(seq 10 250)
do
for j in $(seq 10 250)
do
ip=10.${i}.${j}
#curl -q -X PUT -d $host_ip localhost:8500/v1/kv/subnets/${ip}?release=$session || :
if curl -q -X PUT -d $host_ip http://localhost:8500/v1/kv/subnets/${ip}?acquire=$session | grep true
then
echo $ip
exit 0
fi
done
done
#!/bin/bash -eux
#Example usage of above
subnet=$(obtain_subnet_address.sh $(hostname -i))
members=$(serf members -tag "weave_subnet=10.*" | tr -s " " | cut "-d " -f4 | tr ',' '\n' | grep weave_subnet | cut -d= -f 2 | tr '\n' ' ')
weave launch ${subnet}.1/8 ${members}
serf tags -set weave_subnet=${subnet}
@neilellis
Copy link
Author

This script uses consul to choose a subnet for this machine that can then be passed to zettio/weave (or similar applications).

The subnet will be 10.x.y and the network should be 10.0.0.0/8

This allows for about 50,000 machines and about 240 ips per machine, to be distributed to local applications/containers/vms.

Simply install consul then pass this machines ip address or mac address as an information parameter.

 ./obtain_subnet_address.sh 123.56.12.178

The script relies on the fact that sessions are invalidated when a machine goes down, for exact details see the consul docs on sessions to understand the exact criteria.

Off you go, feedback or suggestions most welcome as I am very new to consul.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment