欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 汽车 > 维修 > haproxy七层代理总结

haproxy七层代理总结

2024/10/24 14:27:48 来源:https://blog.csdn.net/m0_75111244/article/details/141116736  浏览:    关键词:haproxy七层代理总结

一、HAProxy概念

1.1 什么是HAProxy?

     HAProxy是一款开源、高性能的负载均衡器和代理服务器,专为TCP和HTTP应用而设计。它可以将客户端的请求分发到多台后端服务器,从而提高应用的可用性和性能。HAProxy支持多种负载均衡算法和健康检查机制,是构建高可用性系统的理想选择。

1.2 HAProxy的优势
高性能:HAProxy采用事件驱动模型,能够处理大量并发连接。
灵活性强:支持多种负载均衡算法和调度策略,适应不同的应用场景。
高可用性:通过健康检查和故障转移机制,确保服务的连续性。
丰富的功能:支持SSL终止、HTTP重写、压缩等多种功能。

二、HAProxy架构
2.1 HAProxy整体架构
HAProxy的整体架构主要包括以下部分:

前端(Frontend):接受客户端请求,并根据配置的规则进行处理。
后端(Backend):定义一组服务器,处理前端转发的请求。
服务器(Server):实际处理请求的后端服务器。
监听器(Listener):在前端监听特定的IP和端口,等待客户端的连接请求。

2.2 HAProxy的组件
配置文件(haproxy.):HAProxy的核心配置文件,定义了前端、后端和监听器等组件。
统计报告(Statistics Report):HAProxy提供丰富的统计信息,便于监控和调试。
日志(Log):HAProxy支持详细的日志记录,帮助分析和诊断问题。

2.3 HAProxy的工作流程
HAProxy的工作流程如下:客户端发送请求到HAProxy的前端。
前端根据配置的规则,选择合适的后端。
后端将请求分发到具体的服务器进行处理。
服务器处理请求并返回结果,通过后端和前端返回给客户端。

三.实验

1.haproxy基本部署负载均衡的实现

实验工具:四台红帽9,网络设置在NAT模式下配置IP

实验要求:

客户机:172.25.254.100(ping通haproxy)

haproxy:172.25.254.100(下载haproxy)

server1:172.25.254.10(nginx配置部署好)

server2:172.25.254.20(同上)

server1:

 yum  install  nginx  -yecho   webserver1 - 172.25.254.10  >  /user/share/nginx/html/index.htmlsystemctl  enable  --now  nginxsystemctl  stop  firewalld

server2:

       yum  install  nginx  -yecho   webserver2 - 172.25.254.20  >  /user/share/nginx/html/index.htmlsystemctl  enable  --now  nginxsystemctl  stop  firewalld

测试:

    curl  172.25.254.20
haproxy:
安装haproxydnf  install  haproxy  -y修改/etc/haproxy/haproxy.cfg文件配置vim /haproxy/haproxy.cfg

修改如下:

#
#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------frontend webclusterbind *:80mode httpuse_backend webcluster-hostbackend webcluster-hostbalance roundrobinserver web1 172.25.254.10:80server web2 172.25.254.20:80

重启服务:

systemctl  restart  haproxy

总测试:

测客户机是否可以ping....

[root@localhost ~]# curl 172.25.254.100
webserver1 - 172.25.254.10
[root@localhost ~]# curl 172.25.254.100
webserver2 - 172.25.254.20
[root@localhost ~]# curl 172.25.254.100
webserver1 - 172.25.254.10

测server的nginx:

将server1,2的nginx全部停止:

[root@localhost ~]# curl 172.25.254.100
webserver2 - 172.25.254.20
[root@localhost ~]# curl 172.25.254.100
webserver2 - 172.25.254.20
[root@localhost ~]# curl 172.25.254.100
webserver2 - 172.25.254.20

在将其重启:

[root@localhost ~]# curl 172.25.254.100
webserver1 - 172.25.254.10
[root@localhost ~]# curl 172.25.254.100
webserver2 - 172.25.254.20
[root@localhost ~]# curl 172.25.254.100
webserver1 - 172.25.254.10
[root@localhost ~]# curl 172.25.254.100
webserver2 - 172.25.254.20
[root@localhost ~]# 

四.haproxy的基本配置

配置文件位置:

/etc/haproxy/haproxy.cfg

参考配置参数说明:

global        # 对全局参数的说明log         127.0.0.1 local2    # 全局的日志配置,使用log关键字,此日志需要借助rsyslog来进行配置,默认等级为infochroot      /var/lib/haproxy       # 改变当前工作目录,基于安全性的考虑pidfile     /var/run/haproxy.pid  # 当前进程pid文件maxconn     4000                # 当前进程最大的连接数,很重要的一个参数,后面有详细的讲解。user        haproxy             # 启动服务所属用户group       haproxy             # 启动服务所属组daemon                            # 开启守护进程运行模式# turn on stats unix socketstats socket /var/lib/haproxy/stats        # haproxy socket文件
全局参数配置及日志分离:

多进程:

vim/etc/haproxy/haproxy.cfg

多线程:

五.proxies配置

参数说明:

defaultsmode                    http    # http 七层log                     global  # 延用上面的设定option                  httplog # http 的日志option                  dontlognull  # 空连接的日志option http-server-closeoption forwardfor       except 127.0.0.0/8option                  redispatchretries                 3timeout http-request    10stimeout queue           1mtimeout connect         10stimeout client          1mtimeout server          1mtimeout http-keep-alive 10stimeout check           10smaxconn                 3000

 Proxies配置-defaults:

bind:指定HAProxy的监听地址,可以是IPV4或IPV6,可以同时监听多个IP或端口,可同时用于listen字
段中
#格式:
bind [<address>]:<port_range> [, ...] [param*]

frontend 参数:
示例:
frontend webcluster
    bind *:80   #----所有80端口都开启
    mode http
    use_backend webcluster-host         #----使用什么后端 

backend 参数:
定义一组后端服务器,backend服务器将被frontend进行调用。
注意: backend 的名称必须唯一,并且必须在listen或frontend中事先定义才可以使用,否则服务无法启动

mode http|tcp #指定负载协议类型,和对应的frontend必须一致
option #配置选项
server #定义后端real server,必须指定IP和端口

server参数:

check #对指定real进行健康状态检查,如果不加此设置,默认不开启检查,只有check后面没有其它配置也可以启用检查功能
      #默认对相应的后端服务器IP和端口,利用TCP连接进行周期性健康性检查,注意必须指定端口才能实现健康性检查

addr <IP> #可指定的健康状态监测IP,可以是专门的数据网段,减少业务网络的流量
port <num> #指定的健康状态监测端口
inter <num> #健康状态检查间隔时间,默认2000 ms
fall <num> #后端服务器从线上转为线下的检查的连续失效次数,默认为3

示例:
backend webcluster-host
    balance roundrobin
    server web1 172.25.254.10:80
    server web2 172.25.254.20:80

实验:-重定向网络实例

进入配置文件

listen webclusterbind *:80  mode http     balance roundrobinredirect prefix http://www.baidu.com/[转百度】server web1 172.25.254.10:80server web2 172.25.254.20:80

修改保存

进入浏览器,输入IP地址:

Socat工具:

配置:

listen webclusterbind *:80  mode http     balance roundrobinserver web1 172.25.254.10:80server web2 172.25.254.20:80

使用方法说明:

#修改配置文件
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
stats socket /var/lib/haproxy/stats mode 600 level admin
#查看帮助
haproxy ~]# socat -h
haproxy ~]# echo "help" | socat stdio /var/lib/haproxy/stats
The following commands are valid at this level:
help : this message
prompt : toggle interactive mode with prompt
quit : disconnect
。。。省略 。。。
enable server : enable a disabled server (use 'set server' instead) #启用服务器
set maxconn server : change a server's maxconn setting
set server : change a server's state, weight or address #设置服务器
get weight : report a server's current weight #查看权重
set weight : change a server's weight (deprecated) #设置权重
show startup-logs : report logs emitted during HAProxy startup
how peers [peers section]: dump some information about all the peers or this
peers section
set maxconn global : change the per-process maxconn setting
set rate-limit : change a rate limiting value
set severity-output [none|number|string] : set presence of severity level in
feedback information
set timeout : change a timeout setting
show env [var] : dump environment variables known to the process
show cli sockets : dump list of cli sockets
show cli level : display the level of the current CLI session
show fd [num] : dump list of file descriptors in use
。。。省略 。。。
启用backup:

(两台主机没问题时,不会启用)注意

启用backup   ---- 两台服务器状态OK的情况下,不会访问这个vim /etc/httpd/conf/httpd.conf --------- 里面修改sorry server的端口vim/etc/haproxy/haproxy.cfglisten webclusterbind *:80mode httpbalance roundrobin    ---- 动态算法server web1 172.25.254.10:80 check inter 2 fall 3 rise 5 weight 2server web2 172.25.254.20:80 check inter 2 fall 3 rise 5 weight 1server web_sorry 172.25.254.100:8080 backup

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com