Centos7基于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群集

部署环境如下:
Centos 7基于Haproxy搭建高可用Web群集

准备工作

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地址

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

2)访问第一台nginx服务器

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

3)访问第二台nginx服务器

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

4、部署Haproxy服务器

 

[root@centos03 ~]# yum -y install pcre-devel bzip2-devel    
[root@centos03 ~]# ls
anaconda-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
        #quiet

defaults
        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服务器
Centos 7基于Haproxy搭建高可用Web群集

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

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.30
www      A      192.168.100.30
[root@centos03 ~]# named-checkzone bdqn.com /var/named/bdqn.com.zone 
                         
zone bdqn.com/IN: loaded serial 2019112201
OK
[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地址,打开浏览器使用域名访问即可
Centos 7基于Haproxy搭建高可用Web群集

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

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

二、使用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网站群集已经部署完成,现在可以使用客户端访问
Centos 7基于Haproxy搭建高可用Web群集

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

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

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

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


网页标题:Centos7基于Haproxy搭建高可用Web群集
当前链接:http://myzitong.com/article/gehjhc.html