千家信息网

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

发表于:2025-02-05 作者:千家信息网编辑
千家信息网最后更新 2025年02月05日,Haproxy是目前比较流行的一种群集调度工具,同类群集调度工具有很多,如LVS和Nginx。相比较而言,LVS性能最好,但是搭建相对复杂;Nginx的upstream模块支持群集功能,但是相对群集节
千家信息网最后更新 2025年02月05日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安全错误 数据库的锁怎样保障安全 梦幻西游在哪里看服务器开了几天 服务器安全配置总结 一站式网络技术咨询售后服务 搜亚网络技术服务刷赞 计算机网络技术创业能力目标 数据中心网络安全检查 公司成立网络安全委员会文件 网络安全观后感200小学 哪里有浪潮存储服务器客服电话 北京服务器电源哪家强 储入式数据库技术视频 数据库表存储容量 深圳小鹅网络技术福利 南京网络营销软件开发答疑解惑 软件开发互联网开发还是企业 北京it软件开发系统 微信小程序云开发更新数据库 软件开发初学什么软件 我国网络安全真实案例 海康威视 监控软件开发 网络安全的好题目 快捷方式图标软件开发 网络安全五个要素 网络技术软件哪个好用 光遇为啥一直接不了登录服务器 vm虚拟化服务器 apache 重启服务器 网络安全班会体会 普陀区网络软件开发协议 网络安全意识教育活动方案
0