LVS/NAT 配置
三台服务器一台作为director, 两台作为real server
Director 有一个外网ip (192.168.31.166) 和一个内网ip(192.168.21.166), 两个real server上只有内网ip(192.168.21.100)和(192.168.21.101) 并且需要把两个real server的内网网关设置为director的内网ip(192.168.21.166)
vmware网络设置: http://www.apelearn.com/bbs/thread-10536-1-1.html
两个real server 上都安装httpd: yum install -y nginx
Director上安装ipvsadm yum install -y ipvsadm
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.21.0/24 -j MASQUERADE
# director设置ipvsadm
IPVSADM='/sbin/ipvsadm'
$IPVSADM -C
$IPVSADM -A -t 192.168.31.166:80 -s lc -p 300
$IPVSADM -a -t 192.168.31.166:80 -r 192.168.21.100:80 -m -w 1
$IPVSADM -a -t 192.168.31.166:80 -r 192.168.21.101:80 -m -w 1
直接运行这个脚本就可以完成lvs/nat的配置了:
/bin/bash /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
ipvsadm -c 清空规则
ipvsadm -ln 查看规则
iptables -t nat -F 清空防火墙规则
转载于:https://blog.51cto.com/xiongrc/1788147