HAProxy实验

HAProxy 七层负载均衡部署与配置实战
一、实验说明

本实验部署 HAProxy 作为 HTTP/HTTPS 负载均衡器,将请求分发到多个后端应用服务器,实现服务的高可用与会话保持。
二、知识点讲解

HAProxy 定位:支持四层和七层负载均衡,常用于 Web 服务、数据库等场景,支持丰富的健康检查和会话保持策略。

核心组件:

frontend:接收客户端请求,定义监听端口和协议。

backend:定义后端服务器池和调度策略。

listen:同时定义 frontend 和 backend,适用于简单场景。

健康检查:支持 HTTP、TCP、SSL 等多种健康检查方式,自动剔除故障节点。
三、操作步骤

1、安装 HAProxy

复制代码
yum install -y haproxy

2、编辑配置文件 /etc/haproxy/haproxy.cfg

复制代码
global
    log         127.0.0.1 local2
    chroot      /var/lib/haproxy
    pidfile     /var/run/haproxy.pid
    maxconn     4000
    user        haproxy
    group       haproxy
    daemon

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

frontend main
    bind *:80
    default_backend app_servers

backend app_servers
    balance     roundrobin
    server  app1 192.168.1.20:80 check
    server  app2 192.168.1.21:80 check

3、启动服务

复制代码
systemctl start haproxy
systemctl enable haproxy

4、测试验证

(1)访问 http://192.168.1.10(HAProxy 地址),验证负载分发。

(2)查看 HAProxy 统计页面(需在配置中开启)。

相关推荐
tntxia17 小时前
linux curl命令详解_curl详解
linux
扛枪的书生19 小时前
Linux 网络管理器用法速查
linux
顺风尿一寸1 天前
Java Socket 内核之旅:从 SocketChannel.read() 到 tcp_recvmsg 与 epoll 的完整调用链路
linux
XIAOHEZIcode1 天前
Ubuntu 终端美化全栈指南:Bash 到 Kitty 踩坑实录
linux·ubuntu·命令行
唐青枫1 天前
别再只会用 cron:Linux systemd Timer 定时任务实战详解
linux
AlfredZhao3 天前
生产环境里,为什么不建议把普通端口直接暴露到公网?
linux·https·443·80
戴为沐4 天前
Linux内存扩容指南
linux
zylyehuo5 天前
Linux 彻底且安全地删除文件
linux
用户805533698035 天前
主线 U-Boot 上 RK3506:和闭源 rkbin 拔河的三个隐性契约
linux·嵌入式
用户034095297915 天前
linux fcitx 5 雾凇拼音 设置在中文输入法下仍然输入英文标点
linux