Centos6下haproxy+keepalived构建高可用web集群

1)实验拓扑信息

wKioL1gYoFjzQr9jAAEd09D24eo089.png

说明:

a、在客户端使用www.wanwan.com,将由负载均衡器提交给vip1所对应的集群进行处理

b、在客户端使用img.wanwan.com,将由负载均衡器提交给vip1所对应的集群进行处理

c、10.10.10.129和10.10.10.130两台负载均衡器,其中某一台出现故障均不影响整个系统运行


2)haproxy的安装与启动脚本配置

参考我的上一篇博客http://www.jb51.cc/article/p-sytmwgiq-xe.html


3)haproxy的配置(两台负载均衡器10.10.10.129/10.10.10.130均需配置)

[root@MysqL-master~]#adduserhaproxy-s/sbin/nologin-M
[root@MysqL-master~]#cd/usr/local/haproxy/conf/
[root@MysqL-masterconf]#cathaproxy.cfg
global
log127.0.0.1local0info
maxconn4096
userhaproxy
grouphaproxy
daemon
nbproc1
pidfile/usr/local/haproxy/logs/haproxy.pid
defaults
modehttp
retries3
timeoutconnect10s
timeoutclient20s
timeoutserver30s
timeoutcheck5s
frontendwww
bind*:80
modehttp
optionhttplog
optionforwardfor
optionhttpclose
logglobal
#来自www.wanwan.com的请求,均交给htmpool进行处理,来自img.wanwan.com的请求,则提交给imgpool进行处理。认不指定的话,交给htmpool进行处理
aclhost_wwwhdr_dom(host)-iwww.wanwan.com
aclhost_imghdr_dom(host)-iimg.wanwan.com
use_backendhtmpoolifhost_www
use_backendimgpoolifhost_img
default_backendhtmpool
backendhtmpool
modehttp
optionredispatch
optionabortonclose
balancestatic-rr
cookieSERVERID
optionhttpchkGET/index.html
serverweb0110.10.10.128:80cookieserver1weight6checkinter2000rise2fall3
serverweb0210.10.10.132:80cookieserver2weight6checkinter2000rise2fall3
backendimgpool
modehttp
optionredispatch
optionabortonclose
balancestatic-rr
cookieSERVERID
optionhttpchkGET/index.html
serverimg110.10.10.131:80cookieserver1weight6checkinter2000rise2fall3
serverimg210.10.10.133:80cookieserver2weight6checkinter2000rise2fall3
#配置haproxy的web监控界面
listenadmin_stats
bind0.0.0.0:9188
modehttp
log127.0.0.1local0err
statsrefresh30s
statsuri/haproxy-status
statsrealmwelcomelogin\Haproxy
statsauthadmin:admin~!@
statshide-version
statsadminifTRUE

4)keepalived的配置

负载均衡器配置两个vip:10.10.10.188以及10.10.10.189(在两台负载均衡器上互为主备)

[root@MysqL-masterconf]#cat/etc/keepalived/keepalived.conf
!ConfigurationFileforkeepalived
global_defs{
notification_email{
314324506@qq.com
}
notification_email_fromAlexandre.Cassen@firewall.loc
smtp_serversmtp.qq.com
smtp_connect_timeout30
router_idLVS_7
}
#配置这个脚本的作用是为了避免haproxy服务停止后,keepalived不释放vip
vrrp_scriptchk_http_port{
script"/opt/check_haproxy.sh"
interval2
weight2
}
vrrp_instanceVI_188{
stateMASTER#主服务器
interfaceeth0
virtual_router_id188
priority150#slave上的数值更小,数值越大,代表优先级越高
advert_int1
authentication{
auth_typePASS
auth_pass1111
}
virtual_ipaddress{
10.10.10.188/24#vip地址,在系统里面通过ipaddlist可以查看
}
}
vrrp_instanceVI_189{
stateBACKUP#从服务器
interfaceeth0
virtual_router_id189
priority100#master上的数值更大,数值越大,代表优先级越高
advert_int1
authentication{
auth_typePASS
auth_pass1111
}
virtual_ipaddress{
10.10.10.189/24#vip地址,在系统里面通过ipaddlist可以查看
}
}

5)keepalived的启动脚本

#!/bin/sh
#
#StartupscriptfortheKeepaliveddaemon
#
#processname:keepalived
#pidfile:/var/run/keepalived.pid
#config:/etc/keepalived/keepalived.conf
#chkconfig:-2179
#description:StartandstopKeepalived
#Sourcefunctionlibrary
./etc/rc.d/init.d/functions
#Sourceconfigurationfile(wesetKEEPALIVED_OPTIONSthere)
./etc/sysconfig/keepalived
RETVAL=0
prog="keepalived"
start(){
echo-n$"Starting$prog:"
daemonkeepalived${KEEPALIVED_OPTIONS}
RETVAL=$?
echo
[$RETVAL-eq0]&&touch/var/lock/subsys/$prog
}
stop(){
echo-n$"Stopping$prog:"
killprockeepalived
RETVAL=$?
echo
[$RETVAL-eq0]&&rm-f/var/lock/subsys/$prog
}
reload(){
echo-n$"Reloading$prog:"
killprockeepalived-1
RETVAL=$?
echo
}
#Seehowwewerecalled.
case"$1"in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
stop
start
;;
condrestart)
if[-f/var/lock/subsys/$prog];then
stop
start
fi
;;
status)
statuskeepalived
;;
*)
echo"Usage:$0{start|stop|reload|restart|condrestart|status}"
exit1
esac
exit$RETVAL

6)效果测试

a、在客户端测试www.wanwan.com

wKiom1gZJxWwuWznAAA8Ah9SF5I775.png-wh_50

wKioL1gZJxWB5XzGAABBmmVmU_o912.png-wh_50

b、测试img.wanwan.com

wKiom1gZJxaS9BRsAAA-Tvd59Gw368.png-wh_50

wKioL1gZJxbRSm5vAAA534ZI-Jg155.png-wh_50

如上:负载均衡调度的作用已经实现,那么我们在测试下keepalived的相关功能

c、测试keepalived的相关功能

[root@MysqL-master~]#ipaddlist
1:lo:<LOOPBACK,UP,LOWER_UP>mtu65536qdiscnoqueuestateUNKNowN
link/loopback00:00:00:00:00:00brd00:00:00:00:00:00
inet127.0.0.1/8scopehostlo
inet6::1/128scopehost
valid_lftforeverpreferred_lftforever
2:eth0:<broADCAST,MULTICAST,LOWER_UP>mtu1500qdiscpfifo_faststateUPqlen1000
link/ether00:0c:29:67:b3:45brdff:ff:ff:ff:ff:ff
inet10.10.10.129/24brd10.10.10.255scopeglobaleth0
inet10.10.10.188/24scopeglobalsecondaryeth0
inet6fe80::20c:29ff:fe67:b345/64scopelink
valid_lftforeverpreferred_lftforever
[root@MysqL-slave~]#ipaddlist
1:lo:<LOOPBACK,LOWER_UP>mtu1500qdiscpfifo_faststateUPqlen1000
link/ether00:0c:29:53:cf:52brdff:ff:ff:ff:ff:ff
inet10.10.10.130/24brd10.10.10.255scopeglobaleth0
inet10.10.10.189/24scopeglobalsecondaryeth0
inet6fe80::20c:29ff:fe53:cf52/64scopelink
valid_lftforeverpreferred_lftforever

我们可以看到,两个vip地址,10.10.10.188以及10.10.10.189分别在两台负载均衡器上(仔细看下keepalived的配置可以发现,我们配置两台负载均衡互为主从)

模拟10.10.10.129-主负载均衡器宕机,然后观察ip地址切换以及负载均衡是否正常

[root@MysqL-slave~]#ipaddlist
1:lo:<LOOPBACK,LOWER_UP>mtu1500qdiscpfifo_faststateUPqlen1000
link/ether00:0c:29:53:cf:52brdff:ff:ff:ff:ff:ff
inet10.10.10.130/24brd10.10.10.255scopeglobaleth0
inet10.10.10.189/24scopeglobalsecondaryeth0
inet10.10.10.188/24scopeglobalsecondaryeth0
inet6fe80::20c:29ff:fe53:cf52/64scopelink
valid_lftforeverpreferred_lftforever
如上,我们可以观察到vip已经切换到另外一台负载均衡上了,然后我们观察负载均衡器的使用情况

wKiom1gZLVejz0BsAAA8v6e5pao217.png-wh_50

wKioL1gZLVeBRc95AAA-x-HVzFI895.png-wh_50

负载正常,keepalived的功能也实现了,我们重新开启主负载均衡器
[root@MysqL-masterkeepalived]#ipaddlist
1:lo:<LOOPBACK,LOWER_UP>mtu1500qdiscpfifo_faststateUPqlen1000
link/ether00:0c:29:67:b3:45brdff:ff:ff:ff:ff:ff
inet10.10.10.129/24brd10.10.10.255scopeglobaleth0
inet10.10.10.188/24scopeglobalsecondaryeth0
inet6fe80::20c:29ff:fe67:b345/64scopelink
valid_lftforeverpreferred_lftforever
vip地址10.10.10.188已经切换回来了
[root@MysqL-slave~]#ipaddlist
1:lo:<LOOPBACK,LOWER_UP>mtu1500qdiscpfifo_faststateUPqlen1000
link/ether00:0c:29:53:cf:52brdff:ff:ff:ff:ff:ff
inet10.10.10.130/24brd10.10.10.255scopeglobaleth0
inet10.10.10.189/24scopeglobalsecondaryeth0
inet6fe80::20c:29ff:fe53:cf52/64scopelink
valid_lftforeverpreferred_lftforever

到此,keepalived+haproxy的基本功能已经实现了,由于haproxy配置比较多,这里我就不细讲了,后面会整理一篇关于haproxy常用的参数配置。

相关文章

Centos下搭建性能监控Spotlight
CentOS 6.3下Strongswan搭建IPSec VPN
在CentOS6.5上安装Skype与QQ
阿里云基于centos6.5主机VPN配置
CentOS 6.3下配置multipah
CentOS安装、配置APR和tomcat-native