LVS 负载均衡 之 DR模式
场景:
LVS机器L1 (IP:192.168.1.1)
LVS机器L2 (IP:192.168.1.2)
服务机器S1 (IP:192.168.1.3)
服务机器S2 (IP:192.168.1.4)
LVS集群虚拟IP VIP(192.168.1.5)
拓扑图
L1 S1 | / --->VIP| / \ | / L2 -- S2
解释:L1 L2公用 VIP,但同时只有一台LVS机器能使用VIP,另一台机器处于空闲状态,也叫做热备份,L1与L2之间有健康心跳交互检测对方,如果使用VIP的机器出现了问题,VIP会漂移到正常的备份机器上,备份机器开始使用VIP对外服务。使用VIP的L机器根据配置的调度算法(有8种)来进行负载均衡,最终把接入的TCP连接选择接入到S机器集群的一台机器上。
配置步骤
1.在L机器上安装相应的软件
yum install keepalived (我这里是centos系统)
yum install ipvsadm
2.在L机器上配置keepalived
配置文件路径:/etc/keepalived/keepalived.conf
这里我们把L1配置成master, 让他一开始就使用VIP工作,L2配置成backup,
L1配置如下:
! Configuration File for keepalived
global_defs {
router_id ld-1
}
vrrp_instance VI_1 {
interface eth0 # 设置对外服务的接口
state MASTER # L1处于启用MASTER(必须大写)即启用状态
virtual_router_id 82 # 设置虚拟路由表示,L2也要设置为这个值
priority 180 # 设置优先级,数值越大,优先级越高
vrrp_unicast_bind 192.168.1.1 #本机,这里是L1
vrrp_unicast_peer 192.168.1.2 # 对端L机器, 这里是L2
authentication {
auth_type PASS
auth_pass nenad
}
virtual_ipaddress {
192.168.1.5/24 dev eth0 # VIP 后面注意这里要填自己使用的网络接口
}
}
virtual_server_group VSG_1 {
192.168.1.5 8080 # VIP和S端服务的端口号
}
virtual_server group VSG_1 {
delay_loop 6 # 健康检查时间间隔
lb_algo wlc # 负载均衡算法
lb_kind DR # 负载均衡模式:Direct Routing
protocol TCP
real_server 192.168.1.3 8080 { # 后端服务S1
weight 100
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 8080
}
}
real_server 192.168.1.4 8080 { # 后端服务S2
weight 100
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 8080
}
}
}
L2配置如下: