• Haproxy负载均衡


    ha-proxy概述
    ha-proxy是一款高性能的负载均衡软件。因为其专注于负载均衡这一些事情,因此与nginx比起来在负载均衡这件事情上做更好,更专业。

    ha-proxy的特点

    •支持tcp / http 两种协议层的负载均衡,使得其负载均衡功能非常丰富。
    •支持8种左右的负载均衡算法,尤其是在http模式时,有许多非常实在的负载均衡算法,适用各种需求。
    •性能非常优秀,基于事件驱动的链接处理模式及单进程处理模式(和Nginx类似)让其性能卓越。
    •拥有一个功能出色的监控页面,实时了解系统的当前状况。
    •功能强大的ACL支持,给用户极大的方便。

    1. 拓扑结构
    2. [vip: 192.168.246.17]
    3. [LB1 Haproxy] [LB2 Haproxy]
    4. 192.168.246.169 192.168.246.161
    5. [httpd] [httpd]
    6. 192.168.246.162 192.168.246.163
    7. 一、Haproxy实施步骤
    8. 1. 准备工作(集群中所有主机)
    9. [root@ha-proxy-master ~]# cat /etc/hosts
    10. 127.0.0.1 localhost
    11. 192.168.246.169 ha-proxy-master
    12. 192.168.246.161 ha-proxy-slave
    13. 192.168.246.162 test-nginx1
    14. 192.168.246.163 test-nginx2
    15. 2. RS配置
    16. 配置好网站服务器,测试所有RS,所有机器安装nginx
    17. [root@test-nginx1 ~]# yum install -y nginx
    18. [root@test-nginx1 ~]# systemctl start nginx
    19. [root@test-nginx1 ~]# echo "test-nginx1" >> /usr/share/nginx/html/index.html
    20. # 所有nginx服务器按顺序输入编号,方便区分。
    21. 3. 调度器配置Haproxy(主/备)都执行
    22. [root@ha-proxy-master ~]# yum -y install haproxy
    23. [root@ha-proxy-master ~]# cp -rf /etc/haproxy/haproxy.cfg{,.bak}
    24. [root@ha-proxy-master ~]# sed -i.bak -r '/^[ ]*#/d;/^$/d' /etc/haproxy/haproxy.cfg
    25. [root@ha-proxy-master ~]# vim /etc/haproxy/haproxy .cfg
    26. global
    27. log 127.0.0.1 local2 info
    28. pidfile /var/run/haproxy.pid
    29. maxconn 4000 #优先级低
    30. user haproxy
    31. group haproxy
    32. daemon #以后台形式运行ha-proxy
    33. nbproc 1 #工作进程数量 cpu内核是几就写几
    34. defaults
    35. mode http #工作模式 http ,tcp 是 4 层,http是 7
    36. log global
    37. retries 3 #健康检查。3次连接失败就认为服务器不可用,主要通过后面的check检查
    38. option redispatch #服务不可用后重定向到其他健康服务器。
    39. maxconn 4000 #优先级中
    40. contimeout 5000 #ha服务器与后端服务器连接超时时间,单位毫秒ms
    41. clitimeout 50000 #客户端超时
    42. srvtimeout 50000 #后端服务器超时
    43. listen stats
    44. bind *:80
    45. stats enable
    46. stats uri /haproxy #使用浏览器访问 http://192.168.246.169/haproxy,可以看到服务器状态
    47. stats auth qianfeng:123 #用户认证,客户端使用elinks浏览器的时候不生效
    48. frontend web
    49. mode http
    50. bind *:80 #监听哪个ip和什么端口
    51. option httplog #日志类别 http 日志格式
    52. acl html url_reg -i \.html$ #1.访问控制列表名称html。规则要求访问以html结尾的url
    53. use_backend httpservers if html #2.如果满足acl html规则,则推送给后端服务器httpservers
    54. default_backend httpservers #默认使用的服务器组
    55. backend httpservers #名字要与上面的名字必须一样
    56. balance roundrobin #负载均衡的方式
    57. server http1 192.168.246.162:80 maxconn 2000 weight 1 check inter 1s rise 2 fall 2
    58. server http2 192.168.246.163:80 maxconn 2000 weight 1 check inter 1s rise 2 fall 2
    59. 将配置文件拷贝到slave服务器
    60. [root@ha-proxy-master ~]# scp /etc/haproxy/haproxy.cfg 192.168.246.161:/etc/haproxy/
    61. 两台机器启动设置开机启动
    62. [root@ha-proxy-master ~]# systemctl start haproxy
    63. [root@ha-proxy-master ~]# systemctl enable haproxy

     访问192.168.246.17刷新页面跳转便完成操作,访问haproxy查看监控

    页面主要参数解释
    Queue
    Cur: current queued requests //当前的队列请求数量
    Max:max queued requests     //最大的队列请求数量
    Limit:           //队列限制数量

    Errors
    Req:request errors             //错误请求
    Conn:connection errors          //错误的连接

    Server列表:
    Status:状态,包括up(后端机活动)和down(后端机挂掉)两种状态
    LastChk:    持续检查后端服务器的时间
    Wght: (weight) : 权重
    ========================================================
    2.测试访问
    通过访问haparoxy的ip地址访问到后端服务器
    # curl http://192.168.246.169

  • 相关阅读:
    ASP.NETCore统一处理404错误都有哪些方式?
    数据库数据表设计思想一
    CRD2 值得一读的知识蒸馏与对比学习结合的paper 小陈读paper
    d的is表达式
    Old money风盛行,柯罗芭KLOVA演绎中式奢华
    Python面试题:如何在 Python 中处理大数据集?
    国产低功耗Sub-1G全频段收发一体芯片DP4306遥控器、智能抄表、工业控制等应用。
    为什么要用std::function
    1.9 if语句(Python)
    代码随想录算法训练营第6天 | 242. 有效的字母异位词 | 349. 两个数组的交集 | 202. 快乐数 | 1. 两数之和
  • 原文地址:https://blog.csdn.net/weixin_66519301/article/details/134274231