ha-proxy概述
ha-proxy是一款高性能的负载均衡软件。因为其专注于负载均衡这一些事情,因此与nginx比起来在负载均衡这件事情上做更好,更专业。

ha-proxy的特点

•支持tcp / http 两种协议层的负载均衡,使得其负载均衡功能非常丰富。
•支持8种左右的负载均衡算法,尤其是在http模式时,有许多非常实在的负载均衡算法,适用各种需求。
•性能非常优秀,基于事件驱动的链接处理模式及单进程处理模式(和Nginx类似)让其性能卓越。
•拥有一个功能出色的监控页面,实时了解系统的当前状况。
•功能强大的ACL支持,给用户极大的方便。

拓扑结构[vip: 192.168.246.17] [LB1 Haproxy][LB2 Haproxy]192.168.246.169192.168.246.161[httpd] [httpd] 192.168.246.162192.168.246.163一、Haproxy实施步骤1. 准备工作(集群中所有主机)[root@ha-proxy-master ~]# cat /etc/hosts127.0.0.1localhost192.168.246.169ha-proxy-master192.168.246.161ha-proxy-slave192.168.246.162test-nginx1192.168.246.163test-nginx22. RS配置配置好网站服务器,测试所有RS,所有机器安装nginx[root@test-nginx1 ~]# yum install -y nginx[root@test-nginx1 ~]# systemctl start nginx[root@test-nginx1 ~]# echo "test-nginx1" >> /usr/share/nginx/html/index.html# 所有nginx服务器按顺序输入编号,方便区分。3. 调度器配置Haproxy(主/备)都执行[root@ha-proxy-master ~]# yum -y install haproxy[root@ha-proxy-master ~]# cp -rf /etc/haproxy/haproxy.cfg{,.bak}[root@ha-proxy-master ~]# sed -i.bak -r '/^[ ]*#/d;/^$/d' /etc/haproxy/haproxy.cfg[root@ha-proxy-master ~]# vim /etc/haproxy/haproxy.cfggloballog 127.0.0.1 local2 infopidfile /var/run/haproxy.pidmaxconn 4000 #优先级低userhaproxygroup haproxydaemon #以后台形式运行ha-proxynbproc 1#工作进程数量cpu内核是几就写几defaultsmodehttp#工作模式 http ,tcp 是 4 层,http是 7 层log globalretries 3 #健康检查。3次连接失败就认为服务器不可用,主要通过后面的check检查optionredispatch#服务不可用后重定向到其他健康服务器。maxconn 4000#优先级中contimeout5000#ha服务器与后端服务器连接超时时间,单位毫秒msclitimeout50000 #客户端超时srvtimeout50000 #后端服务器超时listen statsbind*:80stats enablestats uri/haproxy#使用浏览器访问 http://192.168.246.169/haproxy,可以看到服务器状态stats auth qianfeng:123#用户认证,客户端使用elinks浏览器的时候不生效frontendwebmode httpbind*:80 #监听哪个ip和什么端口optionhttplog#日志类别 http 日志格式acl html url_reg-i\.html$#1.访问控制列表名称html。规则要求访问以html结尾的urluse_backend httpservers ifhtml #2.如果满足acl html规则,则推送给后端服务器httpserversdefault_backendhttpservers #默认使用的服务器组backend httpservers#名字要与上面的名字必须一样balance roundrobin#负载均衡的方式serverhttp1 192.168.246.162:80 maxconn 2000 weight 1check inter 1s rise 2 fall 2serverhttp2 192.168.246.163:80 maxconn 2000 weight 1check inter 1s rise 2 fall 2将配置文件拷贝到slave服务器[root@ha-proxy-master ~]# scp/etc/haproxy/haproxy.cfg 192.168.246.161:/etc/haproxy/两台机器启动设置开机启动[root@ha-proxy-master ~]# systemctl start haproxy[root@ha-proxy-master ~]# systemctl enable haproxy

访问192.168.246.17刷新页面跳转便完成操作,访问haproxy查看监控

页面主要参数解释
Queue
Cur: current queued requests //当前的队列请求数量
Max:max queued requests //最大的队列请求数量
Limit: //队列限制数量

Errors
Req:request errors //错误请求
Conn:connection errors //错误的连接

Server列表:
Status:状态,包括up(后端机活动)和down(后端机挂掉)两种状态
LastChk: 持续检查后端服务器的时间
Wght: (weight) : 权重
========================================================
2.测试访问
通过访问haparoxy的ip地址访问到后端服务器
# curl http://192.168.246.169