HAProxy/Keepalived vrrp set up in AWS EC2 with EIP (Basic EC2)

amazon ec2amazon-web-servicescentos6.5haproxykeepalived

I am a newbie to CentOS/Linux/HAProxy/Keepalived, so I have had a lot to learn over the past few days!

I have 2 CentOS 6.5 servers in Amazon AWS running on their own EC2 Micro instances. I also have 1 Elastic IP set up. Each instance has the following installed:

  • Amazon EC2 API Tools
  • HAProxy
  • Keepalived
  • Apache / httpd
  • Java OpenJDK 1.6

I have got HAProxy installed and configured on both instances. For testing purposes I installed Apache to serve a basic index.html page so I could test that HAProxy was carrying out the LB task and round robin is working fine.

What I am trying to achieve, is using Keepalived to monitor the haproxy process on LB1 (Has EIP assigned), if it fails or stops for some reason, it calls a script which uses the AWS EC2 API Tools to reassign an Elastic IP to the second Loadbalancer to take over..

I have googled and looked at the documentation to create a script that achieves this, however, when I test it and the HAProxy service is stopped, it does not run the script..

LB1 – Contents of keepalived.conf

vrrp_script chk_haproxy {
script "killall -0 haproxy"
interval 2
weight 2
}

vrrp_instance VI_1 {
interface eth0
state MASTER
virtual_router_id 51
priority 101
vrrp_unicast_bind 10.xx.xx.01 # The IP of the host this file is stored on
vrrp_unicast_peer 10.xx.xx.23 # The IP of the other host
advert_int 1
track_script {
chk_haproxy
}
notify_fault /etc/keepalived/vrrp.sh
}

Output from Tailing the log

Jun 18 12:03:11 ip-10-xx-xx-xx Keepalived_vrrp[25199]: Opening file '/etc/keepalived/keepalived.conf'.
Jun 18 12:03:11 ip-10-xx-xx-xx Keepalived_vrrp[25199]: Configuration is using : 60618 Bytes
Jun 18 12:03:11 ip-10-xx-xx-xx Keepalived_vrrp[25199]: Using LinkWatch kernel netlink reflector...
Jun 18 12:03:11 ip-10-xx-xx-xx Keepalived_vrrp[25199]: VRRP sockpool: [ifindex(2), proto(112), fd(10,11)]
Jun 18 12:03:11 ip-10-xx-xx-xx Keepalived_vrrp[25199]: VRRP_Script(chk_haproxy) succeeded
Jun 18 12:03:12 ip-10-xx-xx-xx Keepalived_vrrp[25199]: VRRP_Instance(VI_1) Transition to MASTER STATE
Jun 18 12:03:13 ip-10-xx-xx-xx Keepalived_vrrp[25199]: VRRP_Instance(VI_1) Entering MASTER STATE
Jun 18 12:03:19 ip-10-xx-xx-xx Keepalived_vrrp[25199]: VRRP_Script(chk_haproxy) failed

My first impression is that it has detected the haproxy service failure, but it doesnt seem to execute the vrrp.sh script. (contents below)

#vrrp.sh
#!/bin/bash
cd $EC2_BASE/tools/bin
#DisAssociate EIP from this instance.
./ec2-disassociate-address  54.xx.xx.xx
#Mapping EIP to secondary server
./ec2-associate-address 54.xx.xx.xx -i [instance-id]

If I run the vrrp.sh script by itself on the server, it runs the commands and successfully re-assigns the EIP. Just not when it is called from the keepalived.conf script.

Any ideas!? I've been scratching my head all day and the laptop is about to be launched out the window..

Best Answer

The notify_fault script is invoked when keepalived enters the fault state.

You want to configure your script using

notify_master /etc/keepalived/vrrp.sh