实验环境:基于centos6.6
haproxy-Server:172.16.249.98 hostname:node1
upsteram server1:172.16.249.100 hostname:node2
upstream server2:172.16.249.99 hostname:node3
web服务的负载均衡以及状态监控:
设置记录haproxy日志的文件位置:
node1:#vim/etc/rsyslog.conf
(1)启用UDP:
#ProvidesUDPsyslogreception
$ModLoadimudp
$UDPServerRun514
(2)添加记录日志的文件位置
#Savebootmessagesalsotoboot.log
local7.*/var/log/boot.log
local2.*/var/log/haproxy.log
#servicersyslogrestart
node1:#yuminstallhaproxy-y
#cd/etc/haproxy/
#cphaproxy.cfg{,.bak}
#vimhaproxy.cfg#---------------------------------------------------------------------
#Globalsettings
#---------------------------------------------------------------------
global
#tohavethesemessagesendupin/var/log/haproxy.logyouwill
#needto:
#
#1)configuresyslogtoacceptnetworklogevents.Thisisdone
#byaddingthe‘-r‘optiontotheSYSLOGD_OPTIONSin
#/etc/sysconfig/syslog
#
#2)configurelocal2eventstogotothe/var/log/haproxy.log
#file.Alinelikethefollowingcanbeaddedto
#/etc/sysconfig/syslog
#
#local2.*/var/log/haproxy.log
#
log127.0.0.1local2
chroot/var/lib/haproxy
pidfile/var/run/haproxy.pid
maxconn4000
userhaproxy
grouphaproxy
daemon
defaults
modehttp
logglobal
optionhttplog
optiondontlognull
optionhttp-server-close
optionforwardforexcept127.0.0.0/8
optionredispatch
retries3
timeouthttp-request10s
timeoutqueue1m
timeoutconnect10s
timeoutclient1m
timeoutserver1m
timeouthttp-keep-alive10s
timeoutcheck10s
maxconn30000
listenstats#此段为设置状态监控
modehttp
bind:8888#生产环境中为保证安全性,为设置特殊端口,例如:8888
statsenable
statshide-version
statsuri/haproxyadmin?stats
statsrealmHaproxy\Statistics
statsauthadmin:admin#设置用户名以及密码
statsadminifTRUE
frontendhttp-in
bind*:80
modehttp
logglobal
optionhttpclose
optionlogasap
optiondontlognull
capturerequestheaderHostlen20
capturerequestheaderRefererlen60
default_backendservers
frontendhealthcheck
bind:1099
modehttp
optionhttpclose
optionforwardfor
default_backendservers
backendservers
balanceroundrobin
servernode2172.16.249.100:80checkmaxconn2000
servernode3172.16.249.99:80checkmaxconn2000#serivcehaproxystart
node2,node3节点分别建立测试页面,并启动httpd服务;node2:#vim/var/www/html/index.html
httpdonnode2
#servicehttpdstart
node3:#vim/var/www/html/index.html
httpdonnode3
#servicehttpdstart
打开浏览器:172.16.249.98:
打开浏览器:172.16.249.98:8888/haproxy?stats
动静分离配置:
#vim /etc/haproxy/haproxy.cfg#---------------------------------------------------------------------
global
#tohavethesemessagesendupin/var/log/haproxy.logyouwill
#needto:
#
#1)configuresyslogtoacceptnetworklogevents.Thisisdone
#byaddingthe‘-r‘optiontotheSYSLOGD_OPTIONSin
#/etc/sysconfig/syslog
#
#2)configurelocal2eventstogotothe/var/log/haproxy.log
#file.Alinelikethefollowingcanbeaddedto
#/etc/sysconfig/syslog
#
#local2.*/var/log/haproxy.log
#
log127.0.0.1local2
chroot/var/lib/haproxy
pidfile/var/run/haproxy.pid
maxconn4000
userhaproxy
grouphaproxy
daemon
#turnonstatsunixsocket
statssocket/var/lib/haproxy/stats
defaults
modehttp
logglobal
optionhttplog
optiondontlognull
optionhttp-server-close
optionforwardforexcept127.0.0.0/8
optionredispatch
retries3
timeouthttp-request10s
timeoutqueue1m
timeoutconnect10s
timeoutclient1m
timeoutserver1m
timeouthttp-keep-alive10s
timeoutcheck10s
maxconn3000
frontendmain*:80
aclurl_staticpath_beg-i/static/images/javascript/stylesheets
aclurl_staticpath_end-i.jpg.gif.png.css.js.html.txt.htm
use_backendstaticifurl_static
default_backendapp
backendstatic
balanceroundrobin
servernode2172.16.249.100:80check
backendapp
balanceroundrobin
servernode3172.16.249.99:80check#serivcehaproxystart#启动haproxy服务
所有静态页面请求代理至node2节点;所有动态页面请求代理至node3节点。node2:#vim/var/www/html/index.html
httpdonnode2
#servicehttpdstart
node3:#vim/var/www/html/index.php
phpinfo();
?>
#servicehttpdstart
打开浏览器:172.16.249.98/index.html
打开浏览器:172.16.249.98/index.php
负载均衡mysql服务:
node1:#yuminstallmysql-y
#vim/etc/haproxy/haproxy.cfg#---------------------------------------------------------------------
global
#tohavethesemessagesendupin/var/log/haproxy.logyouwill
#needto:
#
#1)configuresyslogtoacceptnetworklogevents.Thisisdone
#byaddingthe‘-r‘optiontotheSYSLOGD_OPTIONSin
#/etc/sysconfig/syslog
#
#2)configurelocal2eventstogotothe/var/log/haproxy.log
#file.Alinelikethefollowingcanbeaddedto
#/etc/sysconfig/syslog
#
#local2.*/var/log/haproxy.log
#
log127.0.0.1local2
chroot/var/lib/haproxy
pidfile/var/run/haproxy.pid
maxconn4000
userhaproxy
grouphaproxy
daemon
#turnonstatsunixsocket
statssocket/var/lib/haproxy/stats
defaults
modetcp
logglobal
optiondontlognull
optionhttp-server-close
#optionforwardforexcept127.0.0.0/8
optionredispatch
retries3
timeouthttp-request10s
timeoutqueue1m
timeoutconnect10s
timeoutclient1m
timeoutserver1m
timeouthttp-keep-alive10s
timeoutcheck10s
maxconn3000
frontendbd
bind:3306
default_backenddbsrvs
backenddbsrvs
balanceleastconn
servernode2172.16.249.100:3306check
servernode3172.16.249.99:3306check
listenstats
bind:80
modehttp
statsenable
statsuri/haproxy?stats
statsrealmhaproxy\stats
statsauthadmin:admin
statsadminifTRUE
分别在node2、node3节点建立数据库,并授权可远程连接:
node2:#yuminstallmysql-server-y
#serivcemysqldstart
#mysql
node3:#yuminstallmysql-server-y
#serivcemysqldstart
#mysql
node1:#servicehaproxyrestart#重启服务,配置文件才能生效
#mysql-uroot-h172.16.249.98-pmageedu
检测:
node2:#mysql-uroot-pmageedu
node3:#mysql-uroot-pmageedu;
说明:由于我们在node1节点调用了数据库并创建了表,node2节点查看有此表,node3节点无此表,因此我们刚才的测试中调用了node2节点的mysql数据库而非node3节点。
原文:http://9612520./9602520/1601566