HAProxy简介
它是免费、快速并且可靠的一种解决方案,也是一款实现负载均衡的调度器,主要是做负载均衡的7层,也可以做4层负载均衡,使用于那些负载特大的web站点,这些站点通常又需要会话保持或七层处理,能提供高可用性、负载均衡以及基于TCP和HTTP应用的代理。
HAProxy工作模式
- mode http
- 客户端请求被深度分析后再发往服务器
- mode tcp
- 4层调度,不检查第七层信息
- mode health
- 仅做健康状态检查,已经不建议使用
HAProxy配置实例
HAProxy配置文件解析
- 配置文件可由如下部分构成:
- default:为后续的其他部分设置缺省参数,缺省参数可以被后续部分重置
- listen:定义服务器集群
- 配置文件说明
- /etc/haproxy/haproxy.cfg
global
log 127.0.0.1 local2
chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid ###haproxy的pid存放路径
maxconn 4000 ###最大连接数,默认4000
user haproxy
group haproxy
daemon ###将进程放入deamon模式运行
- /etc/haproxy/haproxy.cfg
defaults
mode http ###默认的模式mode{tcp|http|health}
log global ###采用全局定义的日志
option httplog ## 日志类别http日志格式
option dontlognull ###不记录健康检查的日志信息
option http-server-close ##每次请求完毕后主动关闭http通道
option forwardfor except 127.0.0.0/8
option redispatch ###serverid服务器挂掉后强制定向到其他健康服务器
retries 3 ###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 ###最大连接数
- /etc/haproxy/haproxy.cfg
listen stats 0.0.0.0:1080 #:监听端口
stats refresh 30s #:统计页面自动刷新时间
stats uri/stats #:统计页面url
stats realm Haproxy Manager #:进入管理界面查看状态信息
stats auth admin:admin #:统计页面用户名和密码设置
#stats hide-version #:隐藏统计页面上HAProxy的版本信息
- /etc/haproxy/haproxy.cfg
listen myweb 0.0.0.0:80 # 定义本机监听地址
balance roundrobin # 调度算法为轮询
server web1 192.168.4.100 check inter 2000 rise 2 fall 5
server web2 192.168.4.200 check inter 2000 rise 2 fall 5
# 对web服务器做健康检查,2秒检查一次,如果连续2次检查成功,认为服务器是健康的,如果连续5次检查失败,认为服务器坏了
管理服务
- 启动服务
[root@GeiBai ~]# systemctl start haproxy
- 查看服务
[root@GeiBai ~]# systemctl status haproxy
- 停止服务
[root@GeiBai ~]# systemctl stop haproxy
配置haproxy拓扑
- 环境准备:
- client1:eth0 -> 192.168.4.10
- HAProxy:eth0 -> 192.168.4.5
- web1:eth0 -> 192.168.4.100
- web2:eth0 -> 192.168.4.200
- 初始化配置
# 清理192.168.4.5
[root@lvs1 ~]# yum remove -y ipvsadm keepalived
[root@lvs1 ~]# hostnamectl set-hostname haproxy1
# web服务器,不需要配置vip,不需要改内核参数。但是存在对haproxy也没有影响。
- 配置haproxy
# 装包
[root@haproxy1 ~]# yum install -y haproxy
[root@haproxy1 ~]# vim /etc/haproxy/haproxy.cfg
# 配置文件中,global是全局配置;default是缺省配置,如果后续有和default相同的配置,default配置将会被覆盖。
# 配置文件中,frontend描述haproxy怎么和用户交互;backend描述haproxy怎么和后台应用服务器交互。这两个选项,一般不单独使用,而是合并到一起,名为listen。
# 将61行之后全部删除,写入以下内容
61 listen myweb 0.0.0.0:80 # 定义本机监听地址
62 balance roundrobin # 调度算法为轮询
# 对web服务器做健康检查,2秒检查一次,如果连续2次检查成功,认为服务器是健康的,如果连续5次检查失败,认为服务器坏了
63 server web1 192.168.4.100 check inter 2000 rise 2 fall 5
64 server web2 192.168.4.200 check inter 2000 rise 2 fall 5
65
66 listen stats 0.0.0.0:1080 # 定义监控地址
67 stats refresh 30s # 设置监控页面自动刷新时间为30秒
68 stats uri /stats # 定义监控地址是/stats
69 stats auth admin:admin # 监控页面的用户名和密码都是admin
# 启服务
[root@haproxy1 ~]# systemctl start haproxy.service
# 使用firefox访问监控地址 http://192.168.4.5:1080/stats
# 客户端访问测试
[root@client1 ~]# for i in {1..6}; do curl http://192.168.4.5/; done
192.168.2.100
apache web server2
192.168.2.100
apache web server2
192.168.2.100
apache web server2
监控HAProxy状态
- 使用firefox访问监控地址 http://192.168.4.5:1080/stats
页面详细参数解释
Queue
Cur: current queued requests //当前的队列请求数量
Max:max queued requests //最大的队列请求数量
Limit: //队列限制数量
Session rate(每秒的连接回话)列表:
scur: current sessions //每秒的当前回话的限制数量
smax: max sessions //每秒的新的最大的回话量
slim: sessions limit //每秒的新回话的限制数量
Sessions
Total: //总共回话量
Cur: //当前的回话
Max: //最大回话
Limit: //回话限制
Lbtot: total number of times a server was selected //选中一台服务器所用的总时间
Bytes
In: //网络的字节数输入总量
Out: //网络的字节数输出总量
Denied
Req: denied requests//拒绝请求量
Resp:denied responses //拒绝回应
Errors
Req:request errors //错误请求
Conn:connection errors //错误的连接
Resp: response errors (among which srv_abrt) ///错误的回应
Warnings
Retr: retries (warning) //重新尝试
Redis:redispatches (warning) //再次发送
Server列表:
Status:状态,包括up(后端机活动)和down(后端机挂掉)两种状态
LastChk: 持续检查后端服务器的时间
Wght: (weight) : 权重
Act: server is active (server), number of active servers (backend) //活动链接数量
Bck: server is backup (server), number of backup servers (backend) //backup:备份的服务器数量
Down: //后端服务器连接后都是down的数量
Downtime: downtime: total downtime (in seconds) //总的downtime 时间
Throttle: warm up status //设备变热状态