LVS实现--LVS/NAT模式实现

狂风中的少年 提交于 2020-02-22 05:09:40

LVS/NAT原理和特点 :https://blog.csdn.net/qq_35887546/article/details/104425264

1.实验准备

本次实验需要三个虚拟机以及物理机:

虚拟机名称 作用 IP
server1 DS 172.25.63.1(内网) ,172.25.254.100(外网)
server2 RS1 172.25.63.2
server3 RS2 172.25.63.3

VIP为:172.25.254.100
测试服务:Http 端口:80
物理机为客户端

server2与server3安装apache,在默认发布目录/var/www/html下编写发布文件

2.配置DS

server1 配置好lvsadm

首先删除之前TUN模式添加的策略,及DR模式时添加的隧道:

[root@server1 ~]# ipvsadm -C
[root@server1 ~]# ipvsadm -l
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn

将之前实验的 ldirectord 和 keepalived 服务停止:

[root@server1 ~]# systemctl stop keepalived
[root@server1 ~]# systemctl stop ldirectord

删除之前设置的VIP和隧道接口:

[root@server1 ~]# modprobe -r ipip
[root@server1 ~]# ip addr del 172.25.63.100/32 dev eth0
[root@server1 ~]# ip addr show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
    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 qlen 1000
    link/ether 52:54:00:1b:f6:56 brd ff:ff:ff:ff:ff:ff
    inet 172.25.63.1/24 brd 172.25.63.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::5054:ff:fe1b:f656/64 scope link 
       valid_lft forever preferred_lft forever

2.为DS添加网卡

添加网卡,给新网卡添加外网ip:172.25.254.100,并激活网卡
在这里插入图片描述设置ip:

[root@server1 ~]# ip addr add 172.25.63.100/24 dev eth1
[root@server1 ~]# ip addr show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
    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 qlen 1000
    link/ether 52:54:00:1b:f6:56 brd ff:ff:ff:ff:ff:ff
    inet 172.25.63.1/24 brd 172.25.63.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::5054:ff:fe1b:f656/64 scope link 
       valid_lft forever preferred_lft forever
4: eth1: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN qlen 1000
    link/ether 52:54:00:5a:47:c7 brd ff:ff:ff:ff:ff:ff
    inet 172.25.63.100/24 scope global eth1
       valid_lft forever preferred_lft forever

激活网卡:

[root@server1 ~]# ip link set up eth1
[root@server1 ~]# ip addr show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
    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 qlen 1000
    link/ether 52:54:00:1b:f6:56 brd ff:ff:ff:ff:ff:ff
    inet 172.25.63.1/24 brd 172.25.63.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::5054:ff:fe1b:f656/64 scope link 
       valid_lft forever preferred_lft forever
4: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 52:54:00:5a:47:c7 brd ff:ff:ff:ff:ff:ff
    inet 172.25.63.100/24 scope global eth1
       valid_lft forever preferred_lft forever
    inet6 fe80::5054:ff:fe5a:47c7/64 scope link 
       valid_lft forever preferred_lft forever

3.为DS添加NAT模式策略

在server1:

NAT模式为-m

[root@server1 ~]# ipvsadm -A -t 172.25.254.100:80 -s rr
[root@server1 ~]# ipvsadm -a -t 172.25.254.100:80 -r 172.25.63.2 -m
[root@server1 ~]# ipvsadm -a -t 172.25.254.100:80 -r 172.25.63.3 -m
[root@server1 ~]# ipvsadm -l
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  server1:http rr
  -> server2:http                 Masq    1      0          0         
  -> server3:http                 Masq    1      0          0   

4.为DS开启路由机制
在server1:

永久开启:

[root@server1 ~]# vim /etc/sysctl.conf 
填入:
net.ipv4.ip_forward = 1
[root@server1 ~]# sysctl -p				#使更改生效
net.ipv4.ip_forward = 1

临时开启:

sysctl   -a |   grep   ip_forward

sysctl   -w  net.ipv4.ip_forward=1

sysctl   -p

5.为DS加载nat模块
在server1:

[root@server1 ~]# modprobe iptable_nat

注:如果不加载此模块,也可以在第一次访问时成功,但是会在再次访问时出现延迟过长,或访问超时现象

6.配置RS

在server2与server3:

先删除server2 与server3 中的tun 模块(以server 2为例,server3 也是同样的操作):

[root@server2 ~]# modprobe -r ipip
[root@server2 ~]# ip addr show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
    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 qlen 1000
    link/ether 52:54:00:fb:99:44 brd ff:ff:ff:ff:ff:ff
    inet 172.25.63.2/24 brd 172.25.63.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::5054:ff:fefb:9944/64 scope link 
       valid_lft forever preferred_lft forever

给网卡添加网关172.25.63.1(以server 2为例,server3 也是同样的操作):
永久添加:

[root@server2 ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0 
[root@server2 ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0 
BOOTPROTO=static
DEVICE=eth0
ONBOOT=yes
IPADDR=172.25.63.2
PREFIX=24
GATEWAY=172.25.63.1
[root@server2 ~]# systemctl restart network				#重启网络
[root@server2 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         172.25.63.1     0.0.0.0         UG    0      0        0 eth0				#表示添加成功
169.254.0.0     0.0.0.0         255.255.0.0     U     1002   0        0 eth0
172.25.63.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0

临时添加

route   add   default  gw   172.25.63.1          #删除网关:route   del   default  gw   172.25.8.1

7.测试

在客户端:

[root@foundation63 ~]# curl 172.25.254.100
server3
[root@foundation63 ~]# curl 172.25.254.100
server2
[root@foundation63 ~]# curl 172.25.254.100
server3
[root@foundation63 ~]# curl 172.25.254.100
server2
[root@foundation63 ~]# curl 172.25.254.100
server3

表示nat模式实现成功

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!