負(fù)載均衡服務(wù)器腳本配置
接觸負(fù)載均衡這個領(lǐng)域已經(jīng)有一段時間了,但是對于linux下的負(fù)載均衡服務(wù)器的設(shè)置問題總是掌握的不得要領(lǐng)。那么現(xiàn)在,特意找來了一篇資料,跟大家一起分享。共同看看具體的配置過程是如何的。那么,具體的內(nèi)容,我們一起來看文章吧。
兩臺resin 服務(wù)器和兩臺apache服務(wù)器、一臺負(fù)載均衡服務(wù)器,一共5臺server。
兩臺apache服務(wù)器ip地址:
apache1 ip:192.168.9.101
apache2 ip:192.168.9.110
兩臺resin 服務(wù)器ip地址:
resin1 ip:192.168.9.145
resin2 ip:192.168.9.146
一臺負(fù)載均衡服務(wù)器ip地址:
vip1:192.168.9.106 提供兩臺apache負(fù)載均衡的虛擬ip地址
vip2:192.168.9.150 提供兩臺resin負(fù)載均衡的虛擬ip地址
負(fù)載均衡服務(wù)器 ip地址192.168.9.109
apache1 lvs相關(guān)配置如下:
建一個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服務(wù)器lvs配置和apache1相同
resin1服務(wù)器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服務(wù)器lvs配置和resin1相同
負(fù)載均衡服務(wù)器配置
安裝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負(fù)載均衡服務(wù)器測試)
測試http://192.168.9.150 (兩臺resin負(fù)載均衡服務(wù)器測試)