千家信息网

Centos 7基于Haproxy搭建高可用Web群集

发表于:2026-11-01 作者:千家信息网编辑
千家信息网最后更新 2026年11月01日,Haproxy是目前比较流行的一种群集调度工具,同类群集调度工具有很多,如LVS和Nginx。相比较而言,LVS性能最好,但是搭建相对复杂;Nginx的upstream模块支持群集功能,但是相对群集节
千家信息网最后更新 2026年11月01日Centos 7基于Haproxy搭建高可用Web群集

Haproxy是目前比较流行的一种群集调度工具,同类群集调度工具有很多,如LVS和Nginx。相比较而言,LVS性能最好,但是搭建相对复杂;Nginx的upstream模块支持群集功能,但是相对群集节点健康检查功能不强,性能没有Haproxy好,更多的是应用在企业内网环境中。
搭建LVS群集可以参考博文:Centos 7搭建LVS+Keepalived高可用Web服务群集

Nginx群集可以参考博文:基于centos 7安装Tomcat服务及其负载均衡

关于Haproxy搭建Web群集原理概述参考博文:Haproxy搭建Web群集概述

一、部署基于Haproxy搭建高可用Nginx群集

部署环境如下:

准备工作

1)调通网络,防火墙放行相关流量(我这里直接将防火墙关闭了);

2)准备系统映像,配置本地yum(自行配置);

3)下载haproxy源码包,可以从我提供的网盘链接下载使用:https://pan.baidu.com/s/1I8JCUhejz0VSe8Q4lhzUpQ
提取码:th9x

4)web网站使用apache、Nginx、Tomcat搭建都可以,只要可以访问就行,我这里部署两台Nginx和一台Apache,web网站搭建可以参考:
APache网站服务配置访问控制和构建虚拟主机

Centos 7部署Nginx网站服务

基于centos 7安装Tomcat服务及其负载均衡

1、部署第一台Nginx网站

关于Nginx详细配置及说明可以参考:Centos 7部署Nginx网站服务

[root@centos01 ~]# yum -y install prce-devel zlib-devel      [root@centos01 ~]# useradd -M -s /sbin/nologin nginx      [root@centos01 ~]# umount /mnt/      [root@centos01 ~]# mount /dev/cdrom /mnt/       mount: /dev/sr0 写保护,将以只读方式挂载[root@centos01 ~]# scp /mnt/nginx-1.6.0.tar.gz root@192.168.100.20:/root                              The authenticity of host '192.168.100.20 (192.168.100.20)' can't be established.ECDSA key fingerprint is SHA256:PUueT9fU9QbsyNB5NC5hbSXzaWxxQavBxXmfoknXl4I.ECDSA key fingerprint is MD5:6d:f7:95:0e:51:1a:d8:9e:7b:b6:3f:58:51:51:4b:3b.Are you sure you want to continue connecting (yes/no)? yes   Warning: Permanently added '192.168.100.20' (ECDSA) to the list of known hosts.root@192.168.100.20's password:     nginx-1.6.0.tar.gz                                      100%  784KB  68.2MB/s   00:00[root@centos01 ~]# scp /mnt/haproxy-1.4.24.tar.gz root@192.168.100.30:/root                           The authenticity of host '192.168.100.30 (192.168.100.30)' can't be established.ECDSA key fingerprint is SHA256:PUueT9fU9QbsyNB5NC5hbSXzaWxxQavBxXmfoknXl4I.ECDSA key fingerprint is MD5:6d:f7:95:0e:51:1a:d8:9e:7b:b6:3f:58:51:51:4b:3b.Are you sure you want to continue connecting (yes/no)? yes    Warning: Permanently added '192.168.100.30' (ECDSA) to the list of known hosts.root@192.168.100.30's password:     haproxy-1.4.24.tar.gz                                                                               100%  817KB  31.1MB/s   00:00  00:00  [root@centos01 ~]# tar zxvf /mnt/nginx-1.6.0.tar.gz -C /usr/src/   [root@centos01 ~]# cd /usr/src/nginx-1.6.0/   [root@centos01 nginx-1.6.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module    [root@centos01 nginx-1.6.0]# make && make install   [root@centos01 ~]# ln -s /usr/local/nginx/sbin/* /usr/local/sbin/   [root@centos01 ~]# echo "192.168.100.10:nginx" > /usr/local/nginx/html/index.html                                [root@centos01 ~]# nginx    [root@centos01 ~]# netstat -anptu | grep nginx   tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      3685/ngin: master  

2、部署第二台Nginx网站

[root@centos02 ~]# yum -y install pcre-devel zlib-devel   [root@centos02 ~]# ls     anaconda-ks.cfg  initial-setup-ks.cfg  nginx-1.6.0.tar.gz     [root@centos02 ~]# tar zxvf nginx-1.6.0.tar.gz -C /usr/src/   [root@centos02 ~]# useradd -M -s /sbin/nologin nginx      [root@centos02 ~]# cd /usr/src/nginx-1.6.0/        [root@centos02 nginx-1.6.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module    [root@centos02 nginx-1.6.0]# make && make install       [root@centos02 ~]# ln -s /usr/local/nginx/sbin/* /usr/local/sbin/    [root@centos02 ~]# echo "192.168.100.20:nginx" > /usr/local/nginx/html/index.html             [root@centos02 ~]# nginx      [root@centos02 ~]# netstat -anptu | grep nginx       tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      6059/ngin: master  

3、配置客户端

1)客户端添加VM1网卡,配置和服务器同网段IP地址

2)访问第一台nginx服务器

3)访问第二台nginx服务器

4、部署Haproxy服务器

 [root@centos03 ~]# yum -y install pcre-devel bzip2-devel    [root@centos03 ~]# lsanaconda-ks.cfg  haproxy-1.4.24.tar.gz  initial-setup-ks.cfg [root@centos03 ~]# tar zxvf haproxy-1.4.24.tar.gz -C /usr/src/  [root@centos03 ~]# cd /usr/src/haproxy-1.4.24/      [root@centos03 haproxy-1.4.24]# make install      [root@centos03 ~]# mkdir /etc/haproxy      [root@centos03 ~]# cp /usr/src/haproxy-1.4.24/examples/haproxy.cfg /etc/haproxy/                     [root@centos03 ~]# cp /usr/src/haproxy-1.4.24/examples/haproxy.init /etc/init.d/haproxy                         [root@centos03 ~]# chmod +x /etc/init.d/haproxy      [root@centos03 ~]# chkconfig --add haproxy    [root@centos03 ~]# chkconfig --level 35 haproxy on    [root@centos03 ~]# cp /usr/src/haproxy-1.4.24/haproxy /usr/sbin/[root@centos03 ~]# mkdir -p /usr/share/haproxy               [root@centos03 ~]# vim /etc/haproxy/haproxy.cfg    # this config needs haproxy-1.1.28 or haproxy-1.2.1        gid 99                  daemon        #debug        #quietdefaults        log     global                  mode    http                    option  httplog                 option  dontlognull             retries 3          redispatch               maxconn 2000                     contimeout      10                    clitimeout      10                      srvtimeout      10            listen  nginx 192.168.100.30:80                 balance roundrobin                     server  web01 192.168.100.10:80 check inter 2000 fall 3           server  web02 192.168.100.20:80 check inter 2000 fall 3   [root@centos03 ~]# /etc/init.d/haproxy start       Starting haproxy (via systemctl):                          [  确定  ]

至此Haproxy群集部署完成,现在可以使用客户端访问Haproxy服务器的IP地址,第一次访问到的页面是第一台Nginx服务器,第二次访问到的页面会是第二台Nginx服务器

5、部署DNS

我这里就直接配置了,关于DNS详细配置及配置项解释及DNS工作原理概述请参考博文:CentOS7简单搭建DNS服务

[root@centos03 ~]# yum -y install bind bind-chroot bind-utils    [root@centos03 ~]# echo "" > /etc/named.conf    [root@centos03 ~]# vim /etc/named.conf     options {        listen-on       port    53      { 192.168.100.0/24; };        directory       "/var/named";};zone    bdqn.com        IN      {        type    master;        file    "bdqn.com.zone";};[root@centos03 ~]# named-checkconf -z /etc/named.conf[root@centos03 ~]# vim /var/named/bdqn.com.zone$TTL    86400@       SOA     bdqn.com.       root.bdqn.com.(        2019112201        1H        15M        1W        1D)@       NS      centos03.bdqn.com.centos03 A      192.168.100.30www      A      192.168.100.30[root@centos03 ~]# named-checkzone bdqn.com /var/named/bdqn.com.zone                          zone bdqn.com/IN: loaded serial 2019112201OK[root@centos03 ~]# chmod +x /var/named/bdqn.com.zone                        [root@centos03 ~]# chown named:named /var/named/bdqn.com.zone                         [root@centos03 ~]# systemctl start named      [root@centos03 ~]# systemctl enable named    

至此DNS已经配置完成,客户端添加DNS地址,打开浏览器使用域名访问即可

二、使用Haproxy搭建Apache网站群集

1、部署apache网站

关于Apache网站详细配置及工作原理概述参考博文:CentOS 7.4搭建Apache网站服务

[root@centos04 ~]# tar zxvf /mnt/httpd-2.2.17.tar.gz -C /usr/src/ [root@centos04 ~]# cd /usr/src/httpd-2.2.17/[root@centos04 httpd-2.2.17]# ./configure --prefix=/usr/local/httpd --enable-so -enable-rewrite --enable-charset-lite --enable-cgi                       [root@centos04 httpd-2.2.17]# make && make install    [root@centos04 ~]# ln -s /usr/local/httpd/bin/* /usr/local/bin/    [root@centos04 ~]# cp /usr/local/httpd/bin/apachectl /etc/init.d/httpd[root@centos04 ~]# chmod +x /etc/init.d/httpd  [root@centos04 ~]# vim /etc/init.d/httpd     #!/bin/sh#chkconfig 35 80 20#description:apache server[root@centos04 ~]# chkconfig --add httpd    [root@centos05 ~]# echo "192.168.100.40:apache" > /usr/local/httpd/htdocs/index.html                    [root@centos04 ~]# apachectl -t     [root@centos04 ~]# systemctl start httpd    [root@centos04 ~]# systemctl enable httpd    [root@centos04 ~]# netstat -anptu | grep httpd    tcp6       0      0 :::80                   :::*                    LISTEN      53801/httpd    [root@centos04 ~]# systemctl is-enabled httpd.service                 enabled

2、部署Haproxy服务

listen  nginx 192.168.100.30:80        balance roundrobin                server  web01 192.168.100.10:80 check inter 2000 fall 3           server  web02 192.168.100.20:80 check inter 2000 fall 3           server  apache01 192.168.100.40:80 check inter 2000 fall 3 weight 1                                         [root@centos03 ~]# /etc/init.d/haproxy restart        Restarting haproxy (via systemctl):                        [  确定  ]

至此基于Apache网站群集已经部署完成,现在可以使用客户端访问

[root@centos03 ~]# tail -f /var/log/haproxy/haproxy-info.log        

------ 本文至此结束,感谢阅读 ------

服务 网站 配置 服务器 客户 客户端 至此 原理 地址 二台 工作 均衡 功能 性能 环境 防火墙 页面 准备 参考 调度 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 咖啡网 河南云网互联网络科技有限公司 中国互联网科技股 互联网金融 金融科技 小马过河互联网科技 上海喔噻互联网科技 善班互联网科技有限公司 卓越科技互联网 招聘 太原新华互联网科技学校怎么样 青海省互联网新科技 海南科技产业互联网平台 崇明区互联网教育科技售后服务 国大互联网教育科技有限公司工作 威龙国际互联网科技公司 山东子淼互联网科技有限公司 四川互联网展示科技馆设计 互联网领先科技宣传片 中国互联网公司的科技投入 米泉互联网科技专业怎么样 陕西彩京云互联网科技有限公司 新华互联网科技 唐信互联网科技 未来的互联网科技 科技类的互联网平台 互联网企业和科技型企业 栖莱科技互联网股份有限公司 广西互联网科技孵化中心 用友工业互联网科技发展 陕西云淼互联网科技公司 上海万顺科技互联网有限公司招聘
0