一、安装keepalived 1.2.2
yum -y install gcc make openssl openssl-devel wget kernel-devel
mkdir -p /usr/local/src/hasoft
cd /usr/local/src/hasoft
wget http://www.keepalived.org/software/keepalived-1.2.2.tar.gz
tar -zxvf keepalived-1.2.2.tar.gz
cd keepalived-1.2.2
./configure --prefix=/usr/local/keepalived --with-kernel-dir=/usr/src/kernels/2.6.18-238.19.1.el5-i686/
make && make install
二、整理管理文件
cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
三、服务设置开机启动
chkconfig keepalived on
或者
echo "/usr/local/keepalived/sbin/keepalived" >> /etc/rc.local
四、配置
1. 主-备模式
节点A 192.168.0.11 (主节点),节点B 192.168.0.12(备用节点),虚拟IP(对外提供服务的IP 192.168.0.200,192.168.0.201)
节点A上的配置文件/usr/local/keepalived/etc/keepalived/keepalived.conf:
global_defs {
notification_email { #通知邮件列表
root@localhost
}
notification_email_from root@local.host #发送通知邮件
smtp_server localhost
smtp_connect_timeout 30
router_id NodeA #路由ID,唯一标识本节点
}
vrrp_instance VI_1 {
state MASTER #指定A节点为主节点 备用节点上设置为BACKUP即可
interface eth0 #绑定虚拟IP的网络接口
virtual_router_id 51 #VRRP组名,两个节点的设置必须一样,以指明各个节点属于同一VRRP组
priority 100 #主节点的优先级(1-254之间),备用节点必须比主节点优先级低
advert_int 1 #组播信息发送间隔,两个节点设置必须一样
authentication { #设置验证信息,两个节点必须一致
auth_type PASS
auth_pass 1111
}
virtual_ipaddress { #指定虚拟IP, 两个节点设置必须一样
192.168.0.200
192.168.0.201
}
}
节点B上的配置文件/usr/local/keepalived/etc/keepalived/keepalived.conf:
global_defs {
notification_email { #通知邮件列表
root@localhost
}
notification_email_from root@local.host #发送通知邮件
smtp_server localhost
smtp_connect_timeout 30
router_id NodeB #路由ID,唯一标识本节点
}
vrrp_instance VI_1 {
state BACKUP #指定B节点为备用节点
interface eth0 #绑定虚拟IP的网络接口
virtual_router_id 51 #VRRP组名,两个节点的设置必须一样,以指明各个节点属于同一VRRP组
priority 99 #备用节点的优先级(1-254之间),备用节点必须比主节点优先级低
advert_int 1 #组播信息发送间隔,两个节点设置必须一样
authentication { #设置验证信息,两个节点必须一致
auth_type PASS
auth_pass 1111
}
virtual_ipaddress { #指定虚拟IP, 两个节点设置必须一样
192.168.0.200
192.168.0.201
}
}
2. 双主模式
双主模式允许两台机器均处于工作状态并互相作为备份。
搭建keepalived双主模式的要素:
1. 必须有两个虚拟IP, 分别绑定至两个节点上
2. 每个节点作为某个虚拟IP的主节点,并同时作为另外一个虚拟IP的备用节点。
3. 当某个节点产生故障时,两个虚拟IP自动绑定至正常节点上
也就是说,两个节点的配置应该是交叉的,对同个虚拟IP,交叉互为主备。
节点A上的配置文件/usr/local/keepalived/etc/keepalived/keepalived.conf:
global_defs {
notification_email {
root@localhost
}
notification_email_from root@local.host
smtp_server localhost
smtp_connect_timeout 30
router_id NodeA
}
vrrp_instance VI_1 { #定义路由id=51的主(VIP=192.168.0.200)
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.0.200
}
}
vrrp_instance VI_2 { #定义路由id=52的备(VIP=192.168.0.201)
state BACKUP
interface eth0
virtual_router_id 52
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.0.201
}
}
节点B上的配置文件/usr/local/keepalived/etc/keepalived/keepalived.conf:
global_defs {
notification_email {
root@localhost
}
notification_email_from root@local.host
smtp_server localhost
smtp_connect_timeout 30
router_id NodeB
}
vrrp_instance VI_1 { #定义路由id=52的主(VIP=192.168.0.201)
state MASTER
interface eth0
virtual_router_id 52
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.0.201
}
}
vrrp_instance VI_2 { #定义路由id=51的备(VIP=192.168.0.200)
state BACKUP
interface eth0
virtual_router_id 51
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.0.200
}
}
五、启动keepalived服务
service keepalived start
或者
/etc/init.d/keepalived start
或者
/usr/local/keepalived/sbin/keepalived
#需要分别启动NodeA,NodeB上的keepalived服务
六、验证服务
1. 运行命令ip addr|a|address查看eth0上的ip
2. 停止NodeA或NodeB, 检查ip是否能够成功漂移到正常的节点