1、理论部分
基于lvs的负载均衡无法检测Real Server的状态,当Real Server的服务单点故障,Director照样把请求转发给故障的机器,造成请求无法得到正常的服务。
所以就需要引入一种检测机制来弥补缺陷,这就需要Keepalived来实现,他不但可以检测Real Server的状态,同时也可以检测Director的状态,达到Failover的目的。
2、实验部分
2.1、实验基础
LVS均衡负载基础:
http://cmdschool.blog.51cto.com/2420395/1702421
以上基础并增加一台副Director主机
2.2、主机信息
Dr1:
director ipaddress=10.168.0.89
vip ipaddress=10.168.0.91
hostname=dr1
Dr2:
director ipaddress=10.168.0.90
vip ipaddress=10.168.0.91
hostname=dr2
Rs1:
real ipaddress=10.168.0.94
vip-lo ipaddress=10.168.0.91
hostname=rs1
Rs2:
real ipaddress=10.168.0.95
vip-lo ipaddress=10.168.0.91
hostname=rs2
2.3、yum源安装
In Director
1
|
yum
install
-y keepalived
|
2.4、设置配置文件
2.4.1、step1
IN DR1
vim编辑/etc/keepalived/keepalived.conf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
vrrp_instance VI_1 {
state MASTER
#备用服务器上为 BACKUP
interface eth0
virtual_router_id 51
priority 100
#备用服务器上为90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.168.0.91
}
}
virtual_server 10.168.0.91 80 {
delay_loop 6
#(每隔10秒查询realserver状态)
lb_algo wlc
#(lvs 算法)
lb_kind DR
#(Direct Route)
persistence_timeout 60
#(同一IP的连接60秒内被分配到同一台realserver)
protocol TCP
#(用TCP协议检查realserver状态)
real_server 10.168.0.92 80 {
weight 100
#(权重)
TCP_CHECK {
connect_timeout 10
#(10秒无响应超时)
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 10.168.0.93 80 {
weight 100
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
|
IN DR2
vim编辑/etc/keepalived/keepalived.conf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
vrrp_instance VI_1 {
state BACKUP
#主服务器上为 MASTER
interface eth0
virtual_router_id 51
priority 90
#主服务器上为100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.168.0.91
}
}
virtual_server 10.168.0.91 80 {
delay_loop 6
#(每隔10秒查询realserver状态)
lb_algo wlc
#(lvs 算法)
lb_kind DR
#(Direct Route)
persistence_timeout 60
#(同一IP的连接60秒内被分配到同一台realserver)
protocol TCP
#(用TCP协议检查realserver状态)
real_server 10.168.0.92 80 {
weight 100
#(权重)
TCP_CHECK {
connect_timeout 10
#(10秒无响应超时)
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 10.168.0.93 80 {
weight 100
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
|
1
|
<br>
|
注意,DR1&DR2差异仅如下:
state MASTER <-> state BACKUP
priority 100 <-> priority 90
2.4.2、step2
IN DR1&DR2
开启端口转发:
1
|
echo
1 >
/proc/sys/net/ipv4/ip_forward
|
2.4.3、step3
IN RS1&RS2
启动脚本:
1
|
/usr/local/sbin/lvs_dr_rs
.sh
|
2.4.4、step4
IN DR1&DR2
启动keeplive服务:
1
|
/etc/init
.d
/keepalived
start
|
注意:
以下脚本不需要再像在LVS实验中那样执行“/usr/local/sbin/lvs_dr.sh”