haproxy
1.介绍
HAProxy提供高可用性、负载均衡以及基于TCP和HTTP应用的代理,支持虚拟主机,它是免费、快速并且可靠的一种解决方案。HAProxy特别适用于那些负载特大的web站点,这些站点通常又需要会话保持或七层处理。HAProxy运行在当前的硬件上,完全可以支持数以万计的并发连接。并且它的运行模式使得它可以很简单安全的整合进您当前的架构中, 同时可以保护你的web服务器不被暴露到网络上。
HAProxy实现了一种事件驱动, 单一进程模型,此模型支持非常大的并发连接数。多进程或多线程模型受内存限制 、系统调度器限制以及无处不在的锁限制,很少能处理数千并发连接,适用于中小型公司。
特点
1、支持两种代理模式:TCP(四层)和HTTP(七层),支持虚拟主机;
2、能够补充Nginx的一些缺点比如Session的保持,Cookie的引导等工作
3、支持url检测后端的服务器出问题的检测会有很好的帮助。
4、更多的负载均衡策略比如:动态加权轮循(Dynamic Round Robin),加权源地址哈希(Weighted Source Hash),加权URL哈希和加权参数哈希(Weighted Parameter Hash)已经实现
5、单纯从效率上来讲HAProxy更会比Nginx有更出色的负载均衡速度。
6、HAProxy可以对Mysql进行负载均衡,对后端的DB节点进行检测和负载均衡。
9、支持负载均衡算法:Round-robin(轮循)、Weight-round-robin(带权轮循)、source(原地址保持)、RI(请求URL)、rdp-cookie(根据cookie)
10、不能做Web服务器即Cache。
2.HAProxy基础配置文件详解:
global:
设置全局配置参数,属于进程的配置,通常是和操作系统相关。
defaults:
配置默认参数,这些参数可以被用到frontend,backend,Listen组件;
frontend:
接收请求的前端虚拟节点,Frontend可以更加规则直接指定具体使用后端的backend;
backend:
后端服务集群的配置,是真实服务器,一个Backend对应一个或者多个实体服务器;
Listen :
frontend和backend的组合体。
3.配置方法
名称 | ip |
---|---|
调度器 | 192.168.6.135 |
R1 | 192.168.6.142 |
R2 | 192.168.6.200 |
#关闭3台虚拟机的防火墙,selinux,配置ym源 #在Rs1,Rs2下载httpd写一个网页文件 [root@rs2 ~]# dnf -y install httpd [root@rs2 ~]# echo 'Rs2' > /var/www/html/index.html [root@rs2 ~]# cd /var/www/html/ [root@rs2 html]# ls index.html [root@rs2 html]# cat index.html Rs1 [root@rs2 html]# systemctl enable --now httpd [root@rs2 html]# ss -antl State Recv-Q Send-Q Local Address:Port Peer Address:Port Process LISTEN 0 128 0.0.0.0:22 0.0.0.0:* LISTEN 0 128 *:80 *:* LISTEN 0 128 [::]:22 [::]:* [root@rs1 ~]# dnf -y install httpd [root@rs1 ~]# echo 'Rs1' > /var/www/html/index.html [root@rs1 ~]# cd /var/www/html/ [root@rs1 html]# ls index.html [root@rs1 html]# cat index.html Rs1 [root@rs1 html]# systemctl enable --now httpd Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service→ /usr/lib/systemd/system/httpd.service. [root@rs1 html]# ss -antl State Recv-Q Send-Q Local Address:Port Peer Address:Port Process LISTEN 0 128 0.0.0.0:22 0.0.0.0:* LISTEN 0 128 [::]:22 [::]:* LISTEN 0 128 *:80 *:*
#下载haproxy [root@haproxy ~]# ls anaconda-ks.cfg haproxy-2.6.0.tar.gz #安装依赖包 [root@haproxy ~]# yum -y install make gcc pcre-devel bzip2-devel openssl-devel systemd-devel #创建用户 [root@haproxy ~]# useradd -r -M -s /sbin/nologin haproxy [root@haproxy ~]# id haproxy uid=993(haproxy) gid=990(haproxy) 组=990(haproxy) #解压 [root@haproxy ~]# tar xf haproxy-2.6.0.tar.gz [root@haproxy ~]# ls anaconda-ks.cfg haproxy-2.6.0 haproxy-2.6.0.tar.gz [root@haproxy ~]# cd haproxy-2.6.0 [root@haproxy haproxy-2.6.0]# ls addons CONTRIBUTING include Makefile src VERSION admin dev INSTALL README SUBVERS BRANCHES doc LICENSE reg-tests tests CHANGELOG examples MAINTAINERS scripts VERDATE #编译并安装 [root@haproxy haproxy-2.6.0]# make -j $(grep 'processor' /proc/cpuinfo |wc -l) \ > TARGET=linux-glibc \ > USE_OPENSSL=1 \ > USE_ZLIB=1 \ > USE_PCRE=1 \ > USE_SYSTEMD=1 [root@haproxy haproxy-2.6.0]# make install PREFIX=/usr/local/haproxy
# 做软连接或者设置环境变量都可以
[root@haproxy haproxy-2.6.0]# ln -s /usr/local/haproxy/sbin/haproxy /usr/sbin/
#配置各个负载的内核参数 [root@haproxy haproxy-2.6.0]# vi /etc/sysctl.conf # sysctl settings are defined through files in # /usr/lib/sysctl.d/, /run/sysctl.d/, and /etc/sysctl.d/. # # Vendors settings live in /usr/lib/sysctl.d/. # To override a whole file, create a new file with the same in # /etc/sysctl.d/ and put new settings there. To override # only specific settings, add a file with a lexically later # name in /etc/sysctl.d/ and put new settings there. # # For more information, see sysctl.conf(5) and sysctl.d(5). net.ipv4.ip_nonlocal_bind = 1 #ip绑定功能打开,绑定一个非本地ip防止报错 net.ipv4.ip_forward = 1 #ip转发的功能打开 [root@haproxy ~]# sysctl -p #生效 net.ipv4.ip_nonlocal_bind = 1 net.ipv4.ip_forward = 1
#提供配置文件 [root@haproxy ~]# mkdir /etc/haproxy #创建一个目录 [root@haproxy ~]# cd /etc/haproxy/ [root@haproxy haproxy]# ls [root@haproxy haproxy]# cat > /etc/haproxy/haproxy.cfg <<EOF > #--------------全局配置---------------- > global > log 127.0.0.1 local0 info # 日志放在这个local0 > #log loghost local0 info > maxconn 20480 #最大连接数 > #chroot /usr/local/haproxy > pidfile /var/run/haproxy.pid > #maxconn 4000 > user haproxy #用户 > group haproxy #组 > daemon #守护模式运行 > #--------------------------------------------------------------------- > #common defaults that all the 'listen' and 'backend' sections will > #use if not designated in their block > #--------------------------------------------------------------------- > defaults #默认的意思 > mode http #代理的http协议 > log global > option dontlognull #不记录空日志 > option httpclose > option httplog > #option forwardfor #转发的 > option redispatch #打包的 > balance roundrobin #算法 rr轮询模式 > timeout connect 10s #链接超时时间10秒 > timeout client 10s #客户端超时时间10秒 > timeout server 10s #服务端超时时间10ian > timeout check 10s #检查超时时间10秒 > maxconn 60000 # 最大连接数 > retries 3 #重试次数 > #--------------统计页面配置------------------ > listen admin_stats #管理界面 > bind 0.0.0.0:8189 #绑定在8189的端口号上 > stats enable #状态 > mode http #模式是http > log global #日志 > stats uri /haproxy_stats #访问格式 > stats realm Haproxy\ Statistics > stats auth admin:admin #用户名和密码 > #stats hide-version > stats admin if TRUE #为真可以访问到 > stats refresh 30s #30秒刷新一下 > #---------------web设置----------------------- > listen webcluster #集群 > bind 0.0.0.0:80 #80端口号 > mode http #用到http协议 > #option httpchk GET /index.html > log global > maxconn 3000 #最大连接数 > balance roundrobin #算法是 > cookie SESSION_COOKIE insert indirect nocache > server web01 192.168.6,142:80 check inter 2000 fall 5 > server web02 192.168.6.200:80 check inter 2000 fall 5 > #server web01 192.168.80.102:80 cookie web01 check inter 2000 fall 5> EOF
haproxy.service文件编写
[root@haproxy haproxy]# cat > /usr/lib/systemd/system/haproxy.service <<EOF > [Unit] > Description=HAProxy Load Balancer > After=syslog.target network.target > > [Service] > ExecStartPre=/usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg -c -q > ExecStart=/usr/local/haproxy/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /var/run/haproxy.pid > ExecReload=/bin/kill -USR2 $MAINPID > > [Install] > WantedBy=multi-user.target > EOF [root@haproxy haproxy]# systemctl daemon-reload [root@haproxy haproxy]# cd [root@haproxy ~]# systemctl status haproxy ● haproxy.service - HAProxy Load Balancer Loaded: loaded (/usr/lib/systemd/system/haproxy.service; disabled; ven> Active: inactive (dead) lines 1-3/3 (END) [root@haproxy ~]# ss -antl State Recv-Q Send-Q Local Address:Port Peer Address:Port Process LISTEN 0 128 0.0.0.0:22 0.0.0.0:* LISTEN 0 128 [::]:22 [::]:*
启用日志
[root@haproxy ~]# vi /etc/rsyslog.conf #加入下面内容 local0.* /var/log/haproxy.log [root@haproxy ~]# systemctl restart rsyslog #重启这个服务
启动服务
[root@haproxy ~]# systemctl restart haproxy [root@haproxy ~]# ss -antl State Recv-Q Send-Q Local Address:Port Peer Address:Port Process LISTEN 0 128 0.0.0.0:8189 0.0.0.0:* LISTEN 0 128 0.0.0.0:80 0.0.0.0:* LISTEN 0 128 0.0.0.0:22 0.0.0.0:* LISTEN 0 128 [::]:22 [::]:*
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 什么是nginx的强缓存和协商缓存
· 一文读懂知识蒸馏
· Manus爆火,是硬核还是营销?