Skip to content

Instantly share code, notes, and snippets.

@timusg
Last active October 20, 2016 21:01
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save timusg/8251999 to your computer and use it in GitHub Desktop.
Save timusg/8251999 to your computer and use it in GitHub Desktop.
keepalived notification script for switch elastic ip, helpful to setup highly available haproxy in ec2/VPC
#!/bin/bash
export EC2_HOME=/usr/local/share/ec2_tools/
export JAVA_HOME=/usr/lib/jvm/java
export PATH=$PATH:$EC2_HOME/bin
INSTANCEID=`curl -s http://169.254.169.254/latest/meta-data/instance-id`
case "$1" in
MASTER)
logger triggering notify master event
ADDRESS=`ec2-describe-addresses --aws-secret-key '<%= @secret_key%>' --aws-access-key '<%= @access_key%>' <%= @eip%>`
ASSOCIATED_INSTANCEID=`echo $ADDRESS | awk '{print $3}'`
ASSOCIATION_ID=`echo $ADDRESS | awk '{print $6}'`
ALLOCATION_ID=`echo $ADDRESS | awk '{print $5}'`
if [ ! "$INSTANCEID" == "$ASSOCIATED_INSTANCEID" ]; then
ec2-disassociate-address --aws-secret-key '<%= @secret_key%>' --aws-access-key '<%= @access_key%>' -a $ASSOCIATION_ID 2>&1
logger -p local0.warn associating EIP to instance: $INSTANCEID
ec2-associate-address --aws-secret-key '<%= @secret_key%>' --aws-access-key '<%= @access_key%>' -i $INSTANCEID -a $ALLOCATION_ID 2>&1
fi
;;
FAULT)
logger restarting keepalived
service keepalived restart
;;
*)
echo $"Usage: $0 {MASTER|FAULT}"
exit 2
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment