Linux集群–LVS-DR模式与构建
1.数据包从client端(公网),经过集群的路由器(图中省略)进入集群私网中,经过交换机,发送数据给D(负载均衡调度器);
2.数据包到达D后会修改DMAC(如RS1的DMAC),RS1中配置VIP为D的ip(图中10.10.10.100)后可接收包
3.此时集群内D与RS1均有数据包中的目标ip,存在IP冲突,解决办法见后文(配置ARP);
4.RS1响应客户端
5.下次根据调度算法重新分配服务器。
负载调度器配置:
# 关闭网卡守护进程
[root@localhost ~]# service NetworkManager stop
停止 NetworkManager 守护进程: [确定]
[root@localhost ~]# chkconfig NetworkManager off # 开机不自启
# 开启网卡子接口
[root@localhost network-scripts]# cp ifcfg-eth4 ifcfg-eth4:0
[root@localhost network-scripts]# cat ifcfg-eth4:0
DEVICE=eth4:0
ONBOOT=yes
BOOTPROTO=static
IPADDR=10.10.10.100 # 浮动ip
NETMASK=255.255.255.0
[root@localhost network-scripts]# service network restart
# 关闭广播功能(可省略)
[root@localhost network-scripts]# vim /etc/sysctl.conf
[root@localhost network-scripts]# sysctl -p
# 仅显示添加的内容
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.eth4.send_redirects = 0
# 安装ipvsadm
[root@localhost network-scripts]# yum -y install ipvsadm
[root@localhost network-scripts]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
多台服务器同时配置如下:
# 关闭网卡守护进程
[root@localhost ~]# service NetworkManager stop
停止 NetworkManager 守护进程: [确定]
[root@localhost ~]# chkconfig NetworkManager off # 开机不自启
# 启动Apache
[root@localhost ~]# service httpd start
# 添加VIP
[root@localhost ~]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# cp ifcfg-lo ifcfg-lo:0
[root@localhost network-scripts]# cat ifcfg-lo:0
# 设置如下
DEVICE=lo:0
IPADDR=10.10.10.100
NETMASK=255.255.255.255
NETWORK=127.0.0.0
BROADCAST=127.255.255.255
ONBOOT=yes
NAME=loopback
[root@localhost network-scripts]# vim /etc/sysctl.conf
[root@localhost network-scripts]# sysctl -p
# 仅显示添加的内容
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.default.arp_ignore = 1
net.ipv4.conf.default.arp_announce = 2
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
# 添加路由
[root@localhost network-scripts]# route add -host 10.10.10.100 dev lo:0
[root@localhost network-scripts]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
10.10.10.100 0.0.0.0 255.255.255.255 UH 0 0 0 lo
10.10.10.0 0.0.0.0 255.255.255.0 U 0 0 0 eth2
169.254.0.0 0.0.0.0 255.255.0.0 U 1003 0 0 eth2
# 开机自启
[root@localhost network-scripts]# echo "route add -host 10.10.10.100 dev lo:0" >> /etc/rc.local
# 添加规则(调度器)
#-A添加集群 -a添加节点 -t tcp -s 算法 rr 轮询 -r realserver -g DR模式
[root@localhost network-scripts]# ipvsadm -A -t 10.10.10.100:80 -s rr
[root@localhost network-scripts]# ipvsadm -a -t 10.10.10.100:80 -r 10.10.10.12 -g
[root@localhost network-scripts]# ipvsadm -a -t 10.10.10.100:80 -r 10.10.10.13 -g
[root@localhost network-scripts]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 10.10.10.100:80 rr
-> 10.10.10.12:80 Route 1 0 0
-> 10.10.10.13:80 Route 1 0 0
多次访问10.10.10.100:80(apache)后查看调度结果
[root@localhost network-scripts]# ipvsadm -Ln --stats
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Conns InPkts OutPkts InBytes OutBytes
-> RemoteAddress:Port
TCP 10.10.10.100:80 10 51 0 7617 0
-> 10.10.10.12:80 5 25 0 3490 0
-> 10.10.10.13:80 5 26 0 4127 0
*本文图片来自尚硅谷课件