keepalived是由C语言编写的路由软件,是lvs的扩展项目,主要用作RealServer的健康状态检查以及LoadBalance主机和BackUP主机之间failover的实现。
1、获取安装软件
下载地址: http://www.keepalived.org/download.html
2、安装前提
确保ipvs已经被安装
#如下,执行ipvsadm检查ipvs是否已经安装(当前大多数Linux缺省已安装)
[root@HKBO ~]# ipvsadm
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
#检查当前加载的内核模块,看是否存在ip_vs模块
[root@HKBO ~]# lsmod|grep ip_vs
ip_vs 122113 0
如果没有安装,直接使用yum install ipvsadm
3、安装keepalived
tar -xvf keepalived-1.2.13.tar.gz
cd keepalived-1.2.13
./configure --disable-fwmark --prefix=/usr/local/keepalived
make && make install
安装完后执行如下步骤
mkdir /etc/keepalived/
cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
ln -s /usr/local/keepalived/sbin/keepalived /sbin/
service keepalived restart
4、修改配置文件后重新启动
! Configuration File for keepalived
global_defs {
notification_email {
evanzheng@sohu-inc.com
}
notification_email_from localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id Master
}
vrrp_instance VI_1 {
state BACKUP
interface eth1
virtual_router_id 86
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass 3252
}
virtual_ipaddress {
虚拟ip
}
}
}
5、配置文件解释:
! Configuration File for keepalived
#全局定义块,包含邮件定义部分,
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL //负载均衡器标识,在一个网络内,它应该是唯一的
}
#VRRP实例定义块,负责负载均衡器之间的失败切换
vrrp_instance VI_1 {
state MASTER //只有MASTER和BACKUP 2种状态,主为MASTER,从为BACKUP,使用大写
interface eth0 //监控的网络接口
virtual_router_id 51 //同一实例下virtual_router_id必须相同
priority 100 //定义优先级,数字越大,优先级越高
advert_int 1 //MASTER与BACKUP负载均衡器之间同步检查的时间间隔,单位是秒
authentication { //验证类型和密码
auth_type PASS
auth_pass 1111
}
virtual_ipaddress { //虚拟ip地址virtual_ipaddress,可以定义多个
192.168.200.16
192.168.200.17
192.168.200.18
}
}
#虚拟服务器定义块
virtual_server 192.168.200.100 443 { //定义虚拟服务器
delay_loop 6 //delay_loop,健康检查时间间隔,单位是秒
lb_algo rr //负载调度算法,这里设置为rr,即轮询算法,互联网应用常使用wlc或rr
lb_kind NAT //负载均衡转发规则。一般包括DR,NAT,TUN3种,在我的方案中,都使用DR的方式
nat_mask 255.255.255.0 //子网掩码
persistence_timeout 50 //会话保持时间,单位是秒(可以适当延长时间以保持session)
protocol TCP //转发协议类型,有tcp和udp两种
real_server 192.168.201.100 443 { //真实服务器IP及端口
weight 1 //默认为1,0为失效
SSL_GET {
url {
path /
digest ff20ad2481f97b1754ef3e12ecd3a9cc
}
url {
path /mrtg/
digest 9b3a0c85a887a256d6939da88aabd8cd
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
virtual_server 10.10.10.2 1358 {
delay_loop 6
lb_algo rr
lb_kind NAT
persistence_timeout 50
protocol TCP
sorry_server 192.168.200.200 1358
real_server 192.168.200.2 1358 {
weight 1
HTTP_GET {
url {
path /testurl/test.jsp
digest 640205b7b0fc66c1ea91c463fac6334d
}
url {
path /testurl2/test.jsp
digest 640205b7b0fc66c1ea91c463fac6334d
}
url {
path /testurl3/test.jsp
digest 640205b7b0fc66c1ea91c463fac6334d
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.200.3 1358 {
weight 1
HTTP_GET {
url {
path /testurl/test.jsp
digest 640205b7b0fc66c1ea91c463fac6334c
}
url {
path /testurl2/test.jsp
digest 640205b7b0fc66c1ea91c463fac6334c
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}