服务介绍 Director 分配请求到不同的real server. real server 处理请求后直接回应给用户,这样director 负载均衡器仅处理客户机与服务器的一半连接,负载均衡器处理一半的链接,避免了新的性能瓶颈,同样增加了系统的可伸缩性。Director Routing 采用物理层修改MAC 地址技术,因此所以服务器必须在同一个网段中。 DR的优点:NAT模式效率低 DR方式特点:都是公网地址
拓扑图:
实例 :配置LVS DR模式集群 实验环境 Localhost DIP : 192.168.10.70 VIP: 192.168.10.63 配置Localhost [iyunv@localhost~]# cat /etc/sysconfig/network-scripts/ifcfg-eth1查看eth1的网卡
配置网卡 eth1:1 eth1 和eth1:1网卡的MAC 地址必须一样 [iyunv@localhostnetwork-scripts] # cd/etc/sysconfig/network-scripts/ [iyunv@localhost network-scripts]# vimifcfg-eth1:1
查看网卡信息
配置LVS-DR规则 rpm -ivh/mnt/Packages/ipvsadm-1.26-2.el6.x86_64.rpm # 安装软件包 [iyunv@localhost~]# ipvsadm -A -t 192.168.10.63:80 -s rr -A : 添加-t: 表示tcp -s: 指定调度算法 rr : 表示轮询 -g:表示DR -r:指定real server [iyunv@localhost ~]# ipvsadm -a -t192.168.10.63:80 -r 192.168.10.62 -g [iyunv@localhost~]# ipvsadm -a -t 192.168.10.63:80 -r 192.168.10.64 -g
[iyunv@localhost~]# ipvsadm -L -n IP VirtualServer version 1.2.1 (size=4096) ProtLocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn TCP 192.168.10.63:80 rr -> 192.168.10.62:80 Route 1 0 0 -> 192.168.10.64:80 Route 1 0 0 备注:在LVS 模式中,只要nat 模式需要开启路由转发功能 配置Real Server localhost1.cn Eth1 IP:192.168.10.62 桥接模式 生成回环口配置文件 [iyunv@localhost1 network-scripts]# pwd [iyunv@localhost1 network-scripts]# cpifcfg-lo ifcfg-lo:1 root@localhost1 network-scripts]# catifcfg-lo:1 DEVICE=lo:1 IPADDR=192.168.10.63 NETMASK=255.255.255.255 ONBOOT=yes NAME=loopback 查看网卡信息
关闭ARP转发 [iyunv@localhost1 ~]# echo 1 >/proc/sys/net/ipv4/conf/eth1/arp_ignore [iyunv@localhost1 ~]# echo 2 >/proc/sys/net/ipv4/conf/eth1/arp_announce net.ipv4.conf.eth1.arp_ignore =1 net.ipv4.conf.eth1.arp_announce =2 若要永久生效则要在 vim/etc/sysctl.conf最后添加,sysctl –p 生效 网关指向 公网出口路由器 GATEWAY=192.168.10.1 启动80端口 [iyunv@localhost1 ~]# echo 192.168.10.62 >/var/www/html/index.html [iyunv@localhost1html]# /etc/init.d/httpd restart
配置Real Server localhost2.cn Eth1 IP:192.168.10.64 桥接模式 生成回环口配置文件 [iyunv@localhost2 network-scripts]# cpifcfg-lo ifcfg-lo:1 [iyunv@localhost2 network-scripts]# catifcfg-lo:1 DEVICE=lo:1 IPADDR=192.168.10.63 NETMASK=255.255.255.255 ONBOOT=yes NAME=loopback 查看网卡信息
关闭ARP 转发 echo 1 >/proc/sys/net/ipv4/conf/eth1/arp_ignore echo 2 >/proc/sys/net/ipv4/conf/eth1/arp_announce 网关指向 公网出口路由器 GATEWAY=192.168.10.1 启动80端口 echo 192.168.10.64 >/var/www/html/index.html /etc/init.d/httpd restart
客户端测试vip 先测试real server http://192.168.10.64 http://192.168.10.62
测试VIP http://192.168.10.63 多刷新几次
测试LVS其他的调度算法 [iyunv@localhost~]# ipvsadm -A -t 192.168.10.63:80 -s wrr Wrr 基于集群节点分配处理每一个节点的权重,权重值为2的服务器将为1的服务器的2倍,如果权重值为0,则不会收到新的连接 [iyunv@localhost~]# ipvsadm -a -t 192.168.10.63:80 -r 192.168.10.62 -g -w 10 [iyunv@localhost~]# ipvsadm -a -t 192.168.10.63:80 -r 192.168.10.64 -g -w 20 [iyunv@localhost~]# ipvsadm -L -n --stats IP VirtualServer version 1.2.1 (size=4096) ProtLocalAddress:Port Conns InPkts OutPkts InBytes OutBytes -> RemoteAddress:Port TCP 192.168.10.63:80 0 0 0 0 0 -> 192.168.10.62:80 0 0 0 0 0 -> 192.168.10.64:80 0 0 0 0 0
在物理机上,刷新9次链接 http://192.168.10.63 localhost 1:localhost2 是1:2的关系 查看
如果一个real server 的权重是0 ,就不会分配给客户端的请求 [iyunv@localhost~]# ipvsadm -A -t 192.168.10.63:80 -s wrr [iyunv@localhost~]# ipvsadm -a -t 192.168.10.63:80 -r 192.168.10.62 -g -w 0 [iyunv@localhost~]# ipvsadm -a -t 192.168.10.63:80 -r 192.168.10.64 -g -w 20 [iyunv@localhost~]# ipvsadm -L -n --stats IP VirtualServer version 1.2.1 (size=4096) ProtLocalAddress:Port Conns InPkts OutPkts InBytes OutBytes -> RemoteAddress:Port TCP 192.168.10.63:80 0 0 0 0 0 -> 192.168.10.62:80 0 0 0 0 0 -> 192.168.10.64:80 0 0 0 0 0
在物理机上,刷新链接 http://192.168.10.63,只会出现192.168.10.64
清空内核虚拟服务器中的所有记录 [iyunv@localhost~]# ipvsadm -L -n --stats 显示统计信息 IP VirtualServer version 1.2.1 (size=4096) ProtLocalAddress:Port Conns InPkts OutPkts InBytes OutBytes -> RemoteAddress:Port
[iyunv@localhost ~]# ipvsadm -L -n --rate 显示速率信息 IP VirtualServer version 1.2.1 (size=4096) ProtLocalAddress:Port CPS InPPS OutPPS InBPS OutBPS -> RemoteAddress:Port [iyunv@localhost~]# ipvsadm -L -n –c 显示目前链接 IPVS connectionentries pro expirestate source virtual destination
LVS 的配置文件保存在 /etc/init.d /ipvsadm [iyunv@localhost ~]# cat/etc/sysconfig/ipvsadm-config LVS 的规则存储文件保存位置
|