接触负载均衡这个领域已经有一段时间了,但是对于linux下的负载均衡服务器的设置问题总是掌握的不得要领。那么现在,特意找来了一篇资料,跟大家一起分享。共同看看具体的配置过程是如何的。那么,具体的内容,我们一起来看文章吧。
两台resin 服务器和两台apache服务器、一台负载均衡服务器,一共5台server。
两台apache服务器ip地址:
apache1 ip:192.168.9.101
apache2 ip:192.168.9.110
两台resin 服务器ip地址:
resin1 ip:192.168.9.145
resin2 ip:192.168.9.146
一台负载均衡服务器ip地址:
vip1:192.168.9.106 提供两台apache负载均衡的虚拟ip地址
vip2:192.168.9.150 提供两台resin负载均衡的虚拟ip地址
负载均衡服务器 ip地址192.168.9.109
apache1 lvs相关配置如下:
建一个shell脚本vi /usr/local/lvs/real.sh
- #!/bin/bash
- # set the Virtual IP Address
- /sbin/ifconfig lo:0 192.168.9.106 broadcast 192.168.9.106 netmask 255.255.255.255 up
- /sbin/route add -host 192.168.9.106 dev lo:0
- #off 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
- # run
- sysctl -p
- #end
保存退出#p#
将此脚本添加自启动文件
- [root@web1 ~]# cat /etc/rc.local
- #!/bin/sh
- #
- # This script will be executed *after* all the other init scripts.
- # You can put your own initialization stuff in here if you don't
- # want to do the full Sys V style init stuff.
- /usr/local/lvs/real.sh
- touch /var/lock/subsys/local
apache2服务器lvs配置和apache1相同
resin1服务器lvs配置如下
建一个shell脚本 vi /usr/local/realserver/real.sh
- #!/bin/bash
- # set the Virtual IP Address
- /sbin/ifconfig lo:0 192.168.9.150 broadcast 192.168.9.150 netmask 255.255.255.255 up
- /sbin/route add -host 192.168.9.150 dev lo:0
- #off 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
- # run
- sysctl -p
- #end
#p#将此脚本添加自启动文件
- [root@resin1 ~]# cat /etc/rc.local
- #!/bin/sh
- #
- # This script will be executed *after* all the other init scripts.
- # You can put your own initialization stuff in here if you don't
- # want to do the full Sys V style init stuff.
- /usr/local/realserver/real.sh
- touch /var/lock/subsys/local
resin2服务器lvs配置和resin1相同
负载均衡服务器配置
安装libnet 、ipvsadm、ldirectord软件
yum install libnet
yum install ipvsadm
yum install ldirectord*
新建/usr/local/lvsdr/lvs.sh脚本
- vi /usr/local/lvsdr/lvs.sh
- VIP1=192.168.9.106
- VIP2=192.168.9.150
- RIP1=192.168.9.101
- RIP2=192.168.9.110
- RIP3=192.168.9.145
- RIP4=192.168.9.146
- GW=192.168.9.2
- #set the vritual IP Address
- /sbin/ifconfig eth0:0 $VIP1 broadcast $VIP1 netmask 255.255.255.255 up
- /sbin/route add -host $VIP1 dev eth0:0
- /sbin/ifconfig eth0:1 $VIP2 broadcast $VIP2 netmask 255.255.255.255 up
- /sbin/route add -host $VIP2 dev eth0:1
- #clear IPVS table
- /sbin/ipvsadm -C
- #Set LVS
- /sbin/ipvsadm -A -t $VIP1:80 -s rr
- /sbin/ipvsadm -a -t $VIP1:80 -r $RIP1:80 -g
- /sbin/ipvsadm -a -t $VIP1:80 -r $RIP2:80 -g
- /sbin/ipvsadm -A -t $VIP2:8080 -s wlc -p 120
- /sbin/ipvsadm -a -t $VIP2:8080 -r $RIP3:8080 -g
- /sbin/ipvsadm -a -t $VIP2:8080 -r $RIP4:8080-g
- #Run lvs
- /sbin/ipvsadm
- #end
#p#配置ldirectord
- [root@lvsdr ~]# cp /usr/share/doc/heartbeat-ldirectord-2.1.3/ldirectord.cf /etc/ha.d
- vi /etc/ha.d/ldirectord.cf
- # Global Directives
- checktimeout=3
- checkinterval=1
- #fallback=127.0.0.1:80
- autoreload=yes
- logfile="/var/log/ldirectord.log"
- #logfile="local0"
- #emailalert="admin@x.y.z"
- #emailalertfreq=3600
- #emailalertstatus=all
- quiescent=yes
- #VIP1=192.168.9.106 HTTP
- virtual=192.168.9.106:80
- real=192.168.9.101:80 gate
- real=192.168.9.110:80 gate
- #real=192.168.6.6:80 gate
- #fallback=127.0.0.1:80 gate
- service=http
- #request="index.html"
- #receive="Test Page"
- #virtualhost=some.domain.com.au
- scheduler=rr
- #persistent=600
- #netmask=255.255.255.255
- protocol=tcp
- #checktype=negotiate
- checkport=80
- #request="index.html"
- #receive="Test Page"
- #virtualhost=www.x.y.z
- #VIP2=192.168.9.150 RESIN
- virtual=192.168.9.150:8080
- real=192.168.9.145:8080 gate
- real=192.168.9.146:8080 gate
- #real=192.168.6.6:8080 gate
- #fallback=127.0.0.1:80 gate
- #service=http
- #request="index.html"
- #receive="Test Page"
- #virtualhost=some.domain.com.au
- scheduler=wlc
- persistent=600
- #netmask=255.255.255.255
- protocol=tcp
- checktype=connect
- checkport=8080
- #request="index.html"
- #receive="Test Page"
- #virtualhost=www.x.y.z
加入自启动
- [root@lvsdr ~]# cat /etc/rc.local
- #!/bin/sh
- #
- # This script will be executed *after* all the other init scripts.
- # You can put your own initialization stuff in here if you don't
- # want to do the full Sys V style init stuff.
- touch /var/lock/subsys/local
- #/usr/local/bin/lvsdr start
- /usr/local/lvsdr/lvs.sh
- /etc/init.d/ldirectord start
- #/etc/init.d/ipvsadm start
测试http://192.168.9.106 (两台apache负载均衡服务器测试)
测试http://192.168.9.150 (两台resin负载均衡服务器测试)