LVS的NAT模型实现

实验环境

  • 系统:centos 7 mini

LVS的NAT模型

实验步骤

client 配置

[root@client 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"


[root@client 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配置
[root@router 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配置
[root@router 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"

[root@router 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

  • 开启转发
[root@router 18:59:23 ~]#vi /etc/sysctl.conf

net.ipv4.ip_forward = 1

[root@router 18:59:37 ~]#sysctl -p
net.ipv4.ip_forward = 1

[root@router 19:00:03 ~]#cat /proc/sys/net/ipv4/ip_forward
1

webserver配置

webserver1配置

网络配置
[root@webserver1 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.100

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

[root@webserver1 19:08:53 ~]#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: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配置
[root@webserver1 19:08:53 ~]#yum -y install httpd

[root@webserver1 19:09:49 ~]#echo "RS1" > /var/www/html/index.html

[root@webserver1 19:10:12 ~]#cat /var/www/html/index.html
RS1

[root@webserver1 19:10:27 ~]#systemctl start httpd

webserver2配置

网络配置
[root@webserver2 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.100

DEFROUTE="yes"
NAME="eth0"
DEVICE="eth0"
ONBOOT="yes"
web配置
[root@webserver2 07:12:47 ~]#yum -y install httpd

[root@webserver2 07:13:32 ~]#echo "RS2" > /var/www/html/index.html/index

[root@webserver2 07:13:45 ~]#cat /var/www/html/index.html
RS2

[root@webserver2 07:14:06 ~]#systemctl start httpd

LVS配置

网络配置

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

IPADDR=192.168.38.27
NETMASK=255.225.255.0
GATEWAY=192.168.38.17

DEFROUTE="yes"
NAME="eth0"
DEVICE="eth0"
ONBOOT="yes"
  • eth1 网卡
[root@lvs 19:26:05 ~]#cat /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"
  • 开启转发
[root@lvs 19:28:43 ~]#vi /etc/sysctl.conf

net.ipv4.ip_forward = 1

[root@lvs 19:29:15 ~]#sysctl -p
net.ipv4.ip_forward = 1

[root@lvs 19:29:24 ~]#cat /proc/sys/net/ipv4/ip_forward
1
  • ipvsadm软件安装
[root@lvs 19:27:41 ~]#yum -y install ipvsadm
  • lvs配置
[root@lvs 20:13:48 ~]#ipvsadm -A -t 192.168.38.27:80 -s rr
[root@lvs 20:15:26 ~]#ipvsadm -a -t 192.168.38.27:80 -r 192.168.38.37:80 -m
[root@lvs 20:16:04 ~]#ipvsadm -a -t 192.168.38.27:80 -r 192.168.38.47:80 -m

[root@lvs 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

客户机测试

[root@client 20:24:27 ~]#while true;do curl 192.168.38.27;sleep 0.5;done
RS1
RS2
RS1
RS2
RS1
RS2
RS1
RS2
RS1
RS2
RS1
RS2
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值