Haproxy、Keepalived双主高可用负载均衡

在测试了Nginx+Keepalived的负载均衡后,也对Haproxy+Keepalived双主模式做了测试,双主的模式充分利用了服务器资源,这样不会导致服务器浪费。这里举例说明:

默认情况下,第一台负载均衡器主要分发www.zhirs.com的请求,第二台负载均衡器主要分发img.zhirs.com的请求。

任意一台宕机都不会影响网站分发。

实验环境

VIP1=192.168.137.200(www.zhirs.com)
VIP2=192.168.137.100(img.zhirs.com)
haproxy01=192.168.137.254
haproxy02=192.168.137.253
web1=192.168.137.201
web2=192.168.137.202
img1=192.168.158.101
img2=192.168.158.102

架构图

安装配置过程

1、安装配置过程,(两台Haproxy的安装配置步骤一样)

haproxy下载地址:http://haproxy.1wt.eu/download/1.4/src/haproxy-1.4.24.tar.gz

#cd/usr/local/src
#wgethttp://haproxy.1wt.eu/download/1.4/src/haproxy-1.4.24.tar.gz
#tarxfhaproxy-1.4.24.tar.gz
#cdhaproxy-1.4.24
#makeTARGET=linux26ARCH=x86_64

#TARGET是指定内核版本,ARCH指定CPU架构,我使用的是64bit系统

#makeinstall

2、安装完毕后,创建配置文件和启动文件。

#mkdir/etc/haproxy
#cpexamples/haproxy.cfg/etc/haproxy
#cpexamples/haproxy.init/etc/init.d/haproxy
#chmod+x/etc/init.d/haproxy
#ln-s/usr/local/sbin/haproxy/usr/sbin/
#mkdir/usr/share/haproxy

3、编辑配置文件(两台Haproxy配置文件相同)

#vim/etc/haproxy/haproxy.cfg
#thisconfigneedshaproxy-1.1.28orhaproxy-1.2.1
global
log127.0.0.1local0#日志输出配置,所有日志都记录在本机,通过local0输出
log127.0.0.1local1notice
#logloghostlocal0info
maxconn4096#最大连接数
chroot/usr/share/haproxy#改变当前工作目录。
uid99#所属用户的uid
gid99#所属运行的gid
daemon#以后台形式运行haproxy
#debug
#quiet
defaults
logglobal
modehttp
#默认的模式mode{tcp|http|health},tcp是4层,http是7层,health只会返回OK
optionhttplog
optiondontlognull
optionredispatch
#当serverId对应的服务器挂掉后,强制定向到其他健康的服务器
optionabortonclose
#当服务器负载很高的时候,自动结束掉当前队列处理比较久的链接
retries3#两次连接失败就认为是服务器不可用
maxconn2000#默认的最大连接数
#timeouthttp-keep-alive10s
#timeoutqueue1m
contimeout5000#连接超时
clitimeout50000#客户端超时
srvtimeout50000#服务器超时
timeoutcheck5s#心跳检测超时
statsrefresh30s#统计页面自动刷新时间
statsuri/stats#统计页面url
statsrealmbaison-test-Haproxy#统计页面密码框上提示文本
statsauthadmin:admin123#统计页面用户名和密码设置
statshide-version#隐藏统计页面上HAProxy的版本信息
frontendwww
bind*:80

#这里建议使用bind*:80的方式,要不然做集群高可用的时候有问题,vip切换到其他机器就不能访问了。

aclwebhdr(host)-iwww.zhirs.com

#acl后面是规则名称,-i是要访问的域名,如果访问www.zhirs.com这个域名就分发到下面的webserver的作用域。

aclimghdr(host)-iimg.zhirs.com

#如果访问img.baison.com.cn就分发到imgserver这个作用域。

use_backendwebserverifweb
use_backendimgserverifimg
backendwebserver#webserver作用域
modehttp
balanceroundrobin

#banlanceroundrobin轮询,balancesource保存session值,支持static-rr,leastconn,first,uri等参数

optionhttpchk/index.html

#检测文件,如果分发到后台index.html访问不到就不再分发给它

serverweb01192.168.137.201:80checkinter2000fall3weight30
serverweb01192.168.137.202:80checkinter2000fall3weight20
serverweb01192.168.137.203:80checkinter2000fall3weight10
backendimgserver
modehttp
optionhttpchk/index.php
balanceroundrobin
serverimg01192.168.137.101:80checkinter2000fall3
serverimg02192.168.137.102:80checkinter2000fall3

4、启动Haproxy服务,查看状态。

#servicehaproxystart

5、安装配置keepalived(两台keepalived安装步骤一样)

#cd/root/software
#wgetftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.33.tar.gz
#tar-zxvfpcre-8.33.tar.gz
#cdpcre-8.33
#./configure
#make&&makeinstall
#wgethttp://nginx.org/download/nginx-1.2.9.tar.gz
#tar-zxvfnginx-1.2.9.tar.gz
#cdnginx-1.2.9
#./configure--prefix=/usr/local/nginx--with-http_ssl_module--with-http_sub_module--with-http_stub_status_module--with-http_gzip_static_module
#make&&makeinstall

6、第一台keepalived配置文件。第一台做192.168.137.200vip的master,192.168.137.100的backup。

#vim/etc/keepalived/keepalived.conf
global_defs{
notification_email{
admin@centos.bz
}
notification_email_fromkeepalived@domain.com
smtp_server127.0.0.1
smtp_connect_timeout30
router_idLVS_DEVEL
}
vrrp_scriptchk_http_port{
#script"/opt/nginx_pid.sh"
script"/opt/check_haproxy.sh"
interval2
weight2
}
vrrp_instanceVI_1{
stateMASTER############辅机为BACKUP
interfaceeth0
virtual_router_id51
mcast_src_ip192.168.137.254
priority100###########权值要比back高
advert_int1
authentication{
auth_typePASS
auth_pass1111
}
track_script{
chk_http_port###执行监控的服务
}
virtual_ipaddress{
192.168.137.200
}
}
vrrp_instanceVI_2{
stateBACKUP
interfaceeth0
virtual_router_id52
priority99
advert_int1
authentication{
auth_typePASS
auth_pass1111
}
virtual_ipaddress{
192.168.137.100
}
}

7、第二台keepalived的配置文件。

#vim/etc/keepalived/keepalived.conf
global_defs{
notification_email{
admin@centos.bz
}
notification_email_fromkeepalived@domain.com
smtp_server127.0.0.1
smtp_connect_timeout30
router_idLVS_DEVEL
}
vrrp_scriptchk_http_port{
#script"/opt/nginx_pid.sh"
script"/opt/check_haproxy.sh"
interval2
weight2
}
vrrp_instanceVI_1{
stateBACKUP
interfaceeth0
virtual_router_id51
mcast_src_ip192.168.137.253
priority99##########权值要比master低
advert_int1
authentication{
auth_typePASS
auth_pass1111
}
track_script{
chk_http_port
}
virtual_ipaddress{
192.168.137.200
}
}
vrrp_instanceVI_2{
stateMASTER
interfaceeth0
virtual_router_id52
priority100
advert_int1
authentication{
auth_typePASS
auth_pass1111
}
virtual_ipaddress{
192.168.137.100
}
}

8、检测脚本,为了防止haproxy服务关闭导致keepalived不自动切换。

#vim/opt/check_haproxy.sh
#!/bin/bash
if[$(ps-Chaproxy--no-header|wc-l)-eq0];then
/etc/init.d/haproxystart
fi
sleep2
if[$(ps-Chaproxy--no-header|wc-l)-eq0];then
/etc/init.d/keepalivedstop
fi

9、启动两台keepalived和Haproxy服务。

#servciehaproxystart
#servicekeepalivedstart

10、查看两台服务器的VIP查看

查看192.168.137.254:

2:eth0:<BROADCAST,MULTICAST,UP,LOWER_UP>mtu1500qdiscpfifo_fastqlen1000
link/ether00:0c:29:dc:01:3ebrdff:ff:ff:ff:ff:ff
inet192.168.137.254/24brd192.168.137.255scopeglobaleth0
inet192.168.137.200/32scopeglobaleth0

查看192.168.137.253:

2:eth0:<BROADCAST,MULTICAST,UP,LOWER_UP>mtu1500qdiscpfifo_fastqlen1000
link/ether00:0c:29:d5:e7:3dbrdff:ff:ff:ff:ff:ff
inet192.168.137.253/24brd192.168.137.255scopeglobaleth0
inet192.168.137.100/32scopeglobaleth0
inet6fe80::20c:29ff:fed5:e73d/64scopelink
valid_lftforeverpreferred_lftforever

11、测试,关闭任意一台keepalived,img和www域名访问正常。后续会对Nginx+Keepalived双主结果做测试。

相关推荐