一.基本规划负载均衡调度器 192.168.1.104 默认网关 192.168.1.1 ip别名 192.168.1.233realserver1 192.168.1.102 默认网关 192.168.1.1 ip别名 192.168.1.233realserver2 192.168.1.105 默认网关 192.168.1.1 ip别名 192.168.1.233二.安装必备软件ipvsadm三.负载均衡器配置转发配置:修改负载均衡器的/etc/sysctl.conf文件net.ipv4.ip_forward=1运行sysctl -p加载配置修改/proc/sys/net/ipv4/ip_forward,并将其内容改为1也可以用echo 1 > /proc/sys/net/ipv4/ip_forward 修改为负载均衡器配置别名ifconfig eth0:0 192.168.1.233 broadcast 192.168.1.233 netmask 255.255.255.0 up四.实际服务器配置回环接口#ifconfig lo:0 192.168.1.233 broadcast 192.168.1.233 netmask 255.255.255.255 up#route add -host 192.168.1.233 dev lo:0为防止实际服务器响应来自网络中针对IP别名的ARP广播,执行如下操作#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
制作启动脚本:
#!/bin/sh#set the Virtual IP AddressVIP=192.168.1.233/sbin/ifconfig eth0:0 $VIP broadcast $VIP netmask 255.255.255.0 up/sbin/route add -host $VIP dev eth0:0#off ARPecho "1">/proc/sys/net/ipv4/conf/lo/arp_ignoreecho "2">/proc/sys/net/ipv4/conf/lo/arp_announceecho "1">/proc/sys/net/ipv4/conf/all/arp_ignoreecho "2">/proc/sys/net/ipv4/conf/all/arp_announce# runsysctl -p#end
五.用ipvsadm配置调度服务器#ipvsadm -A -t 192.168.1.233:80 -s rr#ipvsadm -a -t 192.168.1.233:80 -r 192.168.1.105:80 -g#ipvsadm -a -t 192.168.1.233:80 -r 192.168.1.102:80 -g
保存配置
#ipvsadm --save>/etc/sysconfig/ipvsadm
或者可以增加一个开机启动脚本配置#vi lvs.sh#!/bin/shVIP=192.168.1.233RIP1=192.168.1.105RIP2=192.168.1.102Gw=192.168.1.1#set the virtual IP Address/sbin/ifconfig eth0:0 $VIP netmask 255.255.255.0 up/sbin/route add -host $VIP dev eth0:0#Clear IPVS table/sbin/ipvsadm -C#set LVS/sbin/ipvsadm -A -t $VIP:80 -s rr/sbin/ipvsadm -a -t $VIP:80 -r $RIP1:80 -g/sbin/ipvsadm -a -t $VIP:80 -r $RIP2:80 -g#Run LVS/sbin/ipvsadm#end
# chmod 755 lvs.sh
# echo "sh/root/lvs.sh" >>/etc/rc.d/rc.local