DR模式—直接路由(Direct Routing)
LVS-DR数据包流向分析
(1)Client向目标VIP发出请求,Director(负载均衡器)接收,此时的IP包头及数据帧头信息为:
(2)Director根据负载均衡算法选择RealServer_1,不修改也不封装IP报文,而是将数据帧的MAC地址改为RealServer_1的MAC地址,然后在局域网上发送,IP包头及数据帧头信息如下:
(3)RealServer_1收到这个帧,解封装后发现目标IP与本机匹配(RealServer事先绑定了VIP
),于是处理这个报文,随后重新封装报文,发送到局域网,此时IP包头及数据帧头信息为:
(4)Cliebt将收到回复报文,Client人为得到正常的服务,而不会知道哪一台服务器处理的
注意:如果跨网段,那么报文通过路由器经由Internet返回给用户
LVS-DR中的APR问题
解决ARP的两个问题的设置方法
在企业应用中,单台服务器承担应用存在单点故障的危险,在企业应用集群中,存在了至少两处单点故障危险,单点故障一旦发生,企业服务将发生中断,造成极大的危害
Keepalived工具介绍
1、实验环境
主机名 | IP地址 |
---|---|
LVS主 | 192.168.100.110 |
LVS备 | 192.168.100.111 |
web1 | 192.168.100.201 |
web2 | 192.168.100.202 |
客户机 | 192.168.100.120 |
2、LVS主备配置DR调度器
(1)安装Keepalive和ipvsadm
[root@localhost ~]# yum install keepalived ipvsadm -y
(2)开启路由转发功能,proc响应关闭重定向功能
[root@localhost ~]# vim /etc/sysctl.conf
net.ipv4.ip_forward=1 //开启路由转发功能
#proc响应关闭重定向功能
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.ens33.send_redirects = 0
[root@localhost ~]# sysctl -p //生效配置
net.ipv4.ip_forward = 1
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.ens33.send_redirects = 0
(3)网卡配置
[root@localhost ~]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# vim ifcfg-ens33
...
BOOTPROTO="static"
...
IPADDR=192.168.100.110 //LVS备的IP地址为192.168.100.111
NETMASK=255.255.255.0
GATEWAY=192.168.100.1
[root@localhost network-scripts]# cp -p ifcfg-ens33 ifcfg-ens33:0
[root@localhost network-scripts]# vim ifcfg-ens33:0
DEVICE=ens33:0
ONBOOT=yes
IPADDR=192.168.100.10
NETMASK=255.255.255.0
(4)创建LVS-DR模式启动脚本
[root@localhost ~]# vim /etc/init.d/dr.sh
#!/bin/bash
GW=192.168.100.1
VIP=192.168.100.10
RIP1=192.168.100.201
RIP2=192.168.100.202
case "$1" in
start)
/sbin/ipvsadm --save > /etc/sysconfig/ipvsadm
systemctl start ipvsadm
/sbin/ifconfig ens33:0 $VIP broadcast $VIP netmask 255.255.255.255 broadcast $VIP up
/sbin/route add -host $VIP dev ens33:0
/sbin/ipvsadm -A -t $VIP:80 -s rr
/sbin/ipvsadm -a -t $VIP:80 -r $RIP1:80 -g
/sbin/ipvsadm -a -t $VIP:80 -r $RIP2:80 -g
echo "ipvsadm starting--------------------|ok|"
;;
stop)
/sbin/ipvsadm -C
systemctl stop ipvsadm
ifconfig ens33:0 down
route del $VIP
echo "ipvsadm stoped--------------------|ok|"
;;
status)
if [ ! -e /var/lock/subsys/ipvsadm ];then
echo "ipvsadm stoped--------------------"
exit 1
else
echo "ipvsadm Running--------------------|ok|"
fi
;;
*)
echo "Usage: $0 {start|stop|status}"
exit 1
esac
exit 0
[root@localhost ~]# vim /etc/init.d/dr.sh
[root@localhost ~]# chmod +x /etc/init.d/dr.sh
[root@localhost ~]# service dr.sh start
ipvsadm starting--------------------|ok|
3、web服务器配置,两台服务器同样的配置
(1)安装web服务
[root@localhost ~]# yum install httpd -y
(2)网卡配置
[root@localhost ~]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# vim ifcfg-ens33
...
BOOTPROTO=static
...
IPADDR=192.168.100.201
NETMASK=255.255.255.0
GATEWAY=192.168.100.1
[root@localhost network-scripts]# cp -p ifcfg-lo ifcfg-lo:0
[root@localhost network-scripts]# vim ifcfg-lo:0
DEVICE=lo:0
ONBOOT=yes
IPADDR=192.168.100.10
NETMASK=255.255.255.0
(3)建立web服务器抑制ARP响应脚本
[root@localhost ~]# cd /etc/init.d/
[root@localhost init.d]# vim web.sh
#!/bin/bash
VIP=192.168.100.10
case "$1" in
start)
ifconfig lo:0 $VIP netmask 255.255.255.255 broadcast $VIP
/sbin/route add -host $VIP 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/lo/arp_ignore
echo "2" > /proc/sys/net/ipv4/conf/lo/arp_announce
sysctl -p > /dev/null 2>&1
echo "RealServer Start OK "
;;
stop)
ifconfig lo:0 down
route del $VIP /dev/null 2>&1
echo "0" > /proc/sys/net/ipv4/conf/lo/arp_ignore
echo "0" > /proc/sys/net/ipv4/conf/lo/arp_announce
echo "0" > /proc/sys/net/ipv4/conf/all/arp_ignore
echo "0" > /proc/sys/net/ipv4/conf/all/arp_announce
echo "RealServer Stoped"
;;
*)
echo "Usage: $0 {start|stop}"
exit 1
esac
exit 0
[root@localhost init.d]# chmod +x web.sh
(4)启动服务
[root@localhost init.d]# service web.sh start
RealServer Start OK
[root@localhost init.d]# systemctl start httpd
[root@localhost init.d]# ifup lo:0
(5)自检网站
4、Keepalived部署(在调度服务器上设置)
(1)更改主配置文件
[root@localhost ~]# vim /etc/keepalived/keepalived.conf
global_defs {
...
smtp_server 127.0.0.1 //指向本地
router-id LVS_01 //指定名称,备份服务器不同名称
...
}
vrrp_instance VI_1 {
state MASTER //备份服务器是BACKUP
interface ens33
virtual_router_id 10 //组号相同
priority 100 //优先级备份小于主
...
auth_pass 1111 //验证密码
...
virtual_ipaddress {
192.168.100.10
}
...
virtual_server 192.168.100.10 80 {
...
lb_kind DR
...
real_server 192.168.100.201 80 {
weight 1
TCP_CHECK {
connect_port 80 //添加端口
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.100.202 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
(2)开启服务
[root@localhost ~]# systemctl start keepalived
(3)客户端测试
(4)故障模拟,关闭主调度器网络,依旧可以访问