Linux系统负载均衡软件之haproxy+apache

Linux系统负载均衡软件之haproxy+apache,第1张

Linux系统负载均衡软件之haproxy+apache

Hproxy提供了基于TCP和HTTP应用的高可用性、负载均衡和反向代理,尤其适用于那些负载较重的网站,这些网站通常需要会话维护或七层处理。Hproxy运行在目前的硬件上,完全可以支持数万个并发连接,其运行模式使得集成到架构中变得简单安全。 同时可以保护你的web服务器不暴露在网络之下。

环境规划:

tong1:192.168.1.247haproxy

tong2:192.168.1.248web1

tong3:192.168.1.249web2

1.网络结构

Tong1节点:

[root@tong1~]#hostname
tong1
[root@tong1~]#ifconfig|grepMask
inetaddr:192.168.1.247Bcast:192.168.1.255Mask:255.255.255.0
inetaddr:127.0.0.1Mask:255.0.0
[root

192.168.1.247舌片1

[root@tong1~]#

Tong2节点被配置为tong3节点。

2.在tong1节点上安装haproxy软件,开启haproxy日志功能。

[root@tong1~]#yum安装haproxy-y

[root@tong1~]#vim/etc/sysconfig/rsyslog-add-r参数记录haproxy日志

SYSLOGD_OPTIONS="-r-c2"

[root@tong1~]#vim/etc/rsyslog.conf-定义存储日志的路径。

本地2。*/var/log/haproxy.log

[root@tong1~]#/etc/init.d/rsyslogrestart
关闭系统记录器:[OK]
启动系统记录器:[OK]
[root@tong1~]#

[root@tong1~]#CD/etc/haproxy/
[root@tong1haproxy]#vimhaproxy.CFG

全球的

日志127.0.0.1本地2-打开日志功能

chroot/var/lib/haproxy-运行路径
pidfile/var/run/haproxy.PID-存储PID文件的位置
maxconn4000-最大连接数
userhaproxy-正在运行的用户组
Groupproxy-正在运行的用户组
daemon-后台运行服务

defaults
modehttp-处理的模式(HTTP是七层,Tcp是第四层)
logglobal-启用全局日志记录
选项HTTPlog-日志类别HTTP格式
选项dontlognull-不记录健康检查日志
选项http-server-close
选项forwardforexcept127.0.0.0/8
选项redispatch-serverid对应的服务器被强制定向到其他

重试3次-服务器在3次连接失败后被定义为不可用
超时http-请求10s
超时队列1m
outconnect10s-服务器连接超时
超时客户端1m-客户端连接超时
超时服务器1m-服务器连接超时
超时http-保持活动10s-持久连接[/Br/]超时检查10s-心跳检查超时
maxconn3000

listenadmin_status-自定义监听器名称,选择任意
bind0.0.0:80-bindport
modehttp-mode
log127.0.0.1local3err-记录错误日志
stats刷新20s-每20s刷新一次。

stathide-version-隐藏haproxy版本信息

域名后的staturi/haproxy-stats-add/haproxy-stats,查看haproxy监控状态。

statauthhaproxy:system-用户名和密码stathide-version
statadmin如果为true-您可以手动启动和停止该服务。

listensite_status-检查后端主机的健康状况
bind0.0.0:80
modehttp
log127.0.0.1local3err
monitor-uri/site-stats-检查url地址

前端main_status-定义acl规则

绑定0.0.0.0:80-绑定到端口80

模式http

日志全局

选项httplog

选项转发给

aclweb1hdr_reg(主机)-I^(www.itnihao.cn|ww1.itnihao.cn)$

-匹配www.itnihao.cn和ww1.itnihao.cn这两个域名,并将它们放入web1变量中。

ACL2URL_sub-Ikillall=-如果请求中包含killall=,则将其放入web2变量中。

ACLweb3path_beg-I/static/p_w_picpaths/JavaScript/样式表

use_backendserver_web3ifweb3——满足web3变量的虚拟主机扔进server_web3。

default_backendserver_web4——如果它们不满意,就被扔进server_web4中的虚拟主机。

Backserver_web3
模式http
balanceroundrobin
选项httpchkget/test.html-定义主页地址

服务器ton1192.168.1.248:80检查inter1500上升3下降3重量1

-后端服务器,inter1500是检查心跳率,上升3次正确,下降3次失败不可用,权重1是权重。

服务器ton2192.168.1.249:80检查inter1500上升3下降3重量1

后端server_web4
模式http
balanceroundrobin
选项httpchkGET/index.html
serverton3192.168.1.248:80检查inter1500上升3下降3重量1
serverton4192.168.1.249:80检查inter1500上升3下降3重量1

[root@tong1haproxy]#/etc/init.d/haproxyrestart
停止haproxy:[OK]
启动haproxy:[OK]
[root@tong1haproxy]#

3.在后端主机上安装apache服务

Tong2节点:

[root@tong2~]#hostname
tong2

[root@tong2~]#yum安装httpd-y

[root@tong2~]#vim/etc/httpd/conf/httpd.conf

服务器名127.0.0.1

[root@tong2~]#echo'node2'>;/var/www/html/index.html

[root@tong2~]#echo'staticnode2'>;/var/www/html/test.html

[root@tong2~]#/etc/init.d/httpdrestart
停止httpd:[OK]
启动httpd:[OK]
[root@tong2~]#

Tong3节点:

[root@tong3~]#hostname
tong3
[root@tong3~]#yuminstallhttpd-y

[root@tong3~]#vim/etc/httpd/conf/httpd.conf

服务器名127.0.0.1

[root@tong3~]#echo'node3'>;/var/www/html/index.html

[root@tong3~]#echo'staticnode3'>;/var/www/html/test.html

[root@tong3~]#/etc/init.d/httpdrestart
停止httpd:[OK]
启动httpd:[OK]
[root@tong3~]#

4.检查haproxy的状态和监控

在后台管理和监控url页面

后端主机的监控页面

(1)后端主机处于正常状态。

(2)后端主机不正常,宕机。

正常接入节点

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zz/783857.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-04
下一篇 2022-05-04

发表评论

登录后才能评论

评论列表(0条)

保存