本文写于 2015 年。
Keepalived 是在传统网络中,常用的网络容灾方案。

CentOS 6.5 64bit

Keepalived v1.2.19

安装

wget http://www.keepalived.org/software/keepalived-1.2.19.tar.gz

tar -zxvf keepalived-1.2.19.tar.gz

sudo yum install -y openssl openssl-devel

./configure --prefix=/work/admin/keepalived
make
make install

配置

vim /work/admin/keepalived/etc/keepalived/keepalived.conf

主机

! Configuration File for keepalived

global_defs {
notification_email {
mail@server.com
}
notification_email_from mail@server.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id AGENT
}

vrrp_script chk_haproxy {
script "nc -w 0 192.168.80.194 8309"
interval 3
weight -20
fall 3
rise 2
}

vrrp_instance VI_196 {
state MASTER
interface eth0
virtual_router_id 196
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
192.168.80.196
}

track_script {
chk_haproxy
}
}

备机

! Configuration File for keepalived

global_defs {
notification_email {
mail@server.com
}
notification_email_from mail@server.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id AGENT
}

vrrp_script chk_haproxy {
script "nc -w 0 192.168.80.195 8309"
interval 3
weight -20
fall 3
rise 2
}


vrrp_instance VI_196 {
state BACKUP
interface eth0
virtual_router_id 196
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
192.168.80.196
}

track_script {
chk_haproxy
}

}

防火墙

防火墙开放vrrp协议:
vim /etc/sysconfig/iptables
新增一行:-A INPUT -p vrrp -j ACCEPT
保存后,重启iptables

启动

sudo /work/admin/keepalived/sbin/keepalived -f /work/admin/keepalived/etc/keepalived/keepalived.conf

检查keepalived运行状况:
vim /var/log/messages