haproxy七层代理总结

打印 上一主题 下一主题

主题 578|帖子 578|积分 1734

一、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:
  1. yum  install  nginx  -y
  2.         echo   webserver1 - 172.25.254.10  >  /user/share/nginx/html/index.html
  3.         systemctl  enable  --now  nginx
  4.         systemctl  stop  firewalld
复制代码
server2:
  1.        yum  install  nginx  -y
  2.         echo   webserver2 - 172.25.254.20  >  /user/share/nginx/html/index.html
  3.         systemctl  enable  --now  nginx
  4.         systemctl  stop  firewalld
复制代码
测试:
  1.     curl  172.25.254.20
复制代码
haproxy:

  1. 安装haproxy
  2. dnf  install  haproxy  -y
  3. 修改/etc/haproxy/haproxy.cfg文件配置
  4. vim /haproxy/haproxy.cfg
复制代码
修改如下:
  1. #
  2. #---------------------------------------------------------------------
  3. # main frontend which proxys to the backends
  4. #---------------------------------------------------------------------
  5. frontend webcluster
  6.     bind *:80
  7.     mode http
  8.     use_backend webcluster-host
  9. backend webcluster-host
  10.     balance roundrobin
  11.     server web1 172.25.254.10:80
  12.     server web2 172.25.254.20:80
复制代码
重启服务:
  1. systemctl  restart  haproxy
复制代码
总测试:
测客户机是否可以ping....
  1. [root@localhost ~]# curl 172.25.254.100
  2. webserver1 - 172.25.254.10
  3. [root@localhost ~]# curl 172.25.254.100
  4. webserver2 - 172.25.254.20
  5. [root@localhost ~]# curl 172.25.254.100
  6. webserver1 - 172.25.254.10
复制代码
测server的nginx:
将server1,2的nginx全部停止:
  1. [root@localhost ~]# curl 172.25.254.100
  2. webserver2 - 172.25.254.20
  3. [root@localhost ~]# curl 172.25.254.100
  4. webserver2 - 172.25.254.20
  5. [root@localhost ~]# curl 172.25.254.100
  6. webserver2 - 172.25.254.20
复制代码
在将其重启:
  1. [root@localhost ~]# curl 172.25.254.100
  2. webserver1 - 172.25.254.10
  3. [root@localhost ~]# curl 172.25.254.100
  4. webserver2 - 172.25.254.20
  5. [root@localhost ~]# curl 172.25.254.100
  6. webserver1 - 172.25.254.10[root@localhost ~]# curl 172.25.254.100webserver2 - 172.25.254.20[root@localhost ~]#
复制代码
四.haproxy的基本配置
配置文件位置:
/etc/haproxy/haproxy.cfg

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

多进程:
  1. vim/etc/haproxy/haproxy.cfg
复制代码

多线程:

五.proxies配置
参数说明:

  1. defaults
  2.     mode                    http    # http 七层
  3.     log                     global  # 延用上面的设定
  4.     option                  httplog # http 的日志
  5.     option                  dontlognull  # 空连接的日志
  6.     option http-server-close
  7.     option forwardfor       except 127.0.0.0/8
  8.     option                  redispatch
  9.     retries                 3
  10.     timeout http-request    10s
  11.     timeout queue           1m
  12.     timeout connect         10s
  13.     timeout client          1m
  14.     timeout server          1m
  15.     timeout http-keep-alive 10s
  16.     timeout check           10s
  17.     maxconn                 3000
复制代码
 Proxies配置-defaults:
  1. bind:指定HAProxy的监听地址,可以是IPV4或IPV6,可以同时监听多个IP或端口,可同时用于listen字
  2. 段中
  3. #格式:
  4. 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
实验:-重定向网络实例
进入配置文件
  1. listen webcluster
  2.     bind *:80  
  3.     mode http     
  4.     balance roundrobin
  5.     redirect prefix http://www.baidu.com/[转百度】
  6.     server web1 172.25.254.10:80
  7.     server web2 172.25.254.20:80
复制代码
修改保存
进入浏览器,输入IP地址:


Socat工具:
配置:
  1. listen webcluster
  2.     bind *:80  
  3.     mode http     
  4.     balance roundrobin
  5.     server web1 172.25.254.10:80
  6.     server web2 172.25.254.20:80
复制代码

利用方法说明:
  1. #修改配置文件
  2. [root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
  3. stats socket /var/lib/haproxy/stats mode 600 level admin
  4. #查看帮助
  5. haproxy ~]# socat -h
  6. haproxy ~]# echo "help" | socat stdio /var/lib/haproxy/stats
  7. The following commands are valid at this level:
  8. help : this message
  9. prompt : toggle interactive mode with prompt
  10. quit : disconnect
  11. 。。。省略 。。。
  12. enable server : enable a disabled server (use 'set server' instead) #启用服务器
  13. set maxconn server : change a server's maxconn setting
  14. set server : change a server's state, weight or address #设置服务器
  15. get weight : report a server's current weight #查看权重
  16. set weight : change a server's weight (deprecated) #设置权重
  17. show startup-logs : report logs emitted during HAProxy startup
  18. how peers [peers section]: dump some information about all the peers or this
  19. peers section
  20. set maxconn global : change the per-process maxconn setting
  21. set rate-limit : change a rate limiting value
  22. set severity-output [none|number|string] : set presence of severity level in
  23. feedback information
  24. set timeout : change a timeout setting
  25. show env [var] : dump environment variables known to the process
  26. show cli sockets : dump list of cli sockets
  27. show cli level : display the level of the current CLI session
  28. show fd [num] : dump list of file descriptors in use
  29. 。。。省略 。。。
复制代码
启用backup:

(两台主机没题目时,不会启用)注意
  1. 启用backup   ---- 两台服务器状态OK的情况下,不会访问这个
  2. vim /etc/httpd/conf/httpd.conf --------- 里面修改sorry server的端口
  3. vim/etc/haproxy/haproxy.cfg
  4. listen webcluster
  5.     bind *:80
  6.     mode http
  7.     balance roundrobin    ---- 动态算法
  8.     server web1 172.25.254.10:80 check inter 2 fall 3 rise 5 weight 2
  9.     server web2 172.25.254.20:80 check inter 2 fall 3 rise 5 weight 1
  10.     server web_sorry 172.25.254.100:8080 backup
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

勿忘初心做自己

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表