Last active
February 23, 2016 04:19
-
-
Save jkordish/bf9f0fd3b598750867be to your computer and use it in GitHub Desktop.
Make shift HAproxy HA Monitor Upstart for Ubuntu 12.04 and 14.04
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# haproxy HA monitor | |
description "monitor the haproxy EIP for HA" | |
start on runlevel [2345] | |
stop on runlevel [!2345] | |
console log | |
limit nofile 65535 65535 | |
respawn | |
# the haproxy instances will require the following IAM policy | |
# { | |
# "Statement": [ | |
# { | |
# "Action": [ | |
# "ec2:AssociateAddress", | |
# "ec2:DisassociateAddress", | |
# ], | |
# "Effect": "Allow", | |
# "Resource": "*" | |
# } | |
# ] | |
# } | |
pre-start script | |
# wait for consul to come available | |
until [ -f "/var/consul/run/consul.pid" ]; do | |
echo "$(date "+%Y/%m/%d %H:%M:%S") [INFO] waiting on consul" | |
/bin/sleep 5 | |
done | |
if [ `echo -n "$(/usr/bin/ec2metadata --public-ip)"` != "unavailable" ]; then | |
echo "$(date "+%Y/%m/%d %H:%M:%S") [INFO] Setting shared EIP" | |
/usr/local/bin/consulate kv set "haproxy/EIP" $(/usr/bin/ec2metadata --public-ip) | |
fi | |
end script | |
script | |
# since we need a proxy to use the aws cli here | |
. /etc/profile.d/proxy | |
# a makeshift creds profile that provides the key | |
# look at this for an example: https://gist.github.com/jkordish/4392db9993f99f65dc79 | |
. /etc/profile.d/creds | |
# Grab the EIP for the carnifex service from consul. | |
export EIP=$(/usr/local/bin/consulate kv get "haproxy/EIP") | |
echo "$(date "+%Y/%m/%d %H:%M:%S") [INFO] Starting HA monitor for ${ID}" | |
# Enter loop... | |
while true; do | |
# Only evaluate if we currently do not have the EIP assigned. | |
if [ `echo -n "$(/usr/bin/ec2metadata --public-ip)"` != "${EIP}" ]; then | |
# evaulate if our ping returns nothing. If so reassign! | |
export MASTER=`dig +short -p 8600 @localhost haproxy.service.consul | grep -v $(ec2metadata --local-ipv4)` | |
if [ -z `echo -n "$(ping -c 3 -W 1 ${MASTER} | awk '/time=/ {count++} END{ print count}')"` ]; then | |
echo "$(date "+%Y/%m/%d %H:%M:%S") [INFO] HA heartbeat failed, taking over ${EIP} from ${MASTER}" | |
/usr/local/bin/aws ec2 associate-address \ | |
--allow-reassociation \ | |
--instance-id ${INSTANCE_ID} \ | |
--public-ip ${EIP} | |
if [ `echo -n "$(/usr/bin/ec2metadata --public-ip)"` != "${EIP}" ]; then | |
echo "$(date "+%Y/%m/%d %H:%M:%S") [INFO] successfully reassigned EIP: ${EIP}" | |
fi | |
sleep 30 | |
fi | |
fi | |
sleep 2 | |
done | |
end script |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment