LVS负载均衡:DR模式详细部署

前言

部署其实并不难,关键点在网络的配置对原理的理解,要想配置好网络必须要有一副容易理解的拓扑图。

在这里插入图片描述

一、LVS配置

配置VIP

[root@localhost ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.10.30.10  netmask 255.0.0.0  broadcast 10.255.255.255
        inet6 fe80::2ceb:2392:df29:e7bd  prefixlen 64  scopeid 0x20<link>
        ether 00:50:56:25:69:d9  txqueuelen 1000  (Ethernet)
        RX packets 1752  bytes 159026 (155.2 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 302  bytes 27720 (27.0 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 1000  (Local Loopback)
        RX packets 204  bytes 18656 (18.2 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 204  bytes 18656 (18.2 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0


[root@localhost ~]# ifconfig ens33:0 10.10.30.60 broadcast 10.10.30.255 netmask 255.255.255.0 up
[root@localhost ~]# ip add
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:50:56:25:69:d9 brd ff:ff:ff:ff:ff:ff
    inet 10.10.30.10/8 brd 10.255.255.255 scope global noprefixroute ens33
       valid_lft forever preferred_lft forever
    inet 10.10.30.60/24 brd 10.10.30.255 scope global ens33:0
       valid_lft forever preferred_lft forever
    inet6 fe80::2ceb:2392:df29:e7bd/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default 
    link/ether 02:42:1f:5e:2f:14 brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
       valid_lft forever preferred_lft forever
       

给VIP配置一条假的路由
[root@localhost ~]# route add -host 10.5.6.60 dev ens33:0    #VIP只允许使用ens33:0接口通信,意思就是不使用真实网卡通信

开启路由转发

[root@localhost ~]# vim /etc/sysctl.conf

net.ipv4.ip_forward = 1                     #开启路由功能
net.ipv4.conf.all.send_redirects = 0        #禁止转发重定向报文
net.ipv4.conf.ens33.send_redirects = 0      #禁止ens33转发重定向报文,记得改成自己的网卡
net.ipv4.conf.default.send_redirects = 0    #禁止转发默认重定向报文

[root@localhost ~]# sysctl -p
net.ipv4.ip_forward = 1
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.ens33.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0

安装ipvsadm

[root@localhost ~]# yum -y install ipvsadm

添加规则

[root@localhost ~]# ipvsadm -C
[root@localhost ~]# ipvsadm -A -t 10.10.30.60:80 -s rr
[root@localhost ~]# ipvsadm -a -t 10.10.30.60:80 -r 10.10.30.2 -g    #g是指DR模式
[root@localhost ~]# ipvsadm -a -t 10.10.30.60:80 -r 10.10.30.3 -g
[root@localhost ~]# 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.30.60:80 rr
  -> 10.10.30.2:80                Route   1      0          0         
  -> 10.10.30.3:80                Route   1      0          0         

[root@localhost ~]# ipvsadm-save > /etc/sysconfig/ipvsadm           #必须保存到配置文件里面,不然无法开机自启
[root@localhost ~]# cat /etc/sysconfig/ipvsadm
-A -t localhost.localdomain:http -s rr
-a -t localhost.localdomain:http -r 10.10.30.2:http -g -w 1
-a -t localhost.localdomain:http -r 10.10.30.3:http -g -w 1

[root@localhost ~]# systemctl enable ipvsadm
Created symlink from /etc/systemd/system/multi-user.target.wants/ipvsadm.service to /usr/lib/systemd/system/ipvsadm.service.
[root@localhost ~]# 

二、RS1配置

安装apache

[root@RS1 ~]# yum -y install httpd
...

[root@RS1 ~]# systemctl start httpd
[root@RS1 ~]# systemctl enable httpd


[root@RS1 ~]# vim /var/www/html/index.html
添加:
server01

配置VIP

[root@RS1 ~]# ifconfig

ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.10.30.2  netmask 255.0.0.0  broadcast 10.255.255.255
        inet6 fe80::fce3:f59c:6c06:7636  prefixlen 64  scopeid 0x20<link>
        inet6 fe80::2ceb:2392:df29:e7bd  prefixlen 64  scopeid 0x20<link>
        ether 00:50:56:3a:ed:a7  txqueuelen 1000  (Ethernet)
        RX packets 25019  bytes 35723453 (34.0 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 6852  bytes 561647 (548.4 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 1000  (Local Loopback)
        RX packets 258  bytes 23850 (23.2 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 258  bytes 23850 (23.2 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
        
        
[root@RS1 ~]# ifconfig lo:0 10.10.30.60/32

[root@RS1 ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.10.30.2  netmask 255.0.0.0  broadcast 10.255.255.255
        inet6 fe80::fce3:f59c:6c06:7636  prefixlen 64  scopeid 0x20<link>
        inet6 fe80::2ceb:2392:df29:e7bd  prefixlen 64  scopeid 0x20<link>
        ether 00:50:56:3a:ed:a7  txqueuelen 1000  (Ethernet)
        RX packets 25175  bytes 35737407 (34.0 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 6944  bytes 572953 (559.5 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 1000  (Local Loopback)
        RX packets 258  bytes 23850 (23.2 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 258  bytes 23850 (23.2 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo:0: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 10.10.30.60  netmask 0.0.0.0
        loop  txqueuelen 1000  (Local Loopback)

抑制ARP响应

[root@RS1 ~]# echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore       
[root@RS1 ~]# echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce

三、RS2的配置

安装apache

[root@RS2 ~]# yum -y install httpd
...

[root@RS2 ~]# systemctl start httpd
[root@RS2 ~]# systemctl enable httpd


[root@RS1 ~]# vim /var/www/html/index.html
添加:
server02

配置VIP

[root@RS2 ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.10.30.3  netmask 255.0.0.0  broadcast 10.255.255.255
        inet6 fe80::fce3:f59c:6c06:7636  prefixlen 64  scopeid 0x20<link>
        inet6 fe80::2ceb:2392:df29:e7bd  prefixlen 64  scopeid 0x20<link>
        inet6 fe80::8f6c:47f4:edf5:96ed  prefixlen 64  scopeid 0x20<link>
        ether 00:50:56:37:15:f8  txqueuelen 1000  (Ethernet)
        RX packets 24760  bytes 35458206 (33.8 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 7943  bytes 653281 (637.9 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 1000  (Local Loopback)
        RX packets 260  bytes 24067 (23.5 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 260  bytes 24067 (23.5 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@RS2 ~]# ifconfig lo:0 10.10.30.60/32


[root@RS2 ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.10.30.3  netmask 255.0.0.0  broadcast 10.255.255.255
        inet6 fe80::fce3:f59c:6c06:7636  prefixlen 64  scopeid 0x20<link>
        inet6 fe80::2ceb:2392:df29:e7bd  prefixlen 64  scopeid 0x20<link>
        inet6 fe80::8f6c:47f4:edf5:96ed  prefixlen 64  scopeid 0x20<link>
        ether 00:50:56:37:15:f8  txqueuelen 1000  (Ethernet)
        RX packets 24771  bytes 35459160 (33.8 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 7950  bytes 657013 (641.6 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 1000  (Local Loopback)
        RX packets 260  bytes 24067 (23.5 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 260  bytes 24067 (23.5 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo:0: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 10.10.30.60  netmask 0.0.0.0
        loop  txqueuelen 1000  (Local Loopback)

抑制ARP响应

[root@RS2 ~]# echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore       
[root@RS2 ~]# echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce

四、测试结果

开启第4台centos7作为测试机,IP为:10.10.30.4

用elinks测试
[root@localhost ~]# elinks -dump  http://10.10.30.60
   server01
[root@localhost ~]# elinks -dump  http://10.10.30.60
   server02
[root@localhost ~]# elinks -dump  http://10.10.30.60
   server01
[root@localhost ~]# elinks -dump  http://10.10.30.60
   server02
[root@localhost ~]# elinks -dump  http://10.10.30.60
   server01
[root@localhost ~]# elinks -dump  http://10.10.30.60
   server02
[root@localhost ~]# elinks -dump  http://10.10.30.60
   server01
[root@localhost ~]# elinks -dump  http://10.10.30.60
   server02

再用curl测试
[root@localhost ~]# curl http://10.10.30.60
server01
[root@localhost ~]# curl http://10.10.30.60
server02
[root@localhost ~]# curl http://10.10.30.60
server01
[root@localhost ~]# curl http://10.10.30.60
server02
[root@localhost ~]# curl http://10.10.30.60
server01
[root@localhost ~]# curl http://10.10.30.60
server02
[root@localhost ~]# curl http://10.10.30.60
server01
[root@localhost ~]# curl http://10.10.30.60
server02
[root@localhost ~]# curl http://10.10.30.60

查看轮询记录

回到LVS上面查看轮询记录

[root@localhost ~]# ipvsadm -Lnc
IPVS connection entries
pro expire state       source             virtual            destination
TCP 00:04  FIN_WAIT    10.10.30.4:47876   10.10.30.60:80     10.10.30.2:80   
TCP 00:07  FIN_WAIT    10.10.30.4:47888   10.10.30.60:80     10.10.30.2:80
TCP 00:07  FIN_WAIT    10.10.30.4:47886   10.10.30.60:80     10.10.30.3:80
TCP 00:05  FIN_WAIT    10.10.30.4:47878   10.10.30.60:80     10.10.30.3:80
TCP 00:08  FIN_WAIT    10.10.30.4:47890   10.10.30.60:80     10.10.30.3:80
TCP 00:06  FIN_WAIT    10.10.30.4:47884   10.10.30.60:80     10.10.30.2:80   #.4是新开启的测试机
TCP 14:58  ESTABLISHED 10.10.30.100:1705  10.10.30.60:80     10.10.30.2:80   #.100是我的宿主机
TCP 00:05  FIN_WAIT    10.10.30.4:47880   10.10.30.60:80     10.10.30.2:80
TCP 00:06  FIN_WAIT    10.10.30.4:47882   10.10.30.60:80     10.10.30.3:80
TCP 14:54  ESTABLISHED 10.10.30.100:1706  10.10.30.60:80     10.10.30.3:80
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值