参考 http://it165.net/admin/html/201401/2248.html
前期准备
需求:三台服务器一台作为director, 两台作为real server
Director 有一个外网ip (192.168.1.190)用桥接模式 和一个内网ip(192.168.11.190)NAT模式,
两个real server上只有内网ip(192.168.11.160)NAT模式和(192.168.11.20)NAT模式
并且需要把两个real server的内网网关设置为director的内网ip(192.168.11.190)
开始实验
两个real server 上都安装httpd: yum install -y nginx
Director上安装ipvsadm: yum install -y ipvsadm nginx
Direcotr 上 vim /usr/local/sbin/lvs_nat.sh //增加:
#! /bin/bash # director 服务器上开启路由转发功能: echo 1 > /proc/sys/net/ipv4/ip_forward # 关闭icmp的重定向 echo 0 > /proc/sys/net/ipv4/conf/all/send_redirects echo 0 > /proc/sys/net/ipv4/conf/default/send_redirects echo 0 > /proc/sys/net/ipv4/conf/eth0/send_redirects echo 0 > /proc/sys/net/ipv4/conf/eth1/send_redirects # director 设置nat防火墙 iptables -t nat -F iptables -t nat -X iptables -t nat -A POSTROUTING -s 192.168.11.0/24 -j MASQUERADE # director设置ipvsadm IPVSADM='/sbin/ipvsadm' $IPVSADM -C $IPVSADM -A -t 192.168.1.190:80 -s rr $IPVSADM -a -t 192.168.1.190:80 -r 192.168.11.160:80 -m $IPVSADM -a -t 192.168.1.190:80 -r 192.168.11.20:80 -m
直接运行这个脚本就可以完成lvs/nat的配置了:
sh /usr/local/sbin/lvs_nat.sh
通过浏览器测试两台机器上的web内容,为了区分开,我们可以把nginx的默认页修改一下:
rs1上: echo "rs1rs1" >/usr/share/nginx/html/index.html
rs2上: echo "rs2rs2" >/usr/share/nginx/html/index.html
测试一:
[root@dir ~]# curl 192.168.1.190 rs1rs1 [root@dir ~]# curl 192.168.1.190 rs2rs2 [root@dir ~]# curl 192.168.1.190 rs1rs1 [root@dir ~]# curl 192.168.1.190 rs2rs2 [root@dir ~]# curl 192.168.1.190 rs1rs1 [root@dir ~]# curl 192.168.1.190 rs2rs2
测试二:
vim /usr/local/sbin/lvs_nat.sh
sh /usr/local/sbin/lvs_nat.sh
[root@dir ~]# curl 192.168.1.190 rs1rs1 [root@dir ~]# curl 192.168.1.190 rs1rs1 [root@dir ~]# curl 192.168.1.190 rs2rs2 [root@dir ~]# curl 192.168.1.190 rs1rs1 [root@dir ~]# curl 192.168.1.190 rs1rs1 [root@dir ~]# curl 192.168.1.190 rs2rs2