LVS-DR方式实现负载均衡
一、环境介绍
lvs director配置:
[[email protected] ~]# ifconfig eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.2.13 netmask 255.255.255.0 broadcast 192.168.2.255 inet6 fe80::20c:29ff:feeb:7e7d prefixlen 64 scopeid 0x20<link> ether 00:0c:29:eb:7e:7d txqueuelen 1000 (Ethernet) RX packets 534 bytes 47172 (46.0 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 422 bytes 44139 (43.1 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10<host> loop txqueuelen 0 (Local Loopback) RX packets 23 bytes 2480 (2.4 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 23 bytes 2480 (2.4 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 [[email protected] ~]# ifconfig eno16777736:0 192.168.2.14/32 broadcast 192.168.2.14 # 配置虚拟地址DIP [[email protected] ~]# ifconfig eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.2.13 netmask 255.255.255.0 broadcast 192.168.2.255 inet6 fe80::20c:29ff:feeb:7e7d prefixlen 64 scopeid 0x20<link> ether 00:0c:29:eb:7e:7d txqueuelen 1000 (Ethernet) RX packets 673 bytes 58778 (57.4 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 503 bytes 53741 (52.4 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 eno16777736:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.2.14 netmask 0.0.0.0 broadcast 192.168.2.14 ether 00:0c:29:eb:7e:7d txqueuelen 1000 (Ethernet) lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10<host> loop txqueuelen 0 (Local Loopback) RX packets 23 bytes 2480 (2.4 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 23 bytes 2480 (2.4 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 [[email protected] ~]# route add -host 192.168.2.14 dev eno16777736:0
接下来对realserver进行配置:
1、首先对arp_ignore和arp_announce进行修改
2、再次将vip添加到网卡lo
[[email protected] ~]# vim /etc/sysctl.conf # 在文件末尾处添加 net.ipv4.conf.all.arp_ignore = 1 net.ipv4.conf.all.arp_announce = 2 net.ipv4.conf.eth0.arp_ignore = 1 net.ipv4.conf.eth0.arp_announce = 2 [[email protected] ~]# sysctl -p [[email protected] ~]# ifconfig lo:0 192.168.2.14/32 broadcast 192.168.2.14 up # 在本地回环地址上添加VIP[[email protected] ~]# route add -host 192.168.2.14 dev lo:0 两台realserver一样的配置,不在贴代码
配置IPVS规则:
[[email protected] ~]# vim /etc/hosts # 配置好hosts是必须的 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 # director 192.168.2.13 rhel7 10.0.0.1 rhel7 # realserver 10.0.0.10 RS1 10.0.0.11 RS2 [[email protected] ~]# ipvsadm -A -t 192.168.2.14:80 -s rr [[email protected] ~]# ipvsadm -a -t 192.168.2.14:80 -r 192.168.2.10 -g -w 1 [[email protected] ~]# ipvsadm -a -t 192.168.2.14:80 -r 192.168.2.11 -g -w 1 [[email protected] ~]# ipvsadm -L -n IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn TCP 192.168.2.14:80 rr -> 192.168.2.10:80 Route 1 0 0 -> 192.168.2.11:80 Route 1 0 0
测试:
时间: 2024-10-19 23:39:24