Centos7+Haproxy实现Apache负载均衡

前端之家收集整理的这篇文章主要介绍了Centos7+Haproxy实现Apache负载均衡前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

Centos7+Haproxy实现Apache负载均衡

说到Linux下的负载均衡,其实有很多服务是可以实现的,比如:haproxy,lvs,Nginx,这些服务都可以做负载均衡,而我们今天主要介绍的是Haproxy实现Apache的负载均衡。一般做法是通过Haproxy+Keeplive实现高可用负载均衡的,我们将会在后面的文章介绍。

目的:访问Haproxy自动轮询分配后台服务器

环境介绍:

Hostname:A-S

IP:192.168.5.21

Role:Apache

Hostname:B-S

IP:192.168.5.22

Role:Apache

Hostname:C-S

IP:192.168.5.23

Role:Haproxy

我们先看看安装完默认的Haproxy的配置介绍:

vim/etc/haproxy/haproxy.cfg
#---------------------------------------------------------------------
#Exampleconfigurationforapossiblewebapplication.Seethe
#fullconfigurationoptionsonline.
#
#http://haproxy.1wt.eu/download/1.4/doc/configuration.txt
#
#---------------------------------------------------------------------
#---------------------------------------------------------------------
#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#日志输出配置,所有日志都记录在本机,通过local2输出
chroot/var/lib/haproxy#改变当前工作目录。
pidfile/var/run/haproxy.pid#所属运行的gid
maxconn4000#最大连接数
userhaproxy
grouphaproxy
daemon#以后台形式运行haproxy
#turnonstatsunixsocket
statssocket/var/lib/haproxy/stats
#---------------------------------------------------------------------
#commondefaultsthatallthe'listen'and'backend'sectionswill
#useifnotdesignatedintheirblock
#---------------------------------------------------------------------
defaults
modehttp#默认的模式mode{tcp|http|health},tcp是4层,http是7层,health只会返回OK
logglobal
optionhttplog
optiondontlognull
optionhttp-server-close
optionforwardforexcept127.0.0.0/8
optionredispatch#当serverId对应的服务器挂掉后,强制定向到其他健康的服务器
retries3#两次连接失败就认为是服务器不可用
timeouthttp-request10s
timeoutqueue1m
timeoutconnect10s#连接超时
timeoutclient1m#客户端超时
timeoutserver1m#服务器超时
timeouthttp-keep-alive10s
timeoutcheck10s#心跳检测超时
maxconn3000#默认的最大连接数
#---------------------------------------------------------------------
#mainfrontendwhichproxystothebackends
#---------------------------------------------------------------------
frontendmain*:5000#haproxy服务器监听端口
aclurl_staticpath_beg-i/static/images/javascript/stylesheets
aclurl_staticpath_end-i.jpg.gif.png.css.js
use_backendstaticifurl_static
default_backendapp
#---------------------------------------------------------------------
#staticbackendforservingupimages,stylesheetsandsuch
#---------------------------------------------------------------------
backendstatic
balanceroundrobin
#banlanceroundrobin轮询,balancesource保存session值,支持static-rr,leastconn,first,uri等参数
serverstatic127.0.0.1:4331check
#---------------------------------------------------------------------
#roundrobinbalancingbetweenthevarIoUsbackends
#---------------------------------------------------------------------
backendapp
balanceroundrobin
serverapp1127.0.0.1:5001check
serverapp2127.0.0.1:5002check
serverapp3127.0.0.1:5003check
serverapp4127.0.0.1:5004check

接下来我们开始正式环境配置介绍:首先是安装Apache服务,然后配置显示页面

Yuminstallhttpd

clip_image001

安装完成

clip_image002

查看httpd 版本

clip_image003

接下来我们首先要为apache定义一个 默认的页面,方便区分

Vim/var/www/httml/index.html
</html>
<!DOCTYPEhtml>
<html>
<head>
<title>WelcometoApache</title>
<style>
body{
35em;
margin:0auto;
font-family:Tahoma,Verdana,Arial,sans-serif;
}
</style>
<styletype="text/css">
h1{color:red}
h2{color:blue}
h3{color:green}
h4{color:yellow}
}
</style>
</head><bodybgcolor='#46A3FF'>
<h1>WelcometoA-SApache</h1>
<h2>HostName:A-S</h2>
<h3>IP:192.168.5.21</h3>
<h4>Service:Apache</h4>
<inputtype=buttonvalue="Refresh"onclick="window.location.href('http://192.168.5.21')">
</body>
</html>

clip_image004

启动服务

Systemctlstarthttpd

clip_image005

然后添加默认的防火墙端口8o

Firewall-cmd--zone=public--add-port='80/tcp'--permanent

clip_image006

或者vim /etc/firewalld/zone/public.xml

添加一下格式

<portportocal='tcp'port='80'>

clip_image007

接下来我们在本地访问测试一下

clip_image008

接下来我们也同样按照方法部署第二台apache服务,方法跟上面完全一样

</html>
<!DOCTYPEhtml>
<html>
<head>
<title>WelcometoApache</title>
<style>
body{
35em;
margin:0auto;
font-family:Tahoma,sans-serif;
}
</style>
<styletype="text/css">
h1{color:red}
h2{color:blue}
h3{color:green}
h4{color:yellow}
}
</style>
</head><bodybgcolor='#CA8EFF'>
<h1>WelcometoB-SApache</h1>
<h2>HostName:B-S</h2>
<h3>IP:192.168.5.22</h3>
<h4>Service:Apache</h4>
<inputtype=buttonvalue="Refresh"onclick="window.location.href('http://192.168.5.22')">
</body>
</html>

clip_image009

测试访问

clip_image010

最后我们开始部署haproxy了

我们使用192.168.5.33来部署haproxy了

yuminstall-yhaproxy

clip_image011

clip_image012

我们可以编辑haproxy的默认配置文件

vim/etc/haproxy/haproxy.nfg

clip_image013

#---------------------------------------------------------------------
#Exampleconfigurationforapossiblewebapplication.Seethe
#fullconfigurationoptionsonline.
#
#http://haproxy.1wt.eu/download/1.4/doc/configuration.txt
#
#---------------------------------------------------------------------
#---------------------------------------------------------------------
#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
#turnonstatsunixsocket
statssocket/var/lib/haproxy/stats
#---------------------------------------------------------------------
#commondefaultsthatallthe'listen'and'backend'sectionswill
#useifnotdesignatedintheirblock
#---------------------------------------------------------------------
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
#---------------------------------------------------------------------
#mainfrontendwhichproxystothebackends
#---------------------------------------------------------------------
frontendmain*:5000
aclurl_staticpath_beg-i/static/images/javascript/stylesheets
aclurl_staticpath_end-i.jpg.gif.png.css.js
use_backendstaticifurl_static
default_backendapp
#---------------------------------------------------------------------
#staticbackendforservingupimages,stylesheetsandsuch
#---------------------------------------------------------------------
backendstatic
balanceroundrobin
serverstatic127.0.0.1:4331check
#---------------------------------------------------------------------
#roundrobinbalancingbetweenthevarIoUsbackends
#---------------------------------------------------------------------
backendapp
balanceroundrobin
serverapp1127.0.0.1:5001check
serverapp2127.0.0.1:5002check
serverapp3127.0.0.1:5003check
serverapp4127.0.0.1:5004check

我们在配置文件的最后面,将默认的端口从4331修改成80

然后添加服务器

更改前

clip_image014

更改后

#---------------------------------------------------------------------
#Exampleconfigurationforapossiblewebapplication.Seethe
#fullconfigurationoptionsonline.
#
#http://haproxy.1wt.eu/download/1.4/doc/configuration.txt
#
#---------------------------------------------------------------------
#---------------------------------------------------------------------
#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
#turnonstatsunixsocket
statssocket/var/lib/haproxy/stats
#---------------------------------------------------------------------
#commondefaultsthatallthe'listen'and'backend'sectionswill
#useifnotdesignatedintheirblock
#---------------------------------------------------------------------
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
#---------------------------------------------------------------------
#mainfrontendwhichproxystothebackends
#---------------------------------------------------------------------
frontendmain*:80
aclurl_staticpath_beg-i/static/images/javascript/stylesheets
aclurl_staticpath_end-i.jpg.gif.png.css.js
use_backendstaticifurl_static
default_backendapp
#---------------------------------------------------------------------
#staticbackendforservingupimages,stylesheetsandsuch
#---------------------------------------------------------------------
backendstatic
balanceroundrobin
serverstatic127.0.0.1:80check
#---------------------------------------------------------------------
#roundrobinbalancingbetweenthevarIoUsbackends
#---------------------------------------------------------------------
backendapp
balanceroundrobin
serverapp1127.0.0.1:5001check
serverapp2127.0.0.1:5002check
serverapp3127.0.0.1:5003check
serverapp4127.0.0.1:5004check
serverapp5192.168.5.21:80check
serverapp6192.168.5.22:80check

clip_image015

然后启动haproxy服务

systemctlstarthaproxy

clip_image016

然后测试访问haproxy访问

clip_image017

我们通过本地查看端口监听状态

netstat-anlpt

我们发现默认的端口是5000

clip_image018

我们使用5000端口在访问一下

clip_image019

clip_image020

接下来我们更改默认端口

vim/etc/haproxy/haproxy.cfg

clip_image021

修改为80

clip_image022

修改后我们重启

clip_image023

我们查看端口监听状态

clip_image024

测试访问

clip_image025

clip_image026

原文链接:https://www.f2er.com/centos/379428.html

猜你在找的CentOS相关文章