LVS的DR模型实现

标签: LVS

实验环境

  • 系统:centos 7 mini

LVS的NAT模型

实验步骤

client 配置

[[email protected] 18:49:17 ~]#vi /etc/sysconfig/network-scripts/ifcfg-eth0

TYPE="Ethernet"
BOOTPROTO="static"

IPADDR=192.168.3.7
NETMASK=255.225.255.0
GATEWAY=192.168.3.17

DEFROUTE="yes"
NAME="eth0"
DEVICE="eth0"
ONBOOT="yes"


[[email protected] 18:49:21 ~]#ip a
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: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:75:c9:9a brd ff:ff:ff:ff:ff:ff
    inet 192.168.3.7/24 brd 192.168.3.255 scope global noprefixroute eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fe75:c99a/64 scope link
       valid_lft forever preferred_lft forever

router配置

网络配置

  • eth0配置
[[email protected] 18:54:51 ~]#vi /etc/sysconfig/network-scripts/ifcfg-eth0
TYPE="Ethernet"
BOOTPROTO="static"

IPADDR=192.168.38.17
NETMASK=255.225.255.0

DEFROUTE="yes"
NAME="eth0"
DEVICE="eth0"
ONBOOT="yes"    
  • eth1配置
[[email protected] 18:56:59 ~]#vi /etc/sysconfig/network-scripts/ifcfg-eth1
TYPE="Ethernet"
BOOTPROTO="static"

IPADDR=192.168.3.17
NETMASK=255.225.255.0

DEFROUTE="yes"
NAME="eth1"
DEVICE="eth1"
ONBOOT="yes"

[[email protected] 18:57:51 ~]#ip a
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: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:0a:53:ba brd ff:ff:ff:ff:ff:ff
    inet 192.168.38.17/24 brd 192.168.38.255 scope global noprefixroute eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fe0a:53ba/64 scope link
       valid_lft forever preferred_lft forever
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:0a:53:c4 brd ff:ff:ff:ff:ff:ff
    inet 192.168.3.17/24 brd 192.168.3.255 scope global noprefixroute eth1
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fe0a:53c4/64 scope link
       valid_lft forever preferred_lft forever

  • 开启转发
[[email protected] 18:59:23 ~]#vi /etc/sysctl.conf

net.ipv4.ip_forward = 1

[[email protected] 18:59:37 ~]#sysctl -p
net.ipv4.ip_forward = 1

webserver配置

webserver1配置

网络配置

  • 关闭arp相关参数
[[email protected] 21:32:49 ~]#vi /etc/sysctl.conf

# 不回应arp广播
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.lo.arp_ignore = 1

# 不公告拥有的地址(避免将接口信息向非本网络进行通告)
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.lo.arp_announce = 2

[[email protected] 21:32:49 ~]#sysctl -p
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.lo.arp_announce = 2

[[email protected] 19:06:33 ~]#vi /etc/sysconfig/network-scripts/ifcfg-eth0

TYPE="Ethernet"
BOOTPROTO="static"

IPADDR=192.168.38.37
NETMASK=255.225.255.0
GATEWAY=192.168.38.17

DEFROUTE="yes"
NAME="eth0"
DEVICE="eth0"
ONBOOT="yes"

[[email protected] 21:12:02 ~]#vi /etc/sysconfig/network-scripts/ifcfg-lo
DEVICE=lo
IPADDR1=127.0.0.1
NETMASK1=255.0.0.0

IPADDR2=192.168.38.27
NETMASK2=255.255.255.255

BROADCAST=127.255.255.255
ONBOOT=yes
NAME=loopback



[[email protected] 21:23:54 ~]#ip a
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
    inet 192.168.38.27/32 brd 192.168.38.27 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:da:5f:ec brd ff:ff:ff:ff:ff:ff
    inet 192.168.38.37/24 brd 192.168.38.255 scope global noprefixroute eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:feda:5fec/64 scope link
       valid_lft forever preferred_lft forever

web配置

[[email protected] 19:08:53 ~]#yum -y install httpd

[[email protected] 19:09:49 ~]#echo "RS1" > /var/www/html/index.html

[[email protected] 19:10:12 ~]#cat /var/www/html/index.html
RS1

[[email protected] 19:10:27 ~]#systemctl start httpd

webserver2配置

网络配置

  • 关闭arp相关参数
[[email protected] 21:32:49 ~]#vi /etc/sysctl.conf

# 不回应arp广播
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.lo.arp_ignore = 1

# 不公告拥有的地址(避免将接口信息向非本网络进行通告)
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.lo.arp_announce = 2

[[email protected] 21:32:49 ~]#sysctl -p
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.lo.arp_announce = 2

[[email protected] 07:11:36 ~]#vi /etc/sysconfig/network-scripts/ifcfg-eth0

TYPE="Ethernet"
BOOTPROTO="static"

IPADDR=192.168.38.47
NETMASK=255.225.255.0
GATEWAY=192.168.38.17

DEFROUTE="yes"
NAME="eth0"
DEVICE="eth0"
ONBOOT="yes"

[[email protected] 09:12:33 ~]#vi /etc/sysconfig/network-scripts/ifcfg-lo
DEVICE=lo
IPADDR1=127.0.0.1
NETMASK1=255.0.0.0

IPADDR2=192.168.38.27
NETMASK2=255.255.255.255

BROADCAST=127.255.255.255
ONBOOT=yes
NAME=loopback


[[email protected] 09:25:02 ~]#ip a
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
    inet 192.168.38.27/32 brd 192.168.38.27 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:d9:82:5e brd ff:ff:ff:ff:ff:ff
    inet 192.168.38.47/24 brd 192.168.38.255 scope global noprefixroute eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fed9:825e/64 scope link
       valid_lft forever preferred_lft forever

web配置

[[email protected] 07:12:47 ~]#yum -y install httpd

[[email protected] 07:13:32 ~]#echo "RS2" > /var/www/html/index.html

[[email protected] 07:13:45 ~]#cat /var/www/html/index.html
RS2

[[email protected] 07:14:06 ~]#systemctl start httpd

LVS配置

网络配置

  • eth0 网卡
[[email protected] 19:26:01 ~]#vi /etc/sysconfig/network-scripts/ifcfg-eth0
TYPE="Ethernet"
BOOTPROTO="static"

IPADDR=192.168.38.27
NETMASK=255.225.255.255

DEFROUTE="yes"
NAME="eth0"
DEVICE="eth0"
ONBOOT="yes"
  • eth1 网卡
[[email protected] 19:26:05 ~]#vi /etc/sysconfig/network-scripts/ifcfg-eth1
TYPE="Ethernet"
BOOTPROTO="static"

IPADDR=192.168.38.100
NETMASK=255.225.255.0
GATEWAY=192.168.38.17

DEFROUTE="yes"
NAME="eth1"
DEVICE="eth1"
ONBOOT="yes"
  • 开启转发
[[email protected] 19:28:43 ~]#vi /etc/sysctl.conf

net.ipv4.ip_forward = 1

[[email protected] 19:29:37 ~]#sysctl -p
net.ipv4.ip_forward = 1

  • ipvsadm软件安装
[[email protected] 19:27:41 ~]#yum -y install ipvsadm
  • lvs配置
[[email protected] 20:13:48 ~]#ipvsadm -A -t 192.168.38.27:80 -s rr
[[email protected] 20:15:26 ~]#ipvsadm -a -t 192.168.38.27:80 -r 192.168.38.37:80 -g -w 1
[[email protected] 20:16:04 ~]#ipvsadm -a -t 192.168.38.27:80 -r 192.168.38.47:80 -g -w 1

[[email protected] 20:16:11 ~]#ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  192.168.38.27:80 rr
  -> 192.168.38.37:80             Masq    1      0          0
  -> 192.168.38.47:80             Masq    1      0          0

客户机测试

[[email protected]_07 10:49:48 ~]#while true;do curl 192.168.38.27;sleep 0.5;done
RS1
RS2
RS1
RS2
RS1
RS2
RS1
RS2

注意事项

  • vip地址必须一致(LVS RS中的vip地址子网掩码在同一网段,可以设置为 255.255.255.255)

  • RS中必须在vip地址所在网卡设置

    net.ipv4.conf.all.arp_ignore = 1
    net.ipv4.conf.lo.arp_ignore = 1
    net.ipv4.conf.all.arp_announce = 2
    net.ipv4.conf.lo.arp_announce = 2

  • 必须事先配置好局域网yum源

  • 如果lvs与rs在同一网段,lvs中的vip可以绑定在lo回环网卡上,不在同一网段则不能绑定在lo上,可以绑定在其他网卡上

版权声明:本文为weixin_43520186原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_43520186/article/details/102683269