本文详细介绍在CentOS 7系统上安装、配置HAProxy负载均衡器,并设置开机自启动的完整流程。
文章目录
什么是HAProxy?
HAProxy(High Availability Proxy)是一个高性能的开源负载均衡器和反向代理软件,广泛应用于现代Web架构中。它能够将网络流量智能分发到多个后端服务器,提高系统的可用性、可靠性和性能。
主要特性
-
高性能:事件驱动架构,单进程可处理10Gbps流量
-
多协议支持:HTTP/1.1、HTTP/2、TCP、SSL/TLS终止
-
智能健康检查:自动监控后端服务器状态
-
多种负载均衡算法:轮询、最小连接数、源IP哈希等
-
会话保持:确保用户会话一致性
-
详细统计信息:实时监控负载均衡状态
一、安装HAProxy
1.1 更新系统包
sudo yum update -y
1.2 安装HAProxy
sudo yum install haproxy -y
1.3 验证安装
haproxy -v
输出应显示HAProxy版本信息,确认安装成功。
HA-Proxy version 1.5.18 2016/05/10
Copyright 2000-2016 Willy Tarreau <willy@haproxy.org>
二、配置HAProxy
2.1 主要配置文件
HAProxy的主配置文件位于 /etc/haproxy/haproxy.cfg。
2.2 编辑配置文件
sudo vi /etc/haproxy/haproxy.cfg
2.3 完整配置示例
global
log 127.0.0.1 local2
chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
maxconn 4000
user haproxy
group haproxy
daemon
stats socket /var/lib/haproxy/stats
defaults
mode http
log global
option httplog
option dontlognull
option http-server-close
option forwardfor except 127.0.0.0/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
# 监控统计页面配置
listen stats
bind *:8080
mode http
stats enable
stats uri /haproxy?stats
stats realm Haproxy\ Statistics
stats auth admin:your_password # 请修改密码
stats refresh 30s
# 前端服务配置 - 接收客户端请求
frontend web_frontend
bind *:80
mode http
option forwardfor
default_backend app_servers
# 后端服务器池配置
backend app_servers
balance roundrobin
option httpchk GET / HTTP/1.0
server web1 192.168.1.10:80 check inter 2000 rise 2 fall 3
server web2 192.168.1.11:80 check inter 2000 rise 2 fall 3
server web3 192.168.1.12:80 check inter 2000 rise 2 fall 3
# TCP负载均衡示例(如MySQL)
listen m

1699

被折叠的 条评论
为什么被折叠?



