NAT模式的数据走向:client–>vs–>rs–>vs–>clent(原路返回)
1.在server1上加一块网卡eth1,给该网卡加上外网ip,激活网卡
[root@server1 ~]# ip addr add 172.25.254.100/24 dev eth1
[root@server1 ~]# ip link set up eth1
2.在server2和server3上添加网关,开启服务,因为NAT模式下数据包的走向是原路返回,要带着返回的数据包经过调度器回到客户端。
[root@server2 ~]# route add default gw 172.25.63.1
[root@server2 ~]# /etc/init.d/httpd start
[root@server3 ~]# route add default gw 172.25.63.1
[root@server3 ~]# /etc/init.d/httpd start
3.在server1中打开内核路由功能
[root@server1 ~]# sysctl -a | grep ip_forward
net.ipv4.ip_forward = 0
[root@server1 ~]# vim /etc/sysctl.conf
net.ipv4.ip_forward = 1
4.重启网络
[root@server1 ~]# /etc/init.d/network restart
5.查看内核路由功能已被打开
[root@server1 ~]# sysctl -a | grep ip_forward
net.ipv4.ip_forward = 1
为什么开启内核路由功能?
因为server1收到客户端请求后,得到一个公网的IP,跟目的的主机server2和server3不再一个网段,所以如果想要将客户端发来的数据包转发给后端服务器,那么必须对这个数据包进行ip转换,再进行数据包转发。
6.在server1上添加调度策略 -m:NAT 模式
[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:80 -m
[root@server1 ~]# ipvsadm -a -t 172.25.254.100:80 -r 172.25.63.3:80 -m
7.测试:curl 172.25.254.100
[root@foundation63 ~]# curl 172.25.254.100
<h1>server3 - www.westos.org</h1>
[root@foundation63 ~]# curl 172.25.254.100
<h1>server2.example.com</h1>
[root@foundation63 ~]# curl 172.25.254.100
<h1>server3 - www.westos.org</h1>
[root@foundation63 ~]# curl 172.25.254.100
<h1>server2.example.com</h1>
[root@foundation63 ~]# curl 172.25.254.100
<h1>server3 - www.westos.org</h1>
查看调度次数
[root@server1 ~]# ipvsadm -ln