上善若水,不浮不沉!
分类: LINUX
2014-04-02 09:31:49
本文介绍使用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
复制代码 代码如下:[root@localhost ~]# mkdir /media/CentOS
[root@localhost ~]# mount /dev/cdrom /media/CentOS
[root@localhost ~]# yum -y --disablerepo=* --enablerepo=c5-media install ipvsadm
[root@localhost ~]# ipvsadm
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
[root@localhost ~]# lsmod |grep ip_vs
ip_vs 78081 0
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
复制代码 代码如下:[root@localhost ~]# route add default gw 10.10.10.128
[root@localhost ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
10.10.10.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth0
0.0.0.0 10.10.10.128 0.0.0.0 UG 0 0 0 eth0
5,web1,web2分别修改首页,启动http服务
复制代码 代码如下:[root@localhost ~]# vi /var/www/html/index.html
web1 10.10.10.129 ###web1
web2 10.10.10.130 ###web2
[root@localhost ~]# /etc/init.d/httpd start
6,终端及WEB测试
复制代码 代码如下:[root@localhost ~]# for i in $(seq 10);do curl
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
本文原始链接: