High Availability LAMP Stacks with Keepalived (Part 2)

Written by breandan on 4/14/2011 Categories: Development, Home Network, Technology, Work

Now that you have keepalived up, and you are able to fail the IP addresses over between the hosts, you will need to address another crucial question: under what conditions should keepalived fail the IPs to the second host? For us it was pretty simple: if apache and mysql are both running, the node is healthy. If you have a shell script you’ve written that can determine node health, that’s great too. Just pay attention to the exit code you set, and you should be good to go.

The setup is done with the vrrp_script directive, where you tell keepalived what scripts to run, and what the outcome of those scripts is going to be on the priority level of each VIP. Two things to note: you need at least keepalived version 1.1.13 for vrrp_script to work, and you need to list the scripts before the vrrp_instance they are called in. Again, if you use my example password and the IP address listed here, you deserve any punishment you get.

Here is my example configuration file, after the additions of the scripts:


vrrp_script chk_httpd {
 script "killall -0 httpd"
 interval 2
 weight 50
}


vrrp_script chk_mysqld {
 script "killall -0 mysqld"
 interval 2
 weight 50
}


vrrp_instance VI_1 {
 state MASTER
 interface eth0
 virtual_router_id 1
 priority 101
 advert_int 1
 authentication {
  auth_type PASS
  auth_pass 319e49e4-88c2-4f83-a0e0-c0f332f6427c
 }


 virtual_ipaddress {
  1.2.3.4
 }
 track_script {
  chk_httpd
  chk_mysqld
 }
}

The script directive should be pretty obvious: it’s the command you are going to execute to get a return status. (I’m using killall -0, as it’s an exceptionally cheap way to determine if a process with a given name exists at least once in the process table.) The interval is the time, in seconds, between checks, and the weight is the number of points of priority are removed when the check fails. So, as long as your SLAVE config is set to a number higher than 51, when either mysql or httpd aren’t running, the VIP should fail over.

Comments Off