本文介绍使用lVS实现负载均衡的NAT模式实验。
1,实验环境
负载均衡器: eth0:192.168.1.16 eth1:10.10.10.128 vip: 192.168.1.17
后端WEB1:10.10.10.129
后端WEB2:10.10.10.130
2,安装ipvsadm
3,创建lvs nat模式启动脚本
[root@localhost ~]# vi /etc/init.d/lvsnat
#!/bin/bash
VIP=192.168.1.17
RIP1=10.10.10.129
RIP2=10.10.10.130
case "$1" in
start)
echo "start LVS of DirectorServer NAT"
echo "1" >/proc/sys/net/ipv4/ip_forward
/sbin/iptables -F
/sbin/ipvsadm -C
/sbin/ifconfig eth0:0 $VIP netmask 255.255.255.0 up
/sbin/ipvsadm -A -t $VIP:80 -s rr ###rr为算法
/sbin/ipvsadm -a -t $VIP:80 -r $RIP1 -m ###-m指定工作模式为NAT
/sbin/ipvsadm -a -t $VIP:80 -r $RIP2 -m
/sbin/ipvsadm
;;
stop)
echo "stop LVS of DirectorServer NAT"
echo "0" >/proc/sys/net/ipv4/ip_forward
/sbin/ipvsadm -C
/sbin/ifconfig eth0:0 down
;;
*)
echo "Usage: $0 {start|stop}"
exit 1
esac
[root@localhost ~]# chown +x /etc/init.d/lvsnat
[root@localhost init.d]# /etc/init.d/lvsnat start
start LVS of DirectorServer NAT
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.1.17:http rr
-> 10.10.10.130:http Masq 1 0 0
-> 10.10.10.129:http Masq 1 0 0
4,web1,web2修改网关为lvs的内网ip
5,web1,web2分别修改首页,启动http服务
6,终端及WEB测试
[root@localhost ~]# for i in $(seq 10);do curl http://192.168.1.17;done
web2 10.10.10.130
web1 10.10.10.129
web2 10.10.10.130
web1 10.10.10.129
web2 10.10.10.130
web1 10.10.10.129
web2 10.10.10.130
web1 10.10.10.129
web2 10.10.10.130
web1 10.10.10.129
[root@localhost ~]# 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.1.17:80 rr
-> 10.10.10.130:80 Masq 1 0 5
-> 10.10.10.129:80 Masq 1 0 5