一、HAProxy概述
HAProxy是一款免费、快速且可靠的代理软件,提供高可用性、负载均衡,支持TCP和HTTP应用代理,HAProxy凭借其卓越的性能和灵活性,成为众多知名网站和系统的首选代理软件。
核心特点:
- 高性能:采用事件驱动模型,支持大量并发连接,具有低延迟和高吞吐量。
- 高可用性:能够自动检测后端服务器健康状态,实现故障切换,确保服务连续性。
- 灵活性:支持多种负载均衡算法,可根据业务需求灵活配置。
- 安全性:可作为反向代理隐藏后端服务器IP,支持SSL/TLS加密,保护数据传输安全。
- 适用场景:广泛应用于大型网站、Web应用集群、微服务架构等,为各种业务场景提供负载均衡和高可用性解决方案。
- 配置与部署:支持分多个文件配置,通过灵活配置可实现定制化需求。
- 可视化监测:支持通过页面方式可视化监测后端服务运行状态。
二、安装Haproxy
安装文件可点击进入官网下载,也可以在文章关联的资源中进行下载。
# 安装基础软件
yum install -y gcc gcc-c++ glibc glibc-devel pcre pcre-devel openssl openssl-devel systemd-devel net-tools vim iotop bc zip unzip zlib-devel lrzsz tree screen lsof tcpdump wget# 下载安装包
wget https://www.haproxy.org/download/2.0/src/haproxy-2.0.31.tar.gz# 解压依赖
tar xvf haproxy-2.0.31.tar.gz# 查看系统内核版本,根据内核心指定编译参数TARGET的值(如:linux3100、linux5108等)
uname -r# 编译版本
make ARCH=x86_64 TARGET=linux3100 USE_PCRE=1 USE_OPENSSL=1 USE_ZLIB=1 USE_SYSTEMD=1 USE_CPU_AFFINITY=1 PREFIX=/usr/local/haproxy# 进行安装
make install PREFIX=/usr/local/haproxy# 复制应用至path目录
cp haproxy /usr/sbin/# 添加用户
groupadd haproxy
useradd -g haproxy haproxy
id haproxy# 创建配置文件存放目录
mkdir /etc/haproxy && mkdir /etc/haproxy/conf
三、配置文件
以下提供的是一下基础的Http服务配置,保存到主配置文件/etc/haproxy/haproxy.cfg中,配置多配置文件配置在haproxy.cfg也可配置到/etc/haproxy/conf目录下,通过启动参数 -f 指定。
# 编译haproxy.cfg
vi /etc/haproxy/haproxy.cfg
#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
globallog 127.0.0.1 local0 infochroot /usr/local/haproxymaxconn 20000daemon#-----------
# admin settings
#-----------
defaults adminmode httpstats enablestats refresh 30stimeout connect 10stimeout client 1mtimeout server 1m
listen http_statsbind *:8888 #监控访问端口stats uri /haproxy #监控访问路径stats hide-version #隐藏版本号stats auth admin:123456 #设置管理员帐号和密码log 127.0.0.1 local0 err #[err warning info debug]
##################################################################
#Default mode http
##################################################################
defaults HTTPmode httplog globaloption httplogoption dontlognulloption http-server-closeoption forwardfor except 127.0.0.0/8option redispatchretries 3timeout http-request 10stimeout queue 1mtimeout connect 5000timeout client 50000timeout server 50000maxconn 65535stats uri /status
#---------------------------------------------------------------------
# HTTP 外网
#---------------------------------------------------------------------
frontend http-frontbind :80#bind :443 ssl crt /etc/haproxy/ssl/xxxxx.pemmode httpoption forwardforhttp-request set-header X-Client-IP %[src]http-request set-header X-forwarded-Port %[dst_port]http-request add-header X-forwarded-Proto https if { ssl_fc }#强制https#redirect scheme https if !{ ssl_fc }#HAuse_backend haproxy if { path_beg -i /haproxy } #---------------------------------------------------------------------
# HTTP 内网
#---------------------------------------------------------------------
frontend http-prodbind :8085mode httpoption forwardforhttp-request set-header X-Client-IP %[src]#HAuse_backend haproxy if { path_beg -i /haproxy } #票务内部接口
backend haproxy mode httpbalance sourceserver haproxy-1 127.0.0.1:8888 check inter 2000 rise 2 fall 3 weight 1
四、配置服务
- 在/usr/lib/systemd/system目录下添加haproxy.service文件将haproxy添加为系统服务,实现开机启动,-f可以指向文件,也可以指向文件夹;当指向文件夹时文件夹的所有文件将作为配置文件合并到haproxy.cfg中。
# 编写haproxy服务
vi /usr/lib/systemd/system/haproxy.service
[Unit]
Description=HAProxy Load Balancer
After=syslog.target network.target[Service]
#支持多配置文件读取,类似于从侧面是实现配置文件的include功能。
ExecStartPre=/usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -f /etc/haproxy/conf -c -q
ExecStart=/usr/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -f /etc/haproxy/conf -p /run/haproxy.pid
ExecReload=/bin/kill -USR2 $MAINPID[Install]
WantedBy=multi-user.target
- 设置开始启动
# 启用服务,实现开机启动
systemctl enable haproxy.service # 加载systemd管理配置
systemctl daemon-reload# 启动服务
systemctl start haproxy# 停止服务
systemctl stop haproxy# 查看状态
systemctl status haproxy
- 访问测试
启动haproxy服务后,通过http://Ip:8888/haproxy可打开监控页面,通过配置文件中的帐号:admin密码:123456进行登录访问。