接触负载均衡这个领域已经有一段时间了,但是对于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 #settheVirtualIPAddress /sbin/ifconfiglo:0192.168.9.106broadcast192.168.9.106netmask255.255.255.255up /sbin/routeadd-host192.168.9.106devlo:0 #offARP 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 # #Thisscriptwillbeexecuted*after*alltheotherinitscripts. #Youcanputyourowninitializationstuffinhereifyoudon't #wanttodothefullSysVstyleinitstuff. /usr/local/lvs/real.sh touch/var/lock/subsys/local
apache2服务器lvs配置和apache1相同
resin1服务器lvs配置如下
建一个shell脚本 vi /usr/local/realserver/real.sh
#!/bin/bash #settheVirtualIPAddress /sbin/ifconfiglo:0192.168.9.150broadcast192.168.9.150netmask255.255.255.255up /sbin/routeadd-host192.168.9.150devlo:0 #offARP 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 # #Thisscriptwillbeexecuted*after*alltheotherinitscripts. #Youcanputyourowninitializationstuffinhereifyoudon't #wanttodothefullSysVstyleinitstuff. /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 #setthevritualIPAddress /sbin/ifconfigeth0:0$VIP1broadcast$VIP1netmask255.255.255.255up /sbin/routeadd-host$VIP1deveth0:0 /sbin/ifconfigeth0:1$VIP2broadcast$VIP2netmask255.255.255.255up /sbin/routeadd-host$VIP2deveth0:1 #clearIPVStable /sbin/ipvsadm-C #SetLVS /sbin/ipvsadm-A-t$VIP1:80-srr /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-swlc-p120 /sbin/ipvsadm-a-t$VIP2:8080-r$RIP3:8080-g /sbin/ipvsadm-a-t$VIP2:8080-r$RIP4:8080-g #Runlvs /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 #GlobalDirectives 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.106HTTP virtual=192.168.9.106:80 real=192.168.9.101:80gate real=192.168.9.110:80gate #real=192.168.6.6:80gate #fallback=127.0.0.1:80gate service=http #request="index.html" #receive="TestPage" #virtualhost=some.domain.com.au scheduler=rr #persistent=600 #netmask=255.255.255.255 protocol=tcp #checktype=negotiate checkport=80 #request="index.html" #receive="TestPage" #virtualhost=www.x.y.z #VIP2=192.168.9.150RESIN virtual=192.168.9.150:8080 real=192.168.9.145:8080gate real=192.168.9.146:8080gate #real=192.168.6.6:8080gate #fallback=127.0.0.1:80gate #service=http #request="index.html" #receive="TestPage" #virtualhost=some.domain.com.au scheduler=wlc persistent=600 #netmask=255.255.255.255 protocol=tcp checktype=connect checkport=8080 #request="index.html" #receive="TestPage" #virtualhost=www.x.y.z
加入自启动
[root@lvsdr~]#cat/etc/rc.local #!/bin/sh # #Thisscriptwillbeexecuted*after*alltheotherinitscripts. #Youcanputyourowninitializationstuffinhereifyoudon't #wanttodothefullSysVstyleinitstuff. touch/var/lock/subsys/local #/usr/local/bin/lvsdrstart /usr/local/lvsdr/lvs.sh /etc/init.d/ldirectordstart #/etc/init.d/ipvsadmstart
测试http://192.168.9.106 (两台apache负载均衡服务器测试)
测试http://192.168.9.150 (两台resin负载均衡服务器测试)
转载请注明:IT运维空间 » 运维技术 » 负载均衡服务器脚本配置
发表评论