当前位置: 首页 > news >正文

【部署篇】Haproxy-01安装部署(源码方式安装)

‌一、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进行登录访问。

http://www.lryc.cn/news/467903.html

相关文章:

  • 开拓鸿蒙测试新境界,龙测科技引领自动化测试未来
  • Java项目-基于springboot框架的自习室预订系统项目实战(附源码+文档)
  • 调整数组奇偶数顺序
  • Electron调用nodejs的cpp .node扩展【非安全】
  • 一文了解AOSP是什么?
  • ffmpeg视频边缘模糊,打造梦幻般的视觉效果!
  • [Wireshark] 使用Wireshark抓包https数据包并显示为明文、配置SSLKEYLOGFILE变量(附下载链接)
  • 大话红黑树之(1)入门介绍
  • ESC/POS图片打印指令
  • Unity之如何在Linux上部署Dedicated Server专用服务器
  • 十、Linux 故障排除专业案例分享
  • 智慧楼宇平台,构筑未来智慧城市的基石
  • JVM 实战篇(一万字)
  • 线程同步之双摄
  • 使用 PyTorch 构建 LSTM 股票价格预测模型
  • 【C++篇】C++类与对象深度解析(五):友元机制、内部类与匿名对象的讲解
  • 模型训练进度条的代码
  • 直观理解反向传播 | Chapter 3 | Deep Learning | 3Blue1Brown
  • 052_python基于Python高校岗位招聘和分析平台
  • 基于物联网、大数据、人工智能等技术开发的Spring Cloud 智慧工地云平台源码,支持多端应用
  • 常见的跨境电商平台对比【总结表】
  • perl批量改文件后缀
  • 【Python中的字符串处理】正则表达式与常用字符串操作技巧!
  • 又是一年一度的1024,那就记录一篇算法博客吧~ 【二进制加法探秘】
  • LeetCode--买卖股票的最佳时机含冷冻期--动态规划
  • 装了Ubuntu和Windows双系统,如何设置默认启动Windows
  • WPF+MVVM案例实战-设备状态LED灯变化实现
  • MySQL--基本介绍
  • PAT甲级1008 Elevator
  • 数据导入导出