keepalived

Quick HA with Keepalived + Haproxy on Linux Debian

This is a draft note, please use it at your own risk and discernment, could become a post or a mini howto if I get enough feedback on it.

Scenario:
2 servers running keepalived, haproxy
2 servers running elasticsearch

keepalived/haproxy:
server 1: 1.1.1.1
server 2: 1.1.1.2
HA VIP: 1.1.1.3

elasticsearch:
server1: 1.1.1.4
server2: 1.1.1.5

For Debian Squeeze: (for Wheezy probably backports not needed)

In /etc/apt/sources.list.d/squeeze-backports.list:

~# cat /etc/apt/sources.list.d/squeeze-backports.list 
deb http://backports.debian.org/debian-backports squeeze-backports main
# apt-get update
# apt-get install keepalived haproxy

In /etc/keepalived/keepalived.conf:
(put state MASTER and priority 150 in the first server and state BACKUP and priority 100 in the second)

# Settings for otifications                                                                                       
global_defs {
    notification_email {
    recipent@domain.com    # Email address for notifications                                                          
    }
    notification_email_from root@my.server.name   # The from address for the notifications              
    smtp_server 127.0.0.1     # You can specifiy your own smtp server here                                           
    smtp_connect_timeout 15
}
# Define the script used to check if service is working                                                        
vrrp_script chk_service {
    script "ps auxw|grep elasticsearch 2>&1 > /dev/null"
    interval 2
    weight 2
}

vrrp_instance my_hacluster {
  interface eth0 
  state MASTER
  virtual_router_id 11
  priority 150 
  garp_master_delay 5
  nopreempt
  virtual_ipaddress {
   1.1.1.3/32 dev eth0
  } 
    notify_master "/etc/init.d/haproxy start"
    notify_backup "/etc/init.d/haproxy stop"

 authentication {
        auth_type AH
	auth_pass yoursecretpasswordhereXYZ
                                          
    }


# check if we should fail over                                                             
    track_script {
	chk_service
    }
}

Haproxy should not be running at boot but it’s brought up by keepalived when needed.

In /etc/haproxy/haproxy.cfg:
(same config for both servers)

global
  log /dev/log    local1
  maxconn 4096
  user haproxy
  group haproxy
  daemon
  quiet
  stats socket /var/run/haproxy.sock
  # debug

defaults
  log     global
  option  dontlognull
  retries 3
  option redispatch
  option httpclose
  maxconn 4000
  contimeout      50000
  clitimeout      50000
  srvtimeout      120000 
listen elasticsearch
  bind 1.1.1.3:9200
  balance leastconn
  mode http
  stats enable
  stats realm Traffic\ Statistics
  stats auth stats:yourpasswordxyz
  stats scope .
  stats uri /haadmin?stats
    server 1.1.1.4:9200 1.1.1.4:9200 check inter 2000 fall 7
    server 1.1.1.5:9200 1.1.1.5:9200 check inter 2000 fall 7