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机器集群的一台机器上。
yum install keepalived (我这里是centos系统)
yum install ipvsadm
配置文件路径:/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配置如下:
! Configuration File for keepalived
global_defs {
router_id ld-1
}
vrrp_instance VI_1 {
interface eth0 # 设置对外服务的接口
state BACKUP # L1处于启用MASTER(必须大写)即启用状态
virtual_router_id 82 # 设置虚拟路由表示,L1也要设置为这个值
priority 150 # 设置优先级,数值越大,优先级越高,这里L2比L1的180小
vrrp_unicast_bind 192.168.1.2 #本机,这里是L2
vrrp_unicast_peer 192.168.1.1 # 对端L机器, 这里是L1
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
}
}
}
启动 L1,观察系统日志(/var/log/message)可见L1进入了MASTER模式,只是后端S机器没有开启相应的服务端口所以会有TCP Connection to ….. failed 错误
......
Jun 27 17:19:15 192-168-1-1 Keepalived_vrrp[425]: VRRP_Instance(VI_1) Transition to MASTER STATE
Jun 27 17:19:15 192-168-1-1 Keepalived_healthcheckers[424]: TCP connection to [192.168.1.3]:8080 failed !!!
Jun 27 17:19:15 192-168-1-1 Keepalived_healthcheckers[424]: Removing service [192.168.1.3]:8080 from VS [VSG_1]:0
Jun 27 17:19:15 192-168-1-1 Keepalived_healthcheckers[424]: TCP connection to [192.168.1.4]:8080 failed !!!
Jun 27 17:19:15 192-168-1-1 Keepalived_healthcheckers[424]: Removing service [192.168.1.4]:8080 from VS [VSG_1]:0
Jun 27 17:19:15 192-168-1-1 Keepalived_healthcheckers[424]: Lost quorum 1-0=1 > 0 for VS [VSG_1]:0
启动 L2,观察系统日志可见L2进入了BACKUP模式,只是后端S机器没有开启相应的服务端口所以会有TCP Connection to ….. failed 错误
......
Jun 27 17:34:37 192-168-1-2 Keepalived_vrrp[7597]: VRRP_Instance(VI_1) Entering BACKUP STATE
Jun 27 17:34:37 192-168-1-2 Keepalived_healthcheckers[7596]: Using LinkWatch kernel netlink reflector...
Jun 27 17:34:37 192-168-1-2 kernel: [256867.359972] IPVS: [wlc] scheduler registered.
Jun 27 17:34:37 192-168-1-2 Keepalived_healthcheckers[7596]: Activating healthchecker for service [192.168.1.3]:8080
Jun 27 17:34:37 192-168-1-2 Keepalived_healthcheckers[7596]: Activating healthchecker for service [192.168.1.4]:8080
Jun 27 17:34:38 192-168-1-2 Keepalived_healthcheckers[7596]: TCP connection to [192.168.1.3]:8080 failed !!!
Jun 27 17:34:38 192-168-1-2 Keepalived_healthcheckers[7596]: Removing service [192.168.1.3]:8080 from VS [VSG_1]:0
Jun 27 17:34:38 192-168-1-2 Keepalived_healthcheckers[7596]: TCP connection to [192.168.1.4]:8080 failed !!!
Jun 27 17:34:38 192-168-1-2 Keepalived_healthcheckers[7596]: Removing service [192.168.1.4]:8080 from VS [VSG_1]:0
......
S端机器上需要将VIP配置在lo(本地回环)做别名,主要用途为收到的请求src_ip->dst_ip,如192.168.8.137 -> 192.168.9.191,当到达rs后,rs发现自己有对应的服务IP,会从自身开始回包给192.168.8.137。
*记住这是DR模式
在**S1**、**S2**机器的/etc/rc.local配置里加上:
ifconfig lo:0 192.168.1.5 netmask 255.255.255.255 broadcast 192.168.1.5
route add -host 192.168.1.5 dev lo:0
echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce
sysctl -p >/dev/null 2>&1
并在命令行执行以上语句
S端在8080端口的服务开启了后…
去L1、L2机器上分别执行 ipvsadm 可看到:
IP Virtual Server version 1.2.1 (size=1048576)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.1.5:jamlink wlc
-> 192.168.1.3:jamlink Route 100 0 0
-> 192.168.1.4:jamlink Route 100 0 0
证明LVS已经起作用了
在L1、L2机器上分别执行ip a,则会发现在配置为MASTER的机器上才有VIP
接着去S端机器上执行 ifconfig 可以看到:
lo:0 Link encap:Local Loopback
inet addr:192.168.1.5 Mask:255.255.255.255
UP LOOPBACK RUNNING MTU:65536 Metric:1
VIP已经出现在S端的回环上
接下来验证LVS主备容错切换:关掉MASTER上的keepalived服务,发现BACKUP机器系统日志(/var/log/message)中会显示进入了MASTER角色,当重启原来MASTER的keepalived服务,则会发现它的系统日志显示它进入了BACKUP模式,相应的VIP则出现在了L2上
Welcome the modification from the experts!!!