Centos7+LVS-DR+Apache负载均衡web实验

时间:2021-05-25 19:17:39

一、简介

  1、理论已经在上一篇博客简述,不了解得可以看看

    https://www.cnblogs.com/zhangxingeng/p/10497279.html

  2、LVS-DR优缺点复习     

    关于这种模式:

      1)确保前端路由器将目标ip为vip的请求报文发往director

        a、在前端网关做静态绑定;

        b、在RS上使用arptables;

        c、在RS上修改内核参数以限制arp通告即应答级别;

          arp_announce

          arp_ignore

      2)、RS的RIP可以使用私网或公网地址;

      3)、RS跟director在同一物理网络;

      4)、请求报文经由director,响应报文直接发往client;

      5)、此模式不支持端口映射;

      6)、RS支持大多数的OS;

      7)、RIP的网关不能指向DIP,以确保响应报文不经由director;

      

     缺点:LVS调度器及应用服务器在同一个网段中,因此不能实现集群的跨网段应用。

     优点:直接路由转发,通过修改请求报文的目标mac地址进行转发,效率提升明显

  3、实验拓扑

Centos7+LVS-DR+Apache负载均衡web实验

    实验说明:测试环境为虚拟机,准备三台即可,这里是四台外加本机物理机

节点 ip 应用 ip2 备注
web1 192.168.13.11(DIP) director VIP:ens37:0,192.168.13.100  
web2 192.168.13.12(RIP) rs VIP:Lo:0,192.168.13.100  
web3 192.168.13.13(RIP) rs VIP:Lo:0,192.168.13.100  
web4 192.168.13.14 client   curl
本地物理机测试 172网段 client   web访问

    

二、开始部署

  1、网络配置

   修改虚拟机网卡rip、vip、dip都在同一个网段,这里使用仅主机模式

    配置网卡基础参数这里就不演示,配置静态,网关指向仅主机虚拟网卡网关即可

   director一块网卡即可,后续配置添加vip使用别名即可,别着急,在后面具体配置上

    

 [root@web1 ~]#  ifconfig
ens37: flags=<UP,BROADCAST,RUNNING,MULTICAST> mtu
inet 192.168.13.11 netmask 255.255.255.0 broadcast 192.168.13.255
inet6 fe80::20c:29ff:fe1c:8b43 prefixlen scopeid 0x20<link>
ether :0c::1c:8b: txqueuelen (Ethernet)
RX packets bytes (46.0 MiB)
RX errors dropped overruns frame
TX packets bytes (8.8 MiB)
TX errors dropped overruns carrier collisions

   

   rs也是一块网卡,后续vip配置在lo:0上即可但是需要先修改arp_ignore,arp_announce才能添加lo:0

   

 [root@web2 ~]# ifconfig
ens33: flags=<UP,BROADCAST,RUNNING,MULTICAST> mtu
inet 192.168.13.12 netmask 255.255.255.0 broadcast 192.168.13.255
inet6 fe80:::e73d:1ef:2e1 prefixlen scopeid 0x20<link>
ether :0c:::de: txqueuelen (Ethernet)
RX packets bytes (41.4 MiB)
RX errors dropped overruns frame
TX packets bytes (11.7 MiB)
TX errors dropped overruns carrier collisions
[root@web3 ~]# ifconfig
ens33: flags=<UP,BROADCAST,RUNNING,MULTICAST> mtu
inet 192.168.13.13 netmask 255.255.255.0 broadcast 192.168.13.255
inet6 fe80:::e73d:1ef:2e1 prefixlen scopeid 0x20<link>
inet6 fe80:::80e8:f210:1e24 prefixlen scopeid 0x20<link>
ether :0c:::: txqueuelen (Ethernet)
RX packets bytes (41.0 MiB)
RX errors dropped overruns frame
TX packets bytes (11.7 MiB)
TX errors dropped overruns carrier collisions

 

  2、关闭防火墙

    

       systemctl stop firewalld

       systemctl disable firewalld

       sed -i '/SELINUX/s/enforcing/disabled/' /etc/selinux/config 

  3、安装软件

       11上安装ipvsadm     
       yum install ipvsadm -y
      、13安装httpd
        yum install httpd -y
  

  4、配置web

    注意:arp_ignore 改为1的意义是,响应报文,请求报文从哪个地址进来的,就只能这个接口地址响应

       arp_announce 改为2的意义是,通知,不通告不同网段

 #修改web页面
      web2         echo "welcom to web2" >/var/www/html/index.html      web3
        echo "welcom to web3" >/var/www/html/index.html
#启动与开机自启
        systemctl restart httpd
         systemctl enable httpd
#修改内核参数,两台rs都修改
echo > /porc/sys/net/ipv4/conf/all/arp_ignore
echo > /proc/sys/net/ipv4/conf/all/arp_ignore
echo > /proc/sys/net/ipv4/conf/ens33/arp_ignore
echo > /proc/sys/net/ipv4/conf/all/arp_announce
echo > /proc/sys/net/ipv4/conf/ens33/arp_announce
  #添加vip
  ifconfig lo:0 192.168.13.100 broadcast 192.168.13.100 netmask 255.255.255.255 up
#添加默认路由,两台rs都修改, route add -host 192.168.13.100 dev lo:
  #ping 192.168.13.100 

ping 192.168.13.100 

 #物理机查看mac地址没有变化,说明rs没有响应ok ,11和100的地址保持一致就是对的
   物理机是windows10 cmd 命令行 arp -a 查看mac表
arp -a
2 接口: 192.168.13.1 --- 0x16 Internet 地址 物理地址 类型 192.168.13.11 00-0c-29-1c-8b-43 动态 192.168.13.12 -0c---de- 动态 192.168.13.13 -0c---- 动态 192.168.13.14 -0c--0f-ea-b4 动态 192.168.13.100 00-0c-29-1c-8b-43 动态

    检查网络信息,web2举例,web3差不多就不贴上来了

 [root@web2 ~]# ifconfig
ens33: flags=<UP,BROADCAST,RUNNING,MULTICAST> mtu
inet 192.168.13.12 netmask 255.255.255.0 broadcast 192.168.13.255
inet6 fe80:::e73d:1ef:2e1 prefixlen scopeid 0x20<link>
ether :0c:::de: txqueuelen (Ethernet)
RX packets bytes (41.4 MiB)
RX errors dropped overruns frame
TX packets bytes (11.7 MiB)
TX errors dropped overruns carrier collisions lo: flags=<UP,LOOPBACK,RUNNING> mtu
inet 127.0.0.1 netmask 255.0.0.0
inet6 :: prefixlen scopeid 0x10<host>
loop txqueuelen (Local Loopback)
RX packets bytes (97.4 KiB)
RX errors dropped overruns frame
TX packets bytes (97.4 KiB)
TX errors dropped overruns carrier collisions 20 lo:0: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
21 inet 192.168.13.100 netmask 255.255.255.255
22 loop txqueuelen 1 (Local Loopback) virbr0: flags=<UP,BROADCAST,MULTICAST> mtu
inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255
ether ::::a5:7c txqueuelen (Ethernet)
RX packets bytes (0.0 B)
RX errors dropped overruns frame
TX packets bytes (0.0 B)
TX errors dropped overruns carrier collisions [root@web2 ~]#

   

  5、配置director

    lvs-dr模式不需要开启地址转发,ip_forward功能,因为director发给rs的数据包是修改过的包,不是原包,不经过ip_forward转发,而是经过修改后的mac根据l路由决策直接路由到rip

 1 #ens33是另外一块网卡down掉,配置网卡别名,添加vip
ifconfig ens33 down
ifconfig ens37: 192.168.13.100/ broadcast 192.168.13.100 up
4 #配置ipvsadm
ipvsadm -C 清空
6 #添加规则
ipvsadm -A -t 192.168.13.100: -s rr
ipvsadm -a -t 192.168.13.100: -r 192.168.13.12 -g
ipvsadm -a -t 192.168.13.100: -r 192.168.13.13 -g
10 #查看规则
[root@web1 zhangxingeng]# ipvsadm -ln
IP Virtual Server version 1.2. (size=)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.13.100: rr
-> 192.168.13.12: Route
-> 192.168.13.13: Route

    检查网络信息

    

[root@web1 ~]#  ifconfig
ens37: flags=<UP,BROADCAST,RUNNING,MULTICAST> mtu
inet 192.168.13.11 netmask 255.255.255.0 broadcast 192.168.13.255
inet6 fe80::20c:29ff:fe1c:8b43 prefixlen scopeid 0x20<link>
ether :0c::1c:8b: txqueuelen (Ethernet)
RX packets bytes (46.0 MiB)
RX errors dropped overruns frame
TX packets bytes (8.8 MiB)
TX errors dropped overruns carrier collisions ens37:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.13.100 netmask 255.255.255.255 broadcast 192.168.13.100
ether 00:0c:29:1c:8b:43 txqueuelen 1000 (Ethernet) lo: flags=<UP,LOOPBACK,RUNNING> mtu
inet 127.0.0.1 netmask 255.0.0.0
inet6 :: prefixlen scopeid 0x10<host>
loop txqueuelen (Local Loopback)
RX packets bytes (11.0 MiB)
RX errors dropped overruns frame
TX packets bytes (11.0 MiB)
TX errors dropped overruns carrier collisions virbr0: flags=<UP,BROADCAST,MULTICAST> mtu
inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255
ether ::::a5:7c txqueuelen (Ethernet)
RX packets bytes (0.0 B)
RX errors dropped overruns frame
TX packets bytes (0.0 B)
TX errors dropped overruns carrier collisions [root@web1 ~]#

  6、测试结果

Centos7+LVS-DR+Apache负载均衡web实验

 [root@web4 ~]# curl 192.168.13.100
welcom to web3
[root@web4 ~]# curl 192.168.13.100
welcom to web2
[root@web4 ~]# curl 192.168.13.100
welcom to web3
[root@web4 ~]# curl 192.168.13.100
welcom to web2
[root@web4 ~]# curl 192.168.13.100
welcom to web3
[root@web4 ~]# curl 192.168.13.100
welcom to web2
[root@web4 ~]# curl 192.168.13.100
welcom to web3
#统计信息
[root@web1 ~]# ipvsadm -ln --stats
IP Virtual Server version 1.2. (size=)
Prot LocalAddress:Port Conns InPkts OutPkts InBytes OutBytes
-> RemoteAddress:Port
TCP 192.168.13.100:
-> 192.168.13.12:
-> 192.168.13.13:
[root@web1 ~]#
 #连接条目
1 [root@web1 ~]# ipvsadm -lnc
IPVS connection entries
pro expire state source virtual destination
TCP : FIN_WAIT 192.168.13.163: 192.168.13.100: 192.168.13.12:
TCP : FIN_WAIT 192.168.13.163: 192.168.13.100: 192.168.13.13:
TCP : FIN_WAIT 192.168.13.163: 192.168.13.100: 192.168.13.13:
TCP : ESTABLISHED 192.168.13.1: 192.168.13.100: 192.168.13.13:
TCP : FIN_WAIT 192.168.13.163: 192.168.13.100: 192.168.13.12:
TCP : FIN_WAIT 192.168.13.163: 192.168.13.100: 192.168.13.12:
TCP : FIN_WAIT 192.168.13.163: 192.168.13.100: 192.168.13.13:
TCP : FIN_WAIT 192.168.13.163: 192.168.13.100: 192.168.13.12:
TCP : ESTABLISHED 192.168.13.1: 192.168.13.100: 192.168.13.12:

 

转载请注明出处:https://www.cnblogs.com/zhangxingeng/p/10573695.html