Centos7下使用Harpoxy负载均衡Apache

[attach]1485[/attach]  

什么是HAProxy

HAProxy是一个免费的,快速和可靠的解决方案提供高可用性、负载均衡,并为TCP代理和基于http的应用程序。特别适合高流量网站和高并发网站。  

环境说明

操作系统:Centos 7 主机分布:1 haproxy server, 3 client  IP & Hostname
192.168.0.50    server.rootworld.in 
192.168.0.51    web1.rootworld.in
192.168.0.52    web2.rootworld.in
192.168.0.53    web3.rootworld.in
1、在客户端机器上安装web服务器   WEB1
[root@web1 ~]# yum install httpd -y
[root@web1 ~]# systemctl enable httpd.service
[root@web1 ~]# systemctl start httpd.service 
WEB2
[root@web2 ~]# yum install httpd -y
[root@web2 ~]# systemctl enable httpd.service
[root@web2 ~]# systemctl start httpd.service
WEB3
[root@web3 ~]# yum install httpd -y
[root@web3 ~]# systemctl enable httpd.service
[root@web3 ~]# systemctl start httpd.service
2、检查您的web服务器工作正常。   3、安装HAProxy Server.
[root@server ~]# yum install haproxy openssl-devel -y 
 4、配置HAPorxy的主配置文件/etc/haproxy/haproxy.cfg,修改log日志位置
[root@server ~]# vim /etc/haproxy/haproxy.cfg 
[attach]1486[/attach]   5、配置启动UDP syslog reception 在/etc/rsyslog.conf配置文件中,并且把Haproxy日志单独放到/var/log目录下。
[root@server ~]# vim /etc/rsyslog.conf
$ModLoad imudp
$UDPServerRun 514
[attach]1487[/attach]   6、在/etc/rsyslog.d/目录下配置haproxy.conf文件配置单独的log文件
[root@server ~]# vim /etc/rsyslog.d/haproxy.conf
local2.*        /var/log/haproxy.log
[attach]1488[/attach]   7、重启syslog服务,更新配置
[root@server ~]# service rsyslog restart
8、按照需求配置/etc/haproxy/haproxy.cfg文件
[root@server ~]# vim /etc/haproxy/haproxy.cfg
9、为均衡器在/etc/haproxy/haproxy.cfg配置前端和后端
# Configuration for HTTP site

listen server 192.168.0.50:80
        bind *:80
        default_backend LBbackend


backend LBbackend
        mode http
        server web1 192.168.0.51:80 check
        server web1 192.168.0.52:80 check
        server web1 192.168.0.53:80 check
        stats enable
        stats hide-version
        stats uri /stats
        stats realm Haproxy\ Statistics
        stats auth admin:redhat
        balance roundrobin
        option httpchk
        option  httpclose
        option forwardfor
        cookie LB insert 
[attach]1489[/attach]   10、重启HAProxy服务并启用
[root@server ~]# systemctl enable haproxy.service
[root@server ~]# systemctl restart haproxy.service
[root@server ~]# systemctl status haproxy.service
[attach]1490[/attach]   11、验证HAProxy负载均衡器
http://192.168.0.50/
[attach]1493[/attach] [attach]1491[/attach] [attach]1492[/attach]   12、打开HAProxy监控服务面板
 http://192.168.0.50/stats
[attach]1494[/attach]

5 个评论

麻烦提供下 完整的配置文件,因为按照楼主搭建的教程并不成功,主要问题还是出现在配置文件上面
koyo

koyo 回复 shunconf

我给出的就是完整的你详细的报错是啥?
shunconf

shunconf 回复 koyo

● haproxy.service - HAProxy Load Balancer Loaded: loaded (/usr/lib/systemd/system/haproxy.service; disabled; vendor preset: disabled) Active: failed (Result: exit-code) since Thu 2018-03-15 14:42:05 EDT; 2s ago Process: 1388 ExecStart=/usr/sbin/haproxy-systemd-wrapper -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid $OPTIONS (code=exited, status=1/FAILURE) Main PID: 1388 (code=exited, status=1/FAILURE)
shunconf

shunconf 回复 koyo

● haproxy.service - HAProxy Load Balancer Loaded: loaded (/usr/lib/systemd/system/haproxy.service; disabled; vendor preset: disabled) Active: failed (Result: exit-code) since Thu 2018-03-15 15:17:27 EDT; 4min 57s ago Process: 1758 ExecStart=/usr/sbin/haproxy-systemd-wrapper -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid $OPTIONS (code=exited, status=1/FAILURE) Main PID: 1758 (code=exited, status=1/FAILURE) Mar 15 15:17:27 server systemd[1]: Started HAProxy Load Balancer. Mar 15 15:17:27 server systemd[1]: Starting HAProxy Load Balancer... Mar 15 15:17:27 server haproxy-systemd-wrapper[1758]: haproxy-systemd-wrapper: executing /usr/sbin/haproxy -f /et... -Ds Mar 15 15:17:27 server haproxy-systemd-wrapper[1758]: [ALERT] 073/151727 (1759) : Proxy 'main': unable to find re...pp'. Mar 15 15:17:27 server haproxy-systemd-wrapper[1758]: [WARNING] 073/151727 (1759) : parsing [/etc/haproxy/haproxy...ded. Mar 15 15:17:27 server haproxy-systemd-wrapper[1758]: [ALERT] 073/151727 (1759) : Fatal errors found in configuration. Mar 15 15:17:27 server systemd[1]: haproxy.service: main process exited, code=exited, status=1/FAILURE Mar 15 15:17:27 server haproxy-systemd-wrapper[1758]: haproxy-systemd-wrapper: exit, haproxy RC=1 Mar 15 15:17:27 server systemd[1]: Unit haproxy.service entered failed state. Mar 15 15:17:27 server systemd[1]: haproxy.service failed. Hint: Some lines were ellipsized, use -l to show in full.
完整的配置文件: #--------------------------------------------------------------------- # Example configuration for a possible web application. See the # full configuration options online. # # http://haproxy.1wt.eu/download/1.4/doc/configuration.txt # #--------------------------------------------------------------------- #--------------------------------------------------------------------- # Global settings #--------------------------------------------------------------------- global # to have these messages end up in /var/log/haproxy.log you will # need to: # # 1) configure syslog to accept network log events. This is done # by adding the '-r' option to the SYSLOGD_OPTIONS in # /etc/sysconfig/syslog # # 2) configure local2 events to go to the /var/log/haproxy.log # file. A line like the following can be added to # /etc/sysconfig/syslog # # local2.* /var/log/haproxy.log # log 127.0.0.1 local2 chroot /var/lib/haproxy pidfile /var/run/haproxy.pid maxconn 4000 user haproxy group haproxy daemon # turn on stats unix socket stats socket /var/lib/haproxy/stats #--------------------------------------------------------------------- # common defaults that all the 'listen' and 'backend' sections will # use if not designated in their block #--------------------------------------------------------------------- defaults mode http log global option httplog option dontlognull option http-server-close option forwardfor except 192.168.2.135/8 option redispatch retries 3 timeout http-request 10s timeout queue 1m timeout connect 10s timeout client 1m timeout server 1m timeout http-keep-alive 10s timeout check 10s maxconn 3000 #--------------------------------------------------------------------- # main frontend which proxys to the backends #--------------------------------------------------------------------- frontend main *:5000 acl url_static path_beg -i /static /images /javascript /stylesheets acl url_static path_end -i .jpg .gif .png .css .js use_backend static if url_static default_backend app #--------------------------------------------------------------------- # static backend for serving up images, stylesheets and such #--------------------------------------------------------------------- backend static balance roundrobin server static 127.0.0.1:4331 check #--------------------------------------------------------------------- # round robin balancing between the various backends #--------------------------------------------------------------------- listen server 192.168.134.135:80 bind *:80 default_backend LBbackend backend LBbackend mode http server web1 192.168.2.134:80 check server web2 192.168.2.133:80 check stats enable stats hide-version stats uri /stats stats realm Haproxy\ Statistics stats auth admin:redhat balance roundrobin option httpchk option httpclose option forwardfor cookie LB insert

要回复文章请先登录注册