用多少眼泪才能让你相信 发表于 2024-9-3 21:13:31

全网最详细haproxy设置

Haproxy是法国人Willy Tarreau开辟的一款高性能的TCP和HTTP负载均衡器,具有广泛的功能和特性,使其在负载均衡和反向代理领域备受推许。以下是对Haproxy的详细先容:
一、根本概述

定义:Haproxy是一个开源的高性能的反向代理大概说是负载均衡服务软件,可以或许处置惩罚大量并发连接,提供高可用性和负载均衡功能。
功能:支持双机热备、假造主机、基于TCP和HTTP应用代理等功能,同时内置了监控和统计功能,可以及时获取服务器的状态和性能指标。
应用场景:特别适用于负载特大的web站点,这些站点通常又需要会话保持或七层处置惩罚。
二、核心特性

高性能:
使用异步事件驱动的架构,可以或许处置惩罚非常高的并发连接数,支持数以万计的并发连接。
较低的耽误和高吞吐量,使得Haproxy在处置惩罚大规模网络请求时表现优异。
高可靠性:
提供无单点故障的服务,确保服务的高可用性。
可以或许对后端服务器进行健康检查,及时发现并清除出故障的服务器。
负载均衡:
可以将流量匀称地分配到后端服务器上,提高体系的性能和可扩展性。
支持多种负载均衡算法,如轮询(roundrobin)、最少连接数(leastconn)等,可以根据实际需求选择合适的算法。
会话保持:
支持多种会话保持机制,如基于客户端IP的Hash计算、基于cookie的会话保持等,确保用户请求可以或许被转发到正确的后端服务器上。
SSL闭幕和加速:
可以在前端进行SSL闭幕,解密和加密SSL/TLS流量,从而减轻后端服务器的负担并提拔性能。
可定制性:
提供了丰富的设置选项和机动的扩展机制,使得用户可以根据实际需求进行定制化设置和扩展。
支持多种协议:
支持HTTP、HTTPS、TCP、SMTP等多种协议,可以满足差别场景下的需求。
三、工作模式

Haproxy的工作模式一般有两种:TCP模式和HTTP模式。
TCP模式:实例运行于TCP模式,在客户端和服务器端之间将建立一个全双工的连接,且不会对7层报文做任何范例的检查,只能以简单模式工作。通常为SSL、SSH、SMTP等应用使用。
HTTP模式:实例运行于HTTP模式,客户端请求在转发至后端服务器之前将被深度分析,全部不与RFC格式兼容的请求都会被拒绝。适用于需要HTTP层处置惩罚的场景。
haproxy实验情况的设置

haproxy的网络情况设置
https://i-blog.csdnimg.cn/direct/0d924f9cef1f4ce5a95cc2d6b3c055e0.pngwebserver1和webserver2的网络情况的设置https://i-blog.csdnimg.cn/direct/c2c4a4c4f7974fac8da4a6f2314b18f1.png
https://i-blog.csdnimg.cn/direct/4f681ed53cac4f31ae75c0d6303fe671.png
将webserver1和webserver2的文件导入nginx的默认发布文件https://i-blog.csdnimg.cn/direct/525deffc5ed44cfb93267246b36ef048.png



https://i-blog.csdnimg.cn/direct/de96d99551af4cfebd7f2e0cab1838d1.png
在webserver1和webserver2上分别打开nginx的服务
https://i-blog.csdnimg.cn/direct/727c573f5ba148caa1f01fd579ea8e34.png
https://i-blog.csdnimg.cn/direct/53a407ea602a42bb93867a7b3ed3492d.png
末了在haproxy主机上进行测试,访问成功情况设置就搞定了
https://i-blog.csdnimg.cn/direct/a730408c558d4ab6a2cf229fcfcf44da.png
1.haproxy的根本部署方法以及负载均衡的实现

在haproxy主机上安装haproxyhttps://i-blog.csdnimg.cn/direct/550a256ba7d147ae851002ef544fe1c4.png


查看haproxy的设置文件并进行设置

https://i-blog.csdnimg.cn/direct/afb4a6eb71354a7fa3f01682df7ba866.png
https://i-blog.csdnimg.cn/direct/791b958777f0483dbc18712458216b35.png

2.haproxy的全局设置参数以及日志分离

查看haproxy的默认历程
https://i-blog.csdnimg.cn/direct/aada2835ee064a78984bcc3a9cc8e5b3.png
现在进行多历程的设置
https://i-blog.csdnimg.cn/direct/7559a81e2c974dc18429fda385a6083e.png
开启两个以后查看
https://i-blog.csdnimg.cn/direct/fa14d009dd864be4ae5bffaa75a96dbf.png
https://i-blog.csdnimg.cn/direct/bd4e607dd4c64b06b1800da7ef904538.png
https://i-blog.csdnimg.cn/direct/cad5976b92fd45b2af50ce3578489f57.png
查看子历程中的线程数
https://i-blog.csdnimg.cn/direct/3061c95376324e22b65967fa81ee19b3.png
同时设定多线程和多历程,会产生互斥
https://i-blog.csdnimg.cn/direct/fe837cc6f91045baa588b33dddcc3f79.png
互斥后重启服务就会报错
https://i-blog.csdnimg.cn/direct/c5cb3de2629346b9a3b7c479d1d0980d.png
现在将一个解释掉
https://i-blog.csdnimg.cn/direct/52867a8604414cdb83730f94e2317096.png
然后重启服务后在进行查看
https://i-blog.csdnimg.cn/direct/5d00dccd7231435a88a09f99f0c64dfb.png
自定义haproxy的日志
https://i-blog.csdnimg.cn/direct/290e9a70c603434cbfaaa2f9bb675b2a.png
https://i-blog.csdnimg.cn/direct/d85d2c26ed5e404f99909aca685d0960.png
进入设置文件进行设置,同时还需要开启udp协议
https://i-blog.csdnimg.cn/direct/aa3c6591c81b460ab718bf3bc810df40.png
https://i-blog.csdnimg.cn/direct/38fa4febedc442d3b986f69f76e547e1.png
3.haproxy-proxies中的常用设置参数

check:对指定的real进行健康状态检查  inter:健康状态检查隔断时间 默认为2000ms  fall:后端服务器从线上转为线下的连续失败次数,默认为3 rise:后端服务器从下线恢复上线的检查的连续有效次数,默认为2 weight:默认为1,最大值为256,0(状态为蓝色)表现不加入负载均衡,但仍担当持久连接                   
https://i-blog.csdnimg.cn/direct/9e413cb128574260a678692f020dfa02.png
设置backup,设置为web_sorry还要把webserver1和webserver2的nginx下线

https://i-blog.csdnimg.cn/direct/550e9efdd4d746fa87ad9b9cb886b04e.png
https://i-blog.csdnimg.cn/direct/84eae9aa1dd549199fd8c92ec9f72584.png
先在haproxy主机上安装httpd
https://i-blog.csdnimg.cn/direct/417c5bb2be8142b087521e76e57fb81b.png
进入httpd的设置文件将端口号改为8080
https://i-blog.csdnimg.cn/direct/4524785317974565821494a0045d420c.png
然后开启httpd服务
https://i-blog.csdnimg.cn/direct/3e410a3c7a02475cbf7d0325f482db33.png
给index.html文件写入内容
https://i-blog.csdnimg.cn/direct/901b2400e84d44e297b52ded2d679bc4.png
然后进入haproxy设置文件写入
https://i-blog.csdnimg.cn/direct/803833e3ce9d4d31a486417fdbaa3943.png
然后进行访问
https://i-blog.csdnimg.cn/direct/e4e47527f6404f90becd2e68721fc4ed.png
https://i-blog.csdnimg.cn/direct/90234687b41f491699bdab6f93d9cd25.png

要进行维护先将一台下线,使用disabled:将后端服务器标志为不可用状态,即维护状态
https://i-blog.csdnimg.cn/direct/a44a4201f1004670af54a6a4b57292d0.png
下线的为172.25.254.10,以是访问就没有10
https://i-blog.csdnimg.cn/direct/da2fbe2b133940be864d35c8fecd2477.png
然后进入设置文件将disabled删掉10又可以访问
https://i-blog.csdnimg.cn/direct/ece1fb6429e94aa8a69b7722194068a0.png
https://i-blog.csdnimg.cn/direct/c22061ca76d04f31bd7fd78886941c21.png
redirect prefix http://www.baidu.com/ 表现将请求定向定向到一个指定网站
https://i-blog.csdnimg.cn/direct/bc335aa270bb461cb167647eb4dd2de6.png
在浏览器上访问172.25.254.100 定向到了百度
https://i-blog.csdnimg.cn/direct/6298890cb9304af18a7d00c3e5aa315b.png


maxconn:当前后端server的最大并发连接数
https://i-blog.csdnimg.cn/direct/b31dbe9b402144ea9e4c1f195fd99b90.png
https://i-blog.csdnimg.cn/direct/e6468033414b46f3b3ec042e2c49a35c.png


4.haproxy的热更新方法



给该文件提权进行查看

https://i-blog.csdnimg.cn/direct/0c432c33b277453ab2c26cc46dbd0180.png
https://i-blog.csdnimg.cn/direct/8df96d1b2b0b4564bcdd016b1e4e3cce.png
在haproxy上安装socat(动态调整haproxy中的参数)
https://i-blog.csdnimg.cn/direct/344a35447cf8426a9e963475c10736d7.png
查看haproxy的状态
https://i-blog.csdnimg.cn/direct/68db5a6870e149eb8dd6a05be07616ff.png
查看服务器的状态
https://i-blog.csdnimg.cn/direct/4eceab956962445d9b177c7a96048f98.png
查看当前权重,更改权重为1,更改后查看
https://i-blog.csdnimg.cn/direct/52f769c84d3c407dbfbfc5d1296771ca.png
https://i-blog.csdnimg.cn/direct/b3df4fcd1d194730bb12b7c10a1fc7f2.png
https://i-blog.csdnimg.cn/direct/c691f98ebc9d406cbc3f6841ec7e3d47.png
下线后端服务器下线后查看全为172.25.254.20
https://i-blog.csdnimg.cn/direct/0bea2429cce441ea8e3ebbdb01e0065a.png
上线后端服务器,上线后查看恢复正常
https://i-blog.csdnimg.cn/direct/18040c9ee44d40acbe7f2963f2c7d81d.png
haproxy多历程热处置惩罚
https://i-blog.csdnimg.cn/direct/a93faea95a7c48e28ab7ba7d623f811d.png
重启服务后查看
https://i-blog.csdnimg.cn/direct/52bf5f0cccfb46a0b6313d3b1d80cf77.png
5.haproxy的算法

haproxy的静态算法,是不能修改权重
https://i-blog.csdnimg.cn/direct/1accbc20ff7f4425848884aeb2c4feb7.png

https://i-blog.csdnimg.cn/direct/e2d2fe24db3d4ce490d3d8de9e3332bb.png
haproxy的动态算法
roundrobin算法,是谁空闲了就给谁
https://i-blog.csdnimg.cn/direct/e7e515d858ea4166801205604f1f95b4.png
leastconn:加权最少连接的动态,并且支持慢启动
https://i-blog.csdnimg.cn/direct/7e637d6e31fa4e6f9d81eb03fbaff595.png
https://i-blog.csdnimg.cn/direct/f1139fa5a2424687bcaf281582deb508.png
haproxy的其他算法
source:当后端服务器数据量发生变革时,会导致很多用户的请求转发至新的后端服务器
https://i-blog.csdnimg.cn/direct/aa9ba618e7424d7f8d404434fc49b814.png
进行访问时全部掉到172.25.254.20
https://i-blog.csdnimg.cn/direct/b0840bc8b5874a4fbfa430c78dffc1f8.png
同等性hash:当服务器的总权重发生变革时,对调度结果影响时局部的,不会引起大的变动hash(0)mod n 该hash算法是动态的,支持使用socat等工具进行在线权重调整,支持慢启动
https://i-blog.csdnimg.cn/direct/377e694eb36541eca6c54f7e0eaa1684.png
https://i-blog.csdnimg.cn/direct/90c23424b029464595199d028833bae8.png
uri:基于用户请求的uri的左半部门或整个uri做hash,再将hash结果对总权重进行取模后根据最闭幕果将请求转发到后端指定服务器,适用于后端是缓存服务器场景
https://i-blog.csdnimg.cn/direct/94f6441b56a54f55a15729ef6b66c280.png
https://i-blog.csdnimg.cn/direct/2e978a25a269448882c4bf75426e2152.png
https://i-blog.csdnimg.cn/direct/b7fb82a51ce54e84a1a9142ed6453efe.png
https://i-blog.csdnimg.cn/direct/2aa2649c076d4129bb48ac7ddcf3d80b.png
https://i-blog.csdnimg.cn/direct/bd57eb4c3a644d5580b5651d01685c87.png
进行访问
https://i-blog.csdnimg.cn/direct/8d51b36c63c34f26b0c990115f3a22eb.png
https://i-blog.csdnimg.cn/direct/44a36e5072684813b6c10a0bf9bb4cb3.png
https://i-blog.csdnimg.cn/direct/8abc2dc1786746fb846d3bf366b955a6.png
hdr:针对每个http头部(header)请求中的指定信息做hash
https://i-blog.csdnimg.cn/direct/5ea9be57a9834deb8ee8ec3f2771d00b.png
https://i-blog.csdnimg.cn/direct/ede0b836645640e0b2c698907307f4be.png

6.haproxy的状态页面监控

https://i-blog.csdnimg.cn/direct/3e8d8b29f22145499236b59102554774.png
https://i-blog.csdnimg.cn/direct/340c364626624cf888c435c6a96fa158.png
https://i-blog.csdnimg.cn/direct/96f8d730eff04386ba577e213aeafd39.png
7.haproxy-基于cookie的会话保持

把hash打开
https://i-blog.csdnimg.cn/direct/3eb301e2c0794bf2a62adb711d38d316.png

进行基于cookie的会话保持,cookie value:为当前server指定cookie值,实现基于cookie的会话粘性,相对于基于source地点hash调度算法对客户端的粒度更精准,但同时也加大了haproxy负载
https://i-blog.csdnimg.cn/direct/00ff1cd4e05a4ffd8076c7bf89cc047f.png
https://i-blog.csdnimg.cn/direct/229f5978f5e94a0a886fa698e4af4a73.png
8.haproxy-ip透传

把该条解释掉,进行访问
https://i-blog.csdnimg.cn/direct/1167c17096e94a54bbcf2e9021999929.png
https://i-blog.csdnimg.cn/direct/9ebefd4ef9994a788e275a9f85535d2f.png

在172.25.254.20主机上访问日志,看不到配景支持的服务,把该条打开,做透传,就能看到是100进行访问的
https://i-blog.csdnimg.cn/direct/f64ecb59d7824c4da31ed26a24893b3a.png
https://i-blog.csdnimg.cn/direct/17b79e6019cb4f499093bcb5e2ce20bd.png
https://i-blog.csdnimg.cn/direct/3fd9d4e81e0747de8d74956654371253.png
在httpd上做透传,在webserver1主机大将nginx服务关闭掉,然后安装httpd
https://i-blog.csdnimg.cn/direct/bd3c80e804774be7a0235ce1b8ee46d4.png
https://i-blog.csdnimg.cn/direct/fb34dc56a942444a9447193bccddc5cd.png
https://i-blog.csdnimg.cn/direct/6ff9432a52124465990ccd878d9070e9.png
在haproxy主机中访问
https://i-blog.csdnimg.cn/direct/eeb868a1f9c248d1869f8d71a6ef6dd8.png
在webserver1查看日志编辑httpd主设置文件在haproxy主机上进行访问
https://i-blog.csdnimg.cn/direct/89b4461dc4fc435cbf5b970d5057d47d.png
https://i-blog.csdnimg.cn/direct/c1ba2c87079647f0bf82d2b530643d9a.png
https://i-blog.csdnimg.cn/direct/865723ec985d4de7adef051518c911e9.png
四层代理
https://i-blog.csdnimg.cn/direct/d2fcc61169ea4c71af88b3f89ccdc2e1.png
https://i-blog.csdnimg.cn/direct/9a7e8b9ae60d40718a6fdbc85b5aab28.png
https://i-blog.csdnimg.cn/direct/637e95cb698e4da7ae10bdd3da634ca0.png
在webserver2的nginx设置文件中进行设置
https://i-blog.csdnimg.cn/direct/7a6411c1caf847e4bdd94afec16b71b1.png
https://i-blog.csdnimg.cn/direct/37705ce042264c6f8b9dacdf35a6fdb1.png
再次进行访问就可以访问到20
https://i-blog.csdnimg.cn/direct/5733339ddf764edbaf157abdb3c8642c.png
在webserver2的nginx设置文件中添加
https://i-blog.csdnimg.cn/direct/c79571a4603d4f0aa469015916a48ee1.png
https://i-blog.csdnimg.cn/direct/38cd8573ecb84a02a8173750664869eb.png



9.haproxy的访问控制列表应用



添加ACL列表保存然后进行访问在计算机中查看文件然后进行访问即可
https://i-blog.csdnimg.cn/direct/bf6cfdb237c74ad9942b221a6fb8ba89.png
https://i-blog.csdnimg.cn/direct/2a89b6d0447a486090afe73b60516000.png
https://i-blog.csdnimg.cn/direct/3218afdc582e4482a09445705b7fd1a8.png


10.haproxy-自动筛选错误页面内容

先将webserver1的httpd服务和webserver2的nginx服务都暂停
https://i-blog.csdnimg.cn/direct/9e9d4773114e4f5f8ae4029a226b24b7.png
https://i-blog.csdnimg.cn/direct/3ba2223559bd4683b27c7cddfe7e7c24.png
然后使用浏览器进行访问
https://i-blog.csdnimg.cn/direct/6ee33b6702864aaf9d9a5783b9301ff1.png
来到haproxy主机过滤一下设置文件,找到503的,起首在haproxy文件下建立一个文件,然后在新建文件底下再次新建一个503.http文件在此文件中写入内容把该文件加入haproxy主设置文件中,保持与退出并重启服务
https://i-blog.csdnimg.cn/direct/d63eaa5c882949b4a622a99a265fc589.png
https://i-blog.csdnimg.cn/direct/1daa90c91f334fd1832a585a1aab510f.png
https://i-blog.csdnimg.cn/direct/72fa8c59f8ee41a6bc22dc17bc09e0a1.png
https://i-blog.csdnimg.cn/direct/5df3a2e50bb447ee88e36408b708e280.png
https://i-blog.csdnimg.cn/direct/b31a6d3d6c9b4057927ff3bc6c741011.png
然后在浏览器上再次访问172.25.254.100

https://i-blog.csdnimg.cn/direct/ca41d2f67d11445783b435f768c11e56.png11.haproxy的四层负载示例

起首在webserver1和webserver2上安装数据库
https://i-blog.csdnimg.cn/direct/cbdd853c2b064b62993a3da1e042a85c.png
https://i-blog.csdnimg.cn/direct/336fd288cc1a4d8d948da529c0fd5bab.png
进入数据库设置文件修改id在webserver1和webserver2上都要修改
https://i-blog.csdnimg.cn/direct/6fce043c703342a1bebb17717ef0d0dd.png
https://i-blog.csdnimg.cn/direct/22cc1bd77dfc4bcc8a14a51fd16720aa.png
https://i-blog.csdnimg.cn/direct/adb67ed2497b429891459efc658fe866.png
然后就可以实现长途登录
https://i-blog.csdnimg.cn/direct/e7f42c46222a452398edc9f3c56a1c32.png
进入haproxy的主设置文件添加
https://i-blog.csdnimg.cn/direct/a33b0765940444aa9924aa3af3b46e6b.png
https://i-blog.csdnimg.cn/direct/0819f64595e9407c824d9e4e678de20b.png
https://i-blog.csdnimg.cn/direct/d5babf91bb204113a671efbf57c92f43.png
https://i-blog.csdnimg.cn/direct/936f064cb1274fcc89ff9908a9e355c5.png


12.haproxy-https的加密访问

证书制作
https://i-blog.csdnimg.cn/direct/71f4d11c6ad94a49a849b127654537bf.png
将文件导入,公钥和私钥都有了
https://i-blog.csdnimg.cn/direct/c93f9b23f42846eaac9e9eb6c67e3792.png
https://i-blog.csdnimg.cn/direct/ec4c628a8d19471390f9de78ed5ac8f7.png
进入haproxy主设置文件进行写入
https://i-blog.csdnimg.cn/direct/3e653abda6644da9a2f47f751028d328.png
https://i-blog.csdnimg.cn/direct/a51b7e24a7e54c588b0a3a86e775daa6.png
查看443端口已打开
https://i-blog.csdnimg.cn/direct/e4624911376142e19ad51759fe35a7e3.png
https://i-blog.csdnimg.cn/direct/b9fb74df741941a8a31135fc4a08bcf5.png



免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: 全网最详细haproxy设置