案例1:基于NAT的负载均衡
网卡配置这里就不在具体描述,在网络环境调通的情况下操作,具体的拓扑如图:
Director:10.10.10.133
| |
RS1: 10.10.10.131 RS2: 10.10.10.132
我在做实验的时候是采用yum的方式进行安装的LVS管理工具ipvsadm,yum安装非常简单这里也不在赘述,这里写一下源码的方式安装
#解压源码包,在下载源码包时注意内核版本,下载对应的配置工具。
[root@CentOSsoft]# tar xvf ipvsadm-1.26.tar.gz
#可直接编译
[[email protected]]# make
#安装
[[email protected]]# make install
#确认ipvsadm安装成功
[[email protected]]# /sbin/ipvsadm -v
ipvsadm v1.262008/5/15 (compiled with popt and IPVS v1.2.1)
调度器配置
#启用路由转发功能
[root@ Director ~]# vim /etc/sysctl.conf
将net.ipv4.ip_forward 数值由0改成1,开启路由转发功能
退出文件执行[root@director ~]# sysctl �Cp 使内核参数生效
#清除ipvsadm 表
[root@ Director ~]# ipvsadm -C
#使用ipvsadm 安装LVS 服务
[root@ Director ~]# /sbin/ipvsadm -A �Ct 10.10.10.133:80
#增加第1台realserver
[root@ Director ~]# /sbin/ipvsadm -a -t 10.10.10.133:80-r 10.10.10.131:80 -m -w 1
#增加第2台realserver
[root@ Director ~]# /sbin/ipvsadm -a �Ct 10.10.10.133:80-r 10.10.10.132:80 -m -w 1
[root@ Director ~]# service ipvsadm save
[root@ Director ~]# service ipvsadm start
真实服务器的设置 (两台服务器配置相同)
设置IP转发 echo "0">/proc/sys/net/ipv4/ip_forward
避免arp广播问题
echo 1 >/proc/sys/net/ipv4/conf/tunl0/arp_ignore
echo 2 >/proc/sys/net/ipv4/conf/tunl0/arp_announce
echo 1 >/proc/sys/net/ipv4/conf/all/arp_ignore
echo 2 >/proc/sys/net/ipv4/conf/all/arp_announce
在两台服务器上都安装上http服务,分别在他们的index.html文件中写入各自的IP地址
最后在Director主机上进行测试
curlhttp:// 10.10.10.131
curlhttp:// 10.10.10.132
附:(一个Director端的配置脚本,可以直接使用)
#! /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 10.10.10.0/24 -j MASQUERADE
# director设置ipvsadm
IPVSADM='/sbin/ipvsadm'
$IPVSADM -C
$IPVSADM -A -t 10.10.10.133:80 -s lc -p 300 (-p 代表的是超时时间)
$IPVSADM -a -t 10.10.10.133:80 -r 10.10.10.131:80 -m -w 1
$IPVSADM -a -t 10.10.10.133:80 -r 10.10.10.132:80 -m -w 1
案例2:基于DR的负载均衡
三台机器:
director(eth0 10.10.10.133, vip eth0:0: 10.10.10.100)
real server1(eth0 rip: 10.10.10.131, vip lo:0: 10.10.10.100)
real server2(eth0 rip: 10.10.10.135, vip lo:0: 10.10.10.100)
Director 上 vim/usr/local/sbin/lvs_dr.sh //增加
#! /bin/bash
echo 1 > /proc/sys/net/ipv4/ip_forward
ipv=/sbin/ipvsadm
vip=10.10.10.100
rs1=10.10.10.131
rs2=10.10.10.135
ifconfig eth0:0 $vip broadcast $vip netmask 255.255.255.255 up
route add -host $vip dev eth0:0
$ipv -C
$ipv -A -t $vip:80 -s rr
$ipv -a -t $vip:80 -r $rs1:80 -g -w 1
$ipv -a -t $vip:80 -r $rs2:80 -g -w 1
两台rs上:vim /usr/local/sbin/lvs_dr_rs.sh
#! /bin/bash
vip=10.10.10.100
ifconfig lo:0 $vip broadcast $vip netmask 255.255.255.255 up
route add -host $vip lo:0
echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce
关于arp_ignore和 arp_announce 参考:http://www.cnblogs.com/lgfeng/archive/2012/10/16/2726308.html
然后director上执行: bash /usr/local/sbin/lvs_dr.sh
两台rs上执行: bash /usr/local/sbin/lvs_dr_rs.sh
Windows下浏览器测试访问
案例3:基于tun的负载均衡
VIP=10.10.10.100
Director配置
[root@director ~]# ipvsadm -C
[root@director ~]# ipvsadm -A -t 10.10.10.100:80 -s rr
[root@director ~]# ipvsadm -a -t 10.10.10.100:80 -r10.10.10.131:80 -i -w 1
[root@director ~]# ipvsadm -a -t 10.10.10.100:80 -r10.10.10.135:80 -i -w 1
[root@director ~]# service ipvsadm save
[root@director ~]# service ipvsadm start
[root@director ~]# echo "1" >/proc/sys/net/ipv4/ip_forward
[root@director ~]# ifconfig eth1:0 10.10.10.100 netmask255.255.255.0 up
RealServer配置
Vim /usr/local/sbin/tun.sh
#!/bin/bash
if [ $# -ne1 ];
then
echo "usage: $0 virtualIP"
exit 0
fi
VIP=$1
echo"0" >/proc/sys/net/ipv4/ip_forward
/sbin/ifconfigeth0 mtu 1440
/sbin/ifconfigtunl0 up
/sbin/ifconfigtunl0 ${VIP} broadcast ${VIP} netmask 0xffffffff up
/sbin/route add-host ${VIP} dev tunl0
echo 1 >/proc/sys/net/ipv4/conf/tunl0/arp_ignore
echo 2 >/proc/sys/net/ipv4/conf/tunl0/arp_announce
echo 1 >/proc/sys/net/ipv4/conf/all/arp_ignore
echo 2 >/proc/sys/net/ipv4/conf/all/arp_announce
echo 0 >/proc/sys/net/ipv4/conf/tunl0/rp_filter
echo 0 >/proc/sys/net/ipv4/conf/all/rp_filter
[root@slave ~]# bash /usr/local/sbin/tun.sh 10.10.10.100
[root@master ~]# bash /usr/local/sbin/tun.sh 10.10.10.100
配置完成后通过IE浏览器访问http://10.10.10.100 刷新页面查看轮询效果