千家信息网

CentOS7.4—构建最新版haproxy高可用群集

发表于:2026-10-31 作者:千家信息网编辑
千家信息网最后更新 2026年10月31日,CentOS7.4-构建haproxy高可用群集目录第一部分 实验环境第二部分 搭建配置web服务器第三部分 安装配置haproxy服务器第四部分 测试验证第五部分 haproxy配置相关详细解释第一
千家信息网最后更新 2026年10月31日CentOS7.4—构建最新版haproxy高可用群集

CentOS7.4-构建haproxy高可用群集

目录
第一部分 实验环境
第二部分 搭建配置web服务器
第三部分 安装配置haproxy服务器
第四部分 测试验证
第五部分 haproxy配置相关详细解释

第一部分 实验环境

  1. 一台harpoxy调度服务器
    IP地址:192.168.80.10
    需要软件:haproxy-1.7.10.tar
  2. 两台Web服务器(基于nginx)
    IP地址:192.168.80.20(web01)
    IP地址:192.168.80.30(web02)
    需要软件:nginx-1.13.9.tar.gz
    //三台服务器系统:linux-CentOS7.4

    //软件:

  3. Win7客户端一台(验证测试用)
    IP地址:192.168.80.2

第二部分 搭建配置web服务器
第一步:配置web01
[root@web01 ~]# yum install -y \ //安装相关插件及编译安装工具

pcre-devel \
zlib-devel \
make \
gcc \
gcc-c++
[root@web01 ~]# useradd -M -s /sbin/logogin nginx //创建nginx程序用户
[root@web01 ~]# tar xzvf nginx-1.13.9.tar.gz
[root@web01 ~]# cd nginx-1.13.9
[root@web01 nginx-1.13.9]# ./configure \ //定义配置
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx
[root@web01 nginx-1.13.9]# make && make install //编译及安装
[root@web01 nginx-1.13.9]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ //将nginx可执行程序放到系统环境中
[root@web01 nginx-1.13.9]# echo "

SERVER AA

" > /usr/local/nginx/html/index.html
//修改默认主页显示内容(便于后面测试)
[root@web01 nginx-1.13.9]# nginx //启动nginx服务
[root@web01 nginx-1.13.9]# netstat -anpt | grep nginx

Win7访问http://192.168.80.10

第二步:配置web02(与web01一样配置)
[root@web02 ~]# yum install -y \
pcre-devel \
zlib-devel \
gcc \
gcc-c++ \
make
[root@web02 ~]# useradd -M -s /sbin/nologin nginx
[root@web02 ~]# tar xzvf nginx-1.13.9.tar.gz
[root@web02 ~]# cd nginx-1.13.9
[root@web02 nginx-1.13.9]# ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx
[root@web02 nginx-1.13.9]# make && make install
[root@web02 nginx-1.13.9]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[root@web02 nginx-1.13.9]# echo "

SERVER BB

" > /usr/local/nginx/html/index.html
[root@web02 nginx-1.13.9]# nginx
[root@web02 nginx-1.13.9]# netstat -anpt | grep nginx

Win7访问http://192.168.80.20

第三部分 安装配置haproxy服务器
[root@haproxy ~]# yum install -y \ //安装插件及编译工具

pcre-devel \
bzip2-devel \
gcc \
gcc-c++ \
make
[root@haproxy ~]# tar xzvf haproxy-1.7.10.tar.gz
[root@haproxy ~]# cd haproxy-1.7.10
[root@haproxy haproxy-1.7.10]# make TARGET=linux26 //标识64位系统
[root@haproxy haproxy-1.7.10]# make install
[root@haproxy haproxy-1.7.10]# mkdir /etc/haproxy
[root@haproxy haproxy-1.7.10]# groupadd haproxy
[root@haproxy haproxy-1.7.10]# useradd -s /sbin/nologin -M -g haproxy haproxy //添加haproxy运行haproxy账号并设置及属主与属组
[root@haproxy haproxy-1.7.10]# vi /etc/haproxy/haproxy.cfg //创建并编辑haproxy配置文件
--------------全局配置----------------
global
log 127.0.0.1 local2
#chroot /usr/local/haproxy-1.7.10
pidfile /var/run/haproxy.pid
maxconn 4000 //最大连接数
user haproxy
group haproxy
daemon //创建1个进程进入deamon模式运行,此参数要求将运行模式设置为daemon

#---------------------------------------------------------------------

common defaults that all the 'listen' and 'backend' sections will

use if not designated in their block

#---------------------------------------------------------------------
defaults
mode http //默认模式,tcp是四层,http是七层,health只会返回OK,若是混合模式则mode不需要设置
log global //采用全局定义的日志
option dontlognull //不记录健康检查的日志信息
option httpclose //每次请求完毕后主动关闭http通道
option httplog //日志类别http日志格式;如果是混合模式,此处还需要加上tcpclog
#option forwardfor //如果后端服务器需要获得客户端真实ip需要配置的参数,可以从Http Header中获得客户端ip
option redispatch //serverId对应的服务器挂掉后,强制定向到其他健康的服务器
timeout connect 10s //超时连接10s
timeout client 10s //客户端超时连接10s
timeout server 10s //服务器连接超时
maxconn 60000 //最大连接数
retries 3 //3次连接失败就认为服务不可用
--------------统计页面配置------------------
listen admin_stats
bind 0.0.0.0:8089 //监听端口
stats enable //启用监听端口
mode http
log global
stats uri /stats //统计页面url
stats realm Haproxy\ Statistics //统计页面密码框上提示文本
stats auth admin:admin //统计页面用户名和密码设置
#stats hide-version //隐藏统计页面上HAProxy的版本信息
stats admin if TRUE //当通过认证才可管理
stats refresh 30s //页面自动刷新时间30s
---------------web设置-----------------------
listen webcluster
bind 0.0.0.0:80
mode http
option httpchk GET /index.html
log global
maxconn 3000
balance roundrobin
server web01 192.168.80.10:80 check inter 2000 fall 3
server web02 192.168.80.20:80 check inter 2000 fall 3
保存退出
[root@haproxy haproxy-1.7.10]# cp examples/haproxy.init /etc/init.d/haproxy
[root@haproxy haproxy-1.7.10]# chmod 755 /etc/init.d/haproxy
[root@haproxy haproxy-1.7.10]# chkconfig --add haproxy
[root@haproxy haproxy-1.7.10]# ln -s /usr/local/sbin/haproxy /usr/sbin/haproxy
[root@haproxy haproxy-1.7.10]# service haproxy start
[root@haproxy haproxy-1.7.10]# netstat -anpt | grep haproxy

[root@haproxy haproxy-1.7.10]# systemctl stop firewalld
[root@haproxy haproxy-1.7.10]# setenforce 0

第四部分 验证测试
Win7访问调度器地址http://192.168.80.30

等待一会,再次访问

//验证成功

第五部分 haproxy配置相关详细解释
#全局配置, 用于设定义全局参数, 属于进程级的配置, 通常与操作系统配置有关.
global
#定义全局日志, 配置在本地, 通过local0 输出, 默认是info级别,可配置两条
log 127.0.0.1 local0 warning
#定义日志级别【error warning info debug】
#log 127.0.0.1 local1 info
#运行路径
chroot /usr/local/haproxy
#PID 文件存放路径
pidfile /var/run/haproxy.pid
#设置每haproxy进程的最大并发连接数, 其等同于命令行选项"-n"; "ulimit -n"自动计算的结果参照此参数设定.
maxconn 4096
#运行haproxy 用户, 或者使用关键字uid
user haproxy
#运行haproxy 用户组, 或者使用关键字gid
group haproxy
#后台运行haproxy
daemon
#设置启动的haproxy进程数量, 只能用于守护进程模式的haproxy;
#默认只启动一个进程, 鉴于调试困难等多方面的原因, 一般只在单进程仅能打开少数文件描述符的场景中才使用多进程模式.
nbproc 1
#设置每进程所能够打开的最大文件描述符数目, 默认情况其会自动进行计算, 因此不推荐修改此选项.
#ulimit-n 819200
#调试级别, 一般只在开启单进程时调试, 且生产环境禁用.
#debug
#haproxy启动后不会显示任何相关信息, 这与在命令行启动haproxy时加上参数"-q"相同
#quiet
#定义统计信息保存位置
stats socket /usr/local/haproxy/stats
#默认配置
defaults
#默认的模式【tcp:4层; http:7层; health:只返回OK】
mode http
#继承全局的日志定义输出
log global
#日志类别, httplog
#option httplog
#如果后端服务器需要记录客户端真实ip, 需要在HTTP请求中添加"X-Forwarded-For"字段;
#但haproxy自身的健康检测机制访问后端服务器时, 不应将记录访问日志,可用except来排除127.0.0.0,即haproxy本身.
#option forwardfor except 127.0.0.0/8
option forwardfor
#开启http协议中服务器端关闭功能, 每个请求完毕后主动关闭http通道, 使得支持长连接,使得会话可以被重用,使得每一个日志记录都会被记录.
option httpclose
#如果产生了一个空连接,那这个空连接的日志将不会记录.
option dontlognull
#当与后端服务器的会话失败(服务器故障或其他原因)时, 把会话重新分发到其他健康的服务器上; 当故障服务器恢复时, 会话又被定向到已恢复的服务器上;
#还可以用"retries"关键字来设定在判定会话失败时的尝试连接的次数
option redispatch
retries 3
#当haproxy负载很高时, 自动结束掉当前队列处理比较久的链接.
option abortonclose
#默认http请求超时时间
timeout http-request 10s
#默认队列超时时间, 后端服务器在高负载时, 会将haproxy发来的请求放进一个队列中.
timeout queue 1m
#haproxy与后端服务器连接超时时间.
timeout connect 5s
#客户端与haproxy连接后, 数据传输完毕, 不再有数据传输, 即非活动连接的超时时间.
timeout client 1m
#haproxy与后端服务器非活动连接的超时时间.
timeout server 1m
#默认新的http请求连接建立的超时时间,时间较短时可以尽快释放出资源,节约资源.
timeout http-keep-alive 10s
#心跳检测超时时间
timeout check 10s
#最大并发连接数
maxconn 2000
#设置默认的负载均衡方式
#balance source
#balnace leastconn
#统计页面配置, frontend和backend的组合体, 监控组的名称可按需自定义
listen admin_status
#配置监控运行模式
mode http
#配置统计页面访问端口
bind 0.0.0.0:1080
#统计页面默认最大连接数
maxconn 10
#http日志格式
option httplog
#开启统计
stats enable
#隐藏统计页面上的haproxy版本信息
stats hide-version
#监控页面自动刷新时间
stats refresh 30s
#统计页面访问url
stats uri /stats
#统计页面密码框提示文本
stats realm mCloud\ Haproxy
#监控页面的用户和密码:admin, 可设置多个用户名
stats auth admin:admin
#手工启动/禁用后端服务器, 可通过web管理节点
stats admin if TRUE
#设置haproxy错误页面
errorfile 400 /usr/local/haproxy/errorfiles/400.http
errorfile 403 /usr/local/haproxy/errorfiles/403.http
errorfile 408 /usr/local/haproxy/errorfiles/408.http
errorfile 500 /usr/local/haproxy/errorfiles/500.http
errorfile 502 /usr/local/haproxy/errorfiles/502.http
errorfile 503 /usr/local/haproxy/errorfiles/503.http
errorfile 504 /usr/local/haproxy/errorfiles/504.http
#监控haproxy后端服务器的监控状态
listen site_status
bind 0.0.0.0:1081 #监听端口
mode http #http的7层模式
log 127.0.0.1 local2 err #[err warning info debug]
monitor-uri /site_status #网站健康检测URL,用来检测HAProxy管理的网站是否可以用,正常返回200,不正常返回503
acl site_dead nbsrv(php_server) lt 1 #定义网站down时的策略当挂在负载均衡上的指定backend的中有效机器数小于1台时返回true
acl site_dead nbsrv(html_server) lt 1
acl site_dead nbsrv(backend_default) lt 1
monitor fail if site_dead #当满足策略的时候返回503,网上文档说的是500,实际测试为503
monitor-net 192.168.4.171/32 #来自192.168.4.152的日志信息不会被记录和转发
monitor-net 192.168.4.172/32
#frontend, 名字自定义
frontend HAproxy_Cluster
#定义前端监听端口, 建议采用bind :80的形式,否则做集群高可用的时候有问题,vip切换到其余机器就不能访问.
bind 0.0.0.0:80
#acl后面是规则名称,当请求的url末尾是以.php结尾时,匹配触发php_web规则,以下两种写法均可.
#当请求的url末尾是以.css、.jpg、.png、.jpeg、.js、.gif结尾时,匹配并触发static_web规则.
#acl static_web path_end .gif .png .jpg .css .js .jpeg
#acl static_web url_reg /
.(css|jpg|png|jpeg|js|gif)$
#-i为忽略大小写,当被请求的是以www.test.com开头的主机时,匹配并触发dns_name规则.
acl html_web hdr_beg(host) -i www.haproxytest.com
#acl html_web hdr_beg(host) 10.11.4.152
#当客户端的IP是x.x.x.x时,匹配并触发src_ip规则.
#acl src_ip src x.x.x.x
#如果匹配acl规则php_web,将请求转交到php_server组处理;如果匹配acl规则html_web,将请求转交到html_server组处理.
use_backend php_server if php_web
use_backend html_server if html_web
#如果以上规则都不匹配时,将请求转交到default_backend组处理.
default_backend backend_default
#backend后端配置, 配置php_server组与html_server组
backend php_server
#定义负载均衡方式为roundrobin方式, 即基于权重进行轮询调度的算法, 在服务器性能分布较均匀情况下推荐.
#另有如下几种负载均衡方式:
#-- static-rr: 也是基于权重进行轮转调度, 但属于静态方法, 运行时调整后端机组权重不会使用新的权重;
#-- source: 基于请求源IP进行hash运算匹配后端服务器组;
#-- leastconn: 不适合会话较短的环境, 如基于http的应用;
#-- uri: 对整个URI进行hash运算;
#-- uri_param: 对URI中的参数进行转发;
#-- hdr():根据http头进行转发, 无该头部则转为使用roundrobin.
balance roundrobin
mode http
#允许插入serverid到cookie中,serverid后面可定义
cookie SERVERID
#心跳检测方式为检测后端服务器index.html文件,还有其他方式
option httpchk GET /index.html
#后端服务器定义, maxconn 1024表示该服务器的最大连接数, cookie 1表示serverid为1, weight代表权重(默认1,最大为265,0则表示不参与负载均衡),
#check inter 1500是检测心跳频率, rise 2是2次正确认为服务器可用, fall 3是3次失败认为服务器不可用.
server php1 192.168.4.171:80 maxconn 1024 cookie 1 weight 3 check inter 1500 rise 2 fall 3
backend html_server
balance source
mode http
server html1 192.168.4.172:80 maxconn 1024 cookie 1 weight 3 check inter 1500 rise 2 fall 3
backend backend_default
balance source
mode http
server default1 192.168.4.171:80 maxconn 1024 cookie 1 weight 3 check inter 1500 rise 2 fall 3

服务 服务器 配置 页面 日志 统计 时间 模式 进程 运行 最大 规则 客户 检测 信息 全局 参数 客户端 方式 用户 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 上海睿民互联网科技有限公司 疫情对互联网科技公司影响 碳银互联网科技有限公司 天下捷融互联网科技 互联网科技网站 亿人帮互联网科技 中国互联网金融科技基金 华脉科技工业互联网产品 常德市电业局互联网科技部 互联网金融中的现代科技 岳阳华臻互联网科技有限公司 阿大互联网科技有限公司 成都互联网科技公司工作好不好 伊宁互联网科技做什么的 小米互联网科技园入驻 广州富喜互联网络科技 互联网科技年终奖 山东大美朱楼互联网科技有限公司 珠海东科互联网科技有限公司 互联网高科技绘画 蓝思科技飞鸽互联网 奔汉互联网科技 菲比寻常互联网科技 央行互联网金融科技公司 北京华百购互联网科技有限公司 崇明区互联网教育科技售后服务 佛山市拉法互联网科技有限公司 上海钢联属于互联网科技股吗 国产互联网科技公司 互联网领先科技成果宣传视频
0