自学内容网 自学内容网

搭建HAproxy----7层负载均衡集群

目录

一、HAproxy的特点

二、环境准备

1、准备4台linux,关闭防火墙、selinux、配置yum源

2、全部的服务器完成时间统一

3、web1 & web2 创建测试页面

4、haproxy主机安装haproxy

 5、域名解析

6、配置HAproxy

7、启动haproxy

三、测试

1.配置域名解析

3.测试结果


一、HAproxy的特点

1.支持tcp / http 两种协议层的负载均衡,使得其负载均衡功能非常丰富。

2.支持8种左右的负载均衡算法,尤其是在http模式时,有许多非常实在的负载均衡算法,适用各种需求。

3.性能非常优秀,基于事件驱动的链接处理模式及单进程处理模式(和Nginx类似)让其性能卓越。

4.处理模式

单进程处理模式:所有客户端连接全部都由同一个服务进程来处理,目标就是等待连接,来一个分配一个,主要消耗cpu

多线程处理模式:多线程模式消耗内存,会限制并发而且多线程需要进程间通信,也会消耗相当多的cpu资源

5.拥有一个功能出色的监控页面,实时了解系统的当前状况。

6.功能强大的ACL支持,给用户极大的方便。

二、环境准备

1、准备4台linux,关闭防火墙、selinux、配置yum源

此步骤参考该链接前两个步骤https://blog.csdn.net/qq_73990369/article/details/142148343?spm=1001.2014.3001.5501

1.Client

192.168.229.11/24 (真实机或虚拟机均可做客户端)

2.HAproxy

192.168.229.12/24

3.web1

192.168.229.13/24

4.web2

192.168.229.14/24

2、全部的服务器完成时间统一

ntpdate 时间服务器ip         date查看时间

ntpdate ntp.aliyun.com

date

3、web1 & web2 创建测试页面

yum install httpd -y

web1

echo web1 > /var/www/html/index.html

web2

echo web2 > /var/www/html/index.html

启动、开机自启动

systemctl start httpd

systemctl enable httpd

测试

输入  http://192.168.229.13:80

输入  http://192.168.229.14:80

4、haproxy主机安装haproxy

yum install epel-release -y
yum install haproxy -y

 5、域名解析

在haproxy主机

vim /etc/hosts

添加以下内容

192.168.229.13 web1
192.168.229.14 web2

6、配置HAproxy

vim /etc/haproxy/haproxy.cfg 

haproxy 配置中分成五部分内容

global: 设置全局配置参数,属于进程的配置,通常是和操作系统相关。

defaults:配置默认参数,这些参数可以被用到frontend,backend,Listen组件;

frontend:接收请求的前端虚拟节点,Frontend可以更加规则直接指定具体使用后端的backend;

backend:后端服务集群的配置,是真实服务器,一个Backend对应一个或者多个实体服务器;

Listen :frontend和backend的组合体。

配置示例:

global
log 127.0.0.1 local3 info
maxconn 4096
        uid nobody
#       uid 99
        gid nobody
#       gid 99
daemon
nbproc 1
pidfile /run/haproxy.pid
defaults
log   global
mode   http
maxconn 2048
retries 3
optionredispatch
contimeout5000
clitimeout    50000
srvtimeout    50000
#timeout connect 5000
#timeout client 50000
#timeout server 50000
    option abortonclose

    stats uri /admin?stats
    stats realm Private lands
    stats auth admin:password
    stats hide-version

frontend http-in
bind 0.0.0.0:80
mode http
log global
option httplog
option httpclose
     acl html url_reg  -i  \.html$
     use_backend html-server if  html
     default_backend html-server

backend html-server
mode http
balance roundrobin
option httpchk GET /index.html
cookie SERVERID insert indirect nocache
server html-A web1:80 weight 1 cookie 3 check inter 2000 rise 2 fall 5
server html-B web2:80 weight 1 cookie 4 check inter 2000 rise 2 fall 5

7、启动haproxy

systemctl start haproxy.service

三、测试

在Client客户端

1.配置域名解析

vim /etc/hosts

添加以下内容

192.168.229.12 haproxy
yum install -y elinks

3.测试结果

elinks --dump http://haproxy/index.html


原文地址:https://blog.csdn.net/qq_73990369/article/details/142500451

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!