Keepalived会主动检测web服务器,把有故障的服务器从系统中剔除,在服务器修复以后会重新加入到服务器群众,不影响服务器的正常工作
VRRP:虚拟路由冗余协议 它把一个虚拟路由器的责任动态的分配到局域网中的VRRP中的一台。控制虚拟路由IP地址的VRRP路由器称为主路由器,负责转发数据到虚拟IP地址,一旦主路由出现故障,就把数据包转发到备用的路由器上,虚拟IP地址在路由之间是共享的,其中一个指定为主路由,其余的作为备份路由,如果主路由出现故障,这个虚拟IP地址就会映射到一个备用路由上,这个备份路由就成为主路由
VRRP:能够动态的将一个虚拟路由器的责任指定至同一个VRRP组中的其它路由器上,消除静态路由配置的单点故障,同时切换路由的过程会比较块,主要是在主路由出现故障时,启用备用路由
配置keepalived为实现haproxy高可用的双主模型配置文件示例:
安装keepalived,先配置好两个高可用节点
在两个节点上安装keeoalived
在keepalived中指定vrrp
用不上的话禁用smnp
下载keepalived-1.2.7-5.el5.i386
yum -y install --nogpgcheck localinstall keepalived-1.2.7-5.el5.i386
Scp keepalived-1.2.7-5.el5.i386.rpm node2:/root/复制到另外一个节点
在另外一个节点上再执行yum -y install --nogpgcheck localinstall keepalived-1.2.7-5.el5.i386
安装keepalived
cd keepalived keepalived的配置文件keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
}
notification_email_from [email protected]
smtp_connect_timeout 3
smtp_server 127.0.0.1
router_id LVS_DEVEL
}
vrrp_script chk_haproxy {
script "killall -0 haproxy"
interval 1
weight 2
}
vrrp_script chk_mantaince_down {
script "[[ -f /etc/keepalived/down ]] && exit 1 || exit 0"
interval 1
weight 2
}
vrrp_instance VI_1 {
interface eth0
state MASTER # BACKUP for slave routers
priority 101 # 100 for BACKUP
virtual_router_id 51
garp_master_delay 1
authentication {
auth_type PASS
auth_pass password
}
track_interface {
eth0
}
virtual_ipaddress {
172.16.116.7/16 dev eth0 label eth0:0
}
track_script {
chk_haproxy
chk_mantaince_down
}
notify_master "/etc/keepalived/notify.sh master"
notify_backup "/etc/keepalived/notify.sh backup"
notify_fault "/etc/keepalived/notify.sh fault"
}
vrrp_instance VI_2 {
interface eth0
state BACKUP # BACKUP for slave routers
priority 100 # 100 for BACKUP
virtual_router_id 52
garp_master_delay 1
authentication {
auth_type PASS
auth_pass password
}
track_interface {
eth0
}
virtual_ipaddress {
172.16.116.7/16 dev eth0 label eth0:1
}
track_script {
chk_haproxy
chk_mantaince_down
}
notify_master "/etc/keepalived/notify.sh master"
notify_backup "/etc/keepalived/notify.sh backup"
notify_fault "/etc/keepalived/notify.sh fault"
}
notify.sh脚本的简单示例:
#!/bin/bash
# Author: MageEdu <[email protected]>
# description: An example of notify script
#
vip=172.16.116.8
contact='root@localhost'
Notify() {
mailsubject="`hostname` to be $1: $vip floating"
mailbody="`date '+%F %H:%M:%S'`: vrrp transition, `hostname` changed to be $1"
echo $mailbody | mail -s "$mailsubject" $contact
}
case "$1" in
master)
notify master
/etc/rc.d/init.d/haproxy start
exit 0
;;
backup)
notify backup
/etc/rc.d/init.d/haproxy restart
exit 0
;;
fault)
notify fault
exit 0
;;
*)
echo 'Usage: `basename $0` {master|backup|fault}'
exit 1
;;
esac
配置好keepalived以后安装haproxy-1.4.22.tar.gz
tar xf haproxy-1.4.20.tar.gz
cd haproxy-1.4.20
make TARGET=linux26PREFIX=/usr/local/haproxy install
cd /usr/local/haproxy/
mkdir conf logs
cd conf/
vim haproxy.cfg
/usr/local/haproxy/sbin/haproxy-f /usr/local/haproxy/conf/haproxy.cfg
修改完成后,启动keepalived服务
查看node1与node2上的VIP地址配置node1主机是否为172.16.116.7 eth0:0是否为172.16.116.8
用172.16.116.7测试网页,node1的返回结果是node1.magedu.com
用172.16.116.8测试网页,node2的返回结果是node2.magedu.com