使用Haproxy搭建Web群集(理论结合实战,全程可跟做!
发表于:2026-10-31 作者:千家信息网编辑
千家信息网最后更新 2026年10月31日,常见的Web集群调度器:目前常见的Web集群调度器分为软件和硬件,软件通常使用开源的LVS、Haproxy、 Nginx, 硬件一般使用比较多的是F5,也有很多人使用国内的一些产品,如梭子鱼、绿盟等H
千家信息网最后更新 2026年10月31日使用Haproxy搭建Web群集(理论结合实战,全程可跟做!
常见的Web集群调度器:
目前常见的Web集群调度器分为软件和硬件,软件通常使用开源的LVS、Haproxy、 Nginx, 硬件一般使用比较多的是F5,也有很多人使用国内的一些产品,如梭子鱼、绿盟等
Haproxy应用分析
LVS在企业应用中抗负载能力很强,但存在不足
1.LVS不支持正则处理,不能实现动静分离
2.对于大型网站,LVS的实施配置复杂,维护成本相对较高
Haproxy是一款可提供高可用性、负载均衡、及基于TCP和HTTP应用的代理的软件
1.特别适用于负载特别大的Web站点
2.运行在当前的硬件.上可支持数以万计的并发连接连接请求
Haproxy调度算法原理
Haproxy支持多种调度算法,最常用的有3种:
1.RR (Round Robin):
RR算法是最简单最常用的一-种算法,即轮询调2.理解举例:
有三个节点A、B、C,第一个用户访问会被指派到节点A,第二个用户访问会被指派到节点B,第三个用户访问会被指派到节点第四个用户访问继续指派到节点A,轮询分配访问请求实现负载均衡效果
Haproxy支持多种调度算法, 最常用的有三种:
1.SH (Source Hashing):
SH即基于来源访问调度算法,此算法用于一些有Session会话记录在服务器端的场景,可以基于来源的IP、Cookie等做集群调度
2.理解举例
①有三个节点A、B、C,第一个用户第一次访问被指派到了A,第二个用户第一次访问被指派到了B
②当第一个用户第二次访问时会被继续指派到A,第二个用户第二次访问时依旧会被指派到B,只要负载均衡调度器不重启,第一个用户访问都会被指派到A,第二个用户访问都会被指派到B,实现集群的调度
③此调度算法好处是实现会话保持,但某些IP访问量非常大时会引起负载不均衡,部分节点访问量超大,影响业务使用
Demo:
环境准备:
Nginx1:192.168.100.201---->CentOS 7-2
Nginx2:192.168.100.202---->CentOS 7-3
调度服务器:192.168.100.210---->CentOS 7-4
客户机:192.168.100.58---->win 7-1
Nginx2:CentOS 7-3的操作
[root@localhost ~]# yum install pcre-devel zlib-devel gcc gcc-c++ make -y//改网卡为仅主机模式[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33BOOTPROTO="static"IPADDR=192.168.100.201NETMASK=255.255.255.0GATEWAY=192.168.100.1//修改完成后输入:wq保存退出[root@localhost ~]# service network restart Restarting network (via systemctl): [ 确定 ][root@localhost ~]# mkdir /aaa[root@localhost ~]# mount.cifs //192.168.10.189/rpm /aaaPassword for root@//192.168.10.189/rpm: [root@localhost ~]# cd /aaa[root@localhost aaa]# lsapr-1.6.2.tar.gz jdkapr-util-1.6.0.tar.gz john-1.8.0.tar.gzawstats-7.6.tar.gz lf.jpgcronolog-1.6.2-14.el7.x86_64.rpm mysqlDiscuz_X3.4_SC_UTF8.zip nginx-1.12.2.tar.gzerror.png phpextundelete-0.2.4.tar.bz2 TChaproxy-1.5.19.tar.gz tomcathttpd-2.4.29.tar.bz2 tomcat.tmphzw.jpeg wh.jpg[root@localhost aaa]# cd tomcat/[root@localhost tomcat]# lsapache-tomcat-7.0.54.tar.gz jdk-8u91-linux-x64.tar.gzapache-tomcat-8.5.16.tar.gz nginx-1.12.0.tar.gzjdk-7u65-linux-x64.gz nginx-1.6.0.tar.gz[root@localost tomcat]# tar zxvf nginx-1.12.0.tar.gz -C /opt/[root@localhost tomcat]# cd /opt/[root@localhost opt]# useradd -M -s /sbin/nologin nginx[root@localhost opt]# cd nginx-1.12.0/[root@localhost nginx-1.12.0]# lsauto CHANGES.ru configure html man srcCHANGES conf contrib LICENSE README[root@localhost nginx-1.12.0]# ./configure \--prefix=/usr/local/nginx \--user=nginx \--group=nginx[root@localhost nginx-1.12.0]# make && make install[root@localhost nginx-1.12.0]# cd /usr/local/nginx/html/[root@localhost html]# echo "this is accp web" > test.html[root@localhost html]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/[root@localhost html]# nginx -tnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is oknginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful[root@localhost html]# nginx[root@localhost html]# netstat -ntap | grep 80tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 6408/nginx: master [root@localhost html]# systemctl stop firewalld.service [root@localhost html]# setenforce 0在火狐浏览器中输入:http://127.0.0.1/test.html 验证:此时显示 this is accp web

Nginx1:CentOS 7-2的 操作
[root@localhost ~]# yum install pcre-devel zlib-devel gcc gcc-c++ make -y//改网卡为仅主机模式[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33BOOTPROTO="static"IPADDR=192.168.100.202NETMASK=255.255.255.0GATEWAY=192.168.100.1//修改完成后输入:wq保存退出[root@localhost ~]# service network restart Restarting network (via systemctl): [ 确定 ][root@localhost ~]# mkdir /aaa[root@localhost ~]# mount.cifs //192.168.10.189/rpm /aaaPassword for root@//192.168.10.189/rpm: [root@localhost ~]# cd /aaa[root@localhost aaa]# lsapr-1.6.2.tar.gz jdkapr-util-1.6.0.tar.gz john-1.8.0.tar.gzawstats-7.6.tar.gz lf.jpgcronolog-1.6.2-14.el7.x86_64.rpm mysqlDiscuz_X3.4_SC_UTF8.zip nginx-1.12.2.tar.gzerror.png phpextundelete-0.2.4.tar.bz2 TChaproxy-1.5.19.tar.gz tomcathttpd-2.4.29.tar.bz2 tomcat.tmphzw.jpeg wh.jpg[root@localhost aaa]# cd tomcat/[root@localhost tomcat]# lsapache-tomcat-7.0.54.tar.gz jdk-8u91-linux-x64.tar.gzapache-tomcat-8.5.16.tar.gz nginx-1.12.0.tar.gzjdk-7u65-linux-x64.gz nginx-1.6.0.tar.gz[root@localost tomcat]# tar zxvf nginx-1.12.0.tar.gz -C /opt/[root@localhost tomcat]# cd /opt/[root@localhost opt]# useradd -M -s /sbin/nologin nginx[root@localhost opt]# cd nginx-1.12.0/[root@localhost nginx-1.12.0]# lsauto CHANGES.ru configure html man srcCHANGES conf contrib LICENSE README[root@localhost nginx-1.12.0]# ./configure \--prefix=/usr/local/nginx \--user=nginx \--group=nginx[root@localhost nginx-1.12.0]# make && make install[root@localhost nginx-1.12.0]# cd /usr/local/nginx/html/[root@localhost html]# echo "this is benet web" > test.html[root@localhost html]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/[root@localhost html]# nginx -tnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is oknginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful[root@localhost html]# nginx[root@localhost html]# netstat -ntap | grep 80tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 6408/nginx: master [root@localhost html]# systemctl stop firewalld.service [root@localhost html]# setenforce 0在火狐浏览器中输入:http://127.0.0.1/test.html 验证:此时显示 this is benet web

调度服务器:CentOS 7-4的操作
[root@localhost ~]# yum install bzip2-devel pcre-devel gcc gcc-c++ make -y//改网卡为仅主机模式[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33BOOTPROTO="static"IPADDR=192.168.100.210NETMASK=255.255.255.0GATEWAY=192.168.100.1//修改完成后输入:wq保存退出[root@localhost ~]# service network restart Restarting network (via systemctl): [ 确定 ][root@localhost ~]# mkdir /aaa[root@localhost ~]# mount.cifs //192.168.10.189/rpm /aaaPassword for root@//192.168.10.189/rpm: [root@localhost ~]# cd /aaa[root@localhost aaa]# lsapr-1.6.2.tar.gz jdkapr-util-1.6.0.tar.gz john-1.8.0.tar.gzawstats-7.6.tar.gz lf.jpgcronolog-1.6.2-14.el7.x86_64.rpm mysqlDiscuz_X3.4_SC_UTF8.zip nginx-1.12.2.tar.gzerror.png phpextundelete-0.2.4.tar.bz2 TChaproxy-1.5.19.tar.gz tomcathttpd-2.4.29.tar.bz2 tomcat.tmphzw.jpeg wh.jpg[root@localhost aaa]# tar zxvf haproxy-1.5.19.tar.gz -C /opt/[root@localhost aaa]# cd /opt[root@localhost opt]# lshaproxy-1.5.19 rh[root@localhost opt]# cd haproxy-1.5.19/[root@localhost haproxy-1.5.19]# make TARGET=linux26[root@localhost haproxy-1.5.19]# make install[root@localhost haproxy-1.5.19]# mkdir /etc/haproxy[root@localhost haproxy-1.5.19]# cp examples/haproxy.cfg /etc/haproxy/[root@localhost haproxy-1.5.19]# vim /etc/haproxy/haproxy.cfg //其中chroot /usr/share/haproxy 根目录删除//其中 redispatch 请求转发给宕机服务器删除//下面的所有的Listen全部删除,按88dd//然后添加以下内容listen webcluster 0.0.0.0:80 option httpchk GET /test.html balance roundrobin server instl 192.168.100.201:80 check inter 2000 fall 3 server inst2 192.168.100.202:80 check inter 2000 fall 3//修改完成后输入:wq保存退出[root@localhost haproxy-1.5.19]# cp examples/haproxy.init /etc/init.d/haproxy[root@localhost haproxy-1.5.19]# chmod +x /etc/init.d/haproxy [root@localhost haproxy-1.5.19]# chkconfig --add haproxy [root@localhost haproxy-1.5.19]# ln -s /usr/local/sbin/haproxy /usr/sbin/haproxy[root@localhost haproxy-1.5.19]# service haproxy startStarting haproxy (via systemctl): [ 确定 ][root@localhost haproxy-1.5.19]# systemctl stop firewalld.service [root@localhost haproxy-1.5.19]# setenforce 0win7-1测试结果:
1.在win 7-1中先把网卡设为仅主机模式,然后在网卡配置中将IPv4设为静态IP:192.168.100.58(需要在同一网段),网关为:255.255.255.0
2.然后在浏览器中输入:http://192.168.100.210/test.html
会先看到:this is accp web
再次刷新会看到:this is benet web
此时实现轮询访问 nginx 节点!
调度
用户
算法
节点
输入
网卡
均衡
主机
服务器
模式
集群
支持
服务
三个
常用
浏览器
硬件
软件
应用
浏览
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
小米移动互联网科技园
互联网加科技
天津市摩尔互联网科技
互联网科技公司起名
科技赋能下的互联网金融
互联网金融监管科技数据
互联网通讯科技
期待互联网科技
浩特互联网科技
极限挑战互联网科技
广西互联网科技公司
互联网科技公司有哪些
华中科技有哪些互联网大佬
亳州好赞互联网科技有限公司
爱医互联网络科技
恒生互联网科技业指数调整周期
黑科技 体验场景 互联网大会
长沙渺峰互联网科技有限公司
科技互联网经济论坛
互联网科技大佬云集
精品咖啡豆
互联网金融与金融科技
中国互联网科技发展
迪茉互联网科技
唐信互联网科技 大连
奔汉互联网科技
互联网科技咨询
江阴互联网智能科技产品进口
东莞新华互联网科技学校
数字互联网科技有限公司