so i needed to setup 2 mysql servers in a HA setup on CentOS 6, i decided to use keepalived (yum search keepalive).

here were the requirements:

– 1 virtual ip
– both servers had to still replicate from each other but only one can take traffic from the vip
– quick failover incase machine went down or was pulled

in this setup: = virtual ip address (where all mysql traffic should come to) = mysql server 1 / lb1 = mysql server 2 / lb2

here is my keepalived.conf :

global_defs {
  notification_email {
  notification_email_from loadbalancer1
  smtp_server smtp server
  smtp_connect_timeout 5
  router_id lb1

vrrp_instance mysql_pool {
  interface eth0
  state MASTER
  virtual_router_id 1
  priority 101
  track_interface {

  authentication {
    auth_type PASS
    auth_pass set a password here

  virtual_ipaddress { dev eth0

virtual_server 3306 {
  delay_loop 2
  lb_algo rr
  lb_kind DR
  protocol TCP

  sorry_server 3306

  real_server 3306 {
    weight 10
      connect_port    3306
      connect_timeout 1

  real_server 3306 {
    weight 10
      connect_port    3306
      connect_timeout 1

this was the conf file that was installed on balancer one – make sure you make the right changes for it to work on balancer two like setting state to slave and changing router_id.

you can also add a second TCP_CHECK under the 3306 check with a port that you can keep open with netcat. in that case, you can stop netcat and then mysql would be automagically be pulled out of the balancer while still up (in case you needed to do some work on it), but this would require netcat to remain up and running, while keeping that port open at all times.

for the network configuration, we bound the vip to LO:1 :

lo:1      Link encap:Local Loopback  
          inet addr:  Mask:
          UP LOOPBACK RUNNING  MTU:16436  Metric:1

here is the ifcfg-lo:1 :


once keepalive is up and running, we can check the balancers and pools by using ipvsadm (yum search ipvsadm):

# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP rr
  ->         Local   10     13         8         
  ->         Route   10     8          93

and for mysql replication, you can google that one since your replications needs may be different.

once all of this is configured, you can ensure that mysql is up and running and being seen by keepalived by running ipvsadm -Ln