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.169 192.168.246.161
-
- [httpd] [httpd]
- 192.168.246.162 192.168.246.163
-
- 一、Haproxy实施步骤
- 1. 准备工作(集群中所有主机)
- [root@ha-proxy-master ~]# cat /etc/hosts
- 127.0.0.1 localhost
- 192.168.246.169 ha-proxy-master
- 192.168.246.161 ha-proxy-slave
- 192.168.246.162 test-nginx1
- 192.168.246.163 test-nginx2
- 2. 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 .cfg
- global
- log 127.0.0.1 local2 info
- pidfile /var/run/haproxy.pid
- maxconn 4000 #优先级低
- user haproxy
- group haproxy
- daemon #以后台形式运行ha-proxy
- nbproc 1 #工作进程数量 cpu内核是几就写几
- defaults
- mode http #工作模式 http ,tcp 是 4 层,http是 7 层
- log global
- retries 3 #健康检查。3次连接失败就认为服务器不可用,主要通过后面的check检查
- option redispatch #服务不可用后重定向到其他健康服务器。
- maxconn 4000 #优先级中
- contimeout 5000 #ha服务器与后端服务器连接超时时间,单位毫秒ms
- clitimeout 50000 #客户端超时
- srvtimeout 50000 #后端服务器超时
- listen stats
- bind *:80
- stats enable
- stats uri /haproxy #使用浏览器访问 http://192.168.246.169/haproxy,可以看到服务器状态
- stats auth qianfeng:123 #用户认证,客户端使用elinks浏览器的时候不生效
- frontend web
- mode http
- bind *:80 #监听哪个ip和什么端口
- option httplog #日志类别 http 日志格式
- acl html url_reg -i \.html$ #1.访问控制列表名称html。规则要求访问以html结尾的url
- use_backend httpservers if html #2.如果满足acl html规则,则推送给后端服务器httpservers
- default_backend httpservers #默认使用的服务器组
- backend httpservers #名字要与上面的名字必须一样
- balance roundrobin #负载均衡的方式
- server http1 192.168.246.162:80 maxconn 2000 weight 1 check inter 1s rise 2 fall 2
- server http2 192.168.246.163:80 maxconn 2000 weight 1 check 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