Build HA cluster with keepalived
OS: CentOS 5.4
Software:
keepalived, openssl097a, lighttpd
Arch:
————— | –lb_master (192.168.1.101/24) |
Visitors -(VIP) - |
————— | –lb_slave (192.168.1.102/24) |
VIP: 192.168.1.100/24
Install
-
preparation: enable Two important repo
-
wget ftp://rpmfind.net/linux/dag/redhat/el4/en/i386/dag/RPMS/keepalived-1.1.13-5.el4.rf.i386.rpm
# keepalived need old libs libssl.so.4 and libcrypto.so.4,if you use some other OS, maybe no need.
-
yum install -y openssl097a lighttpd
-
rpm -ivh keepalived-1.1.13-5.el4.rf.i386.rpm
-
/etc/init.d/keepalived && chkconfig keepalived on && /etc/init.d/lighttpd && chkconfig lighttpd on
-
check with command $ip addr show eth0
# if the above steps, no error. let’s configure the keepalived.
Configuration
$mv /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.back
$touch /etc/keepalived/keepalived.conf
#edit /etc/keepalived/keepalived.conf
# Master contents:
—begin——content cut here—————–
vrrp_script chk_http_port {
script “/usr/bin/killall -0 lighttpd”
interval 2
weight 2
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 101
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.100/24 dev eth0
}
}
—end——content cut here—————–
# Slave contents:
—begin——content cut here—————–
vrrp_script chk_http_port {
script “/usr/bin/killall -0 lighttpd”
interval 2
weight 2
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.100/24 dev eth0
}
}
—end——content cut here—————–
$/etc/init.d/keepalived restart
$tail -f /var/log/messages
……….
Apr 3 11:34:15 stage Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.1.100
Apr 3 11:34:15 stage Keepalived_vrrp: Netlink reflector reports IP 192.168.1.100 added
Apr 3 11:34:15 stage Keepalived_healthcheckers: Netlink reflector reports IP 192.168.1.100 added
Apr 3 11:34:20 stage Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.1.100
………
Test
#edit the /srv/www/lighttpd/index.htm
#master
$echo “I am master” > /srv/www/lighttpd/index.htm
#slave
$echo “I am slave” > /srv/www/lighttpd/index.htm
# shutdown master
$curl 192.168.1.100
I am slave
# start master
$curl 192.168.1.100
I am master
# some info from messages when master is down
Apr 3 11:36:24 stage Keepalived_vrrp: VRRP_Instance(VI_1) Entering BACKUP STATE
Apr 3 11:36:24 stage Keepalived_vrrp: VRRP_Instance(VI_1) removing protocol VIPs.
Apr 3 11:36:24 stage Keepalived_vrrp: Netlink reflector reports IP 192.168.1.100 removed
Apr 3 11:36:24 stage Keepalived_healthcheckers: Netlink reflector reports IP 192.168.1.100 removed
Extend
RTFM, the keepalived is very complex and good, it has lots of options, this is only one simple tutorial.