keepalived安装与配置
目录
keepalived安装与配置... 1
安装:... 1
基本的主备配置... 2
主服务器:... 2
备服务器:... 3
主备重启服务:... 4
查看虚拟IP:... 5
备注:vrrp_script和track_script. 5
备注:notify_[stop/master/backup/fault]6
备注: nopreempt. 6
验证测试:... 6
主服务器停止keepalived :... 6
备份服务器--虚拟IP切换:... 7
软件卸载... 7
版本历史
时间 |
版本 |
说明 |
编写者 |
2015-1-9 |
1.1 |
keepalived安装与配置 |
csc |
系统环境: centos-5.8 64bit
keepalived: keepalived-1.1.15.tar.gz
安装:
tar -xvzfkeepalived-1.1.15.tar.gz
cd keepalived-1.1.15
./configure
make
make install
mkdir /etc/keepalived
cp /usr/local/etc/rc.d/init.d/keepalived/etc/rc.d/init.d/
cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
cp /usr/local/etc/keepalived/keepalived.conf/etc/keepalived/
cp /usr/local/sbin/keepalived /usr/sbin/
/etc/init.d/keepalived start
ps -ef|grep keepalived
基本的主备配置
主服务器:
more /etc/keepalived/keepalived.conf
! ConfigurationFile for keepalived
#global_defs {
# notification_email {
# }
# smtp_server 192.168.200.1
# smtp_connect_timeout 30
# router_id LVS_DEVEL
#}
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 {
10.8.10.130 #虚拟IP,可以有多个
#10.8.10.131
}
}
备服务器:
[root@P-client01keepalived]# more keepalived.conf
! ConfigurationFile for keepalived
#global_defs {
# notification_email {
# }
# smtp_server 192.168.200.1
# smtp_connect_timeout 30
# router_id LVS_DEVEL
#}
vrrp_instance VI_1 {
state BACkUP
interface eth0
virtual_router_id 51 #主备保持一致
priority 90 #优先级 主比备高
advert_int 1 #心跳广播时间间隔 秒
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.8.10.130 #虚拟IP,可以有多个
#10.8.10.131
}
}
主备重启服务:
/etc/init.d/keepalived restart
查看虚拟IP:
[root@local-115keepalived]# ip a
1: lo:
link/loopback 00:00:00:00:00:00 brd00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
2: eth0:
link/ether 00:16:0a:08:0a:79 brdff:ff:ff:ff:ff:ff
inet 10.8.10.115/24 brd 10.8.10.255 scopeglobal eth0
inet 10.8.10.130/32 scope global eth0
备注:vrrp_script和track_script
vrrp_script代码块是用来定义监控脚本,脚本执行时间间隔以及脚本的执行结果导致优先级变更幅度的。
vrrp_scriptchk_redis {
script "/etc/keepalived/scripts/redis_check.sh" #指定执行脚本的路径
interval 1 #指定脚本的执行时间间隔
weight 10 #脚本结果导致的优先级变更:10表示优先级+10;-10则表示优先级-10
}
定义好vrrp_script代码块之后,就可以在instance中使用了
track_script{
chk_redis
}
注意:VRRP脚本(vrrp_script)和VRRP实例(vrrp_instance)属于同一个级别
备注:notify_[stop/master/backup/fault]
notify_stop keepalived停止运行前运行notify_stop指定的脚本。
配合官方文档提到的以下三个参数一起使用,功能更强大:
notify_master keepalived切换到master时执行的脚本
notify_backup keepalived切换到backup时执行的脚本
notify_fault keepalived出现故障时执行的脚本
备注: nopreempt
当master down了,backup接管了,master再次起来,不能再成为master。否则master恢复了再接管的话,会造成业务来回切换,这时候就需要nopreempt参数了。
nopreempt:设置不抢占,这里只能设置在state为backup的节点上,而且这个节点的优先级必须别另外的高。
以上备注 -参数联合使用方法,可以参考<
验证测试:
主服务器停止keepalived :
/etc/init.d/keepalived restart
[root@local-115keepalived]# ip a
1: lo:
link/loopback 00:00:00:00:00:00 brd00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
2: eth0:
link/ether 00:16:0a:08:0a:79 brdff:ff:ff:ff:ff:ff
inet 10.8.10.115/24 brd 10.8.10.255 scopeglobal eth0
备份服务器--虚拟IP切换:
[root@P-client01keepalived]# ip a
1: lo:
link/loopback 00:00:00:00:00:00 brd00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
2: eth0:
link/ether 00:fe:c0:a8:0a:7f brdff:ff:ff:ff:ff:ff
inet 10.8.10.128/24 brd 10.8.10.255 scopeglobal eth0
inet 10.8.10.130/32 scope global eth0
当主服务器启动keepalived时候,虚拟IP 重新出现在主服务器
软件卸载
直接make uninstall,然后
make clean