简单了解Haproxy
Haproxy的概念:
Haproxy是一个使用C语言编写的自由及开放源代码软件,其提供高可用性、负载均衡,以及基于TCP和HTTP的应用程序代理。
Haproxy特别适用于那些负载特大的web站点,这些站点通常又需要会话保持或七层处理。Haproxy运行在当前的硬件上,完全可以支持数以万计的并发连接。并且它的运行模式使得它可以很简单安全的整合进您当前的架构中, 同时可以保护你的web服务器不被暴露到网络上。
Haproxy的原理:
HAProxy实现了一种事件驱动, 单一进程模型,此模型支持非常大的并发连接数。多进程或多线程模型受内存限制 、系统调度器限制以及无处不在的锁限制,很少能处理数千并发连接。
事件驱动模型因为在有更好的资源和时间管理的用户空间(User-Space) 实现所有这些任务,所以没有这些问题。
此模型的弊端是,在多核系统上,这些程序通常扩展性较差。这就是为什么他们必须进行优化以使每个CPU时间片(Cycle)做更多的工作
Haproxy的优点:
1. Haproxy支持两种代理模式 TCP(四层)和HTTP(七层),也是支持虚拟主机的。
2. Haproxy的优点能够补充Nginx的一些缺点,比如支持Session的保持,Cookie的引导;同时支持通过获取指定的url来检测后端服务器的状态。
3. Haproxy跟LVS类似,本身就只是一款负载均衡软件;单纯从效率上来讲Haproxy会比Nginx有更出色的负载均衡速度,在并发处理上也是优于Nginx的。
4. Haproxy支持TCP协议的负载均衡转发,可以对MySQL读进行负载均衡,对后端的MySQL节点进行检测和负载均衡,大家可以用LVS+Keepalived对MySQL主从做负载均衡。
5. HAProxy负载均衡策略非常多:Round-robin(轮循)、Weight-round-robin(带权轮循)、source(原地址保持)、RI(请求URL)、rdp-cookie(根据cookie)。
Haproxy的基础配置
global :全局配置段
进程及安全配置相关的参数
性能调整相关参数
Debug参数
proxies :代理配置段
defaults:为frontend, backend, listen提供默认配置
frontend:前端,相当于nginx中的server {}
backend:后端,相当于nginx中的upstream {}
listen:同时拥有前端和后端配置,配置简单,生产推荐使用
下面通过实验来了解我们的Haproxy
实验的环境配置(本次实验依旧使用红帽9,使用VMware来完成)
主机名称 | IP |
客户端 | 本机IP |
Haproxy | eth0:172.168.0.100 |
webserver1 | eth0:172.25.254.10 |
webserver2 | eth0:172.25.254..20 |
webserver1配置:
[root@webserver1 ~]# vmset.sh eth0 172.25.254.10 webserver1.org
[root@webserver1 ~]# yum install nginx -y
[root@webserver1 ~]# echo webserver1 - 172.25.254.10 > /usr/share/nginx/html/index.html
[root@webserver1 ~]# systemctl restart nginx.service
webserver2配置:
[root@webserver2 ~]# vmset.sh eth0 172.25.254.20 webserver2.oeg
[root@webserver2 ~]# yum install nginx -y
[root@webserver2 ~]# echo webserver2 - 172.25.254.20 > /usr/share/nginx/html/index.html
[root@webserver2 ~]# systemctl restart nginx.service
Haproxy配置:
[root@haproxy ~]# vmset.sh eth0 172.25.254.100 haproxy.org
[root@haproxy ~]# vim install haproxy -y 安装haproxy
多线程多进程
# 开启Haproxy
[root@haproxy ~]# systemctl enable --now haproxy.service
# 进入配置文件
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg 修改配置文件
listen webcluster
bind *:80
mode http
balance roundrobin
server webl 172.25.254.10:80
server web2 172.25.254.20:80# 重启服务
[root@haproxy ~]# systemctl restart haproxy.service
测试:
查看多线程
在配置文件修改多进程
查看多进程
日志
proxies配置
参数说明
参数 | 类型 | 作用 |
default[] | proxies | 默认配置项,针对以下的frontend、backend和listen生效,可以多个namet也可以没有name |
frontend | proxies | 前端servername,类似于Nginx的一个虚拟主机server和LVS服务集群 |
backend | proxies | 后端服务器组,等Fnginx的upstream和LVS中的RS服务器 |
listen | proxies | 将frontend和backend合并在一起配置,相对于frontend和backend配置更简洁,生产常用 |
实验以及算法
热处理socat工具
对服务器动态权重和其它状态可以利用 socat 工具进行调整, Socat 是 Linux 下的一个多功能的网络工具,名字来由是Socket CAT,相当于netCAT 的增强版 .Socat 的主要特点就是在两个数据流之间建立双向通道,且支持众多协议和链接方式。如 IP、TCP 、 UDP 、 IPv6 、 Socket 文件等
# 单进程
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
log 127.0.0.1 local2
chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
maxconn 4000
user haproxy
group haproxy
daemon
# turn on stats unix socket
stats socket /var/lib/haproxy/stats mode 600 level admin
# 重启haporxy
[root@haproxy ~]# systemctl restart haproxy.service
# 下载socat工具
[root@haproxy ~]# dnf install socat-1.7.4.1-5.el9.x86_64 -y
[root@haproxy ~]# echo "show servers state" | socat stdio /var/lib/haproxy/stats
1
# be_id be_name srv_id srv_name srv_addr srv_op_state srv_admin_state srv_uweight srv_iweight srv_time_since_last_change srv_check_status srv_check_result srv_check_health srv_check_state srv_agent_state bk_f_forced_id srv_f_forced_id srv_fqdn srv_port srvrecord srv_use_ssl srv_check_port srv_check_addr srv_agent_addr srv_agent_port
2 webcluster 1 web1 172.25.254.10 2 0 1 2 218 6 0 7 7 0 0 0 - 80 - 0 0 - - 0
2 webcluster 2 web2 172.25.254.20 2 0 1 1 103 6 0 7 7 0 0 0 - 80 - 0 0 - - 0
4 static 1 static 127.0.0.1 0 0 1 1 217 8 2 0 6 0 0 0 - 4331 - 0 0 - - 0
5 app 1 app1 127.0.0.1 0 0 1 1 217 8 2 0 6 0 0 0 - 5001 - 0 0 - - 0
5 app 2 app2 127.0.0.1 0 0 1 1 217 8 2 0 6 0 0 0 - 5002 - 0 0 - - 0
5 app 3 app3 127.0.0.1 0 0 1 1 217 8 2 0 6 0 0 0 - 5003 - 0 0 - - 0
5 app 4 app4 127.0.0.1 0 0 1 1 216 8 2 0 6 0 0 0 - 5004 - 0 0 - - 0
[root@haproxy ~]# echo "show info" | socat stdio /var/lib/haproxy/stats
Name: HAProxy
Version: 2.4.17-9f97155
Release_date: 2022/05/13
Nbthread: 2
Nbproc: 1
Process_num: 1
Pid: 2634
Uptime: 0d 0h05m04s
Uptime_sec: 304
Memmax_MB: 0
PoolAlloc_MB: 0
# 多进程
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
# turn on stats unix socket
stats socket /var/lib/haproxy/stats1 mode 600 level admin process 1
stats socket /var/lib/haproxy/stats2 mode 600 level admin process 2
# utilize system-wide crypto-policies
ssl-default-bind-ciphers PROFILE=SYSTEM
ssl-default-server-ciphers PROFILE=SYSTEM
# 开启多进程
nbproc 2
cpu-map 1 0
cpu-map 2 1
[root@haproxy ~]# systemctl restart haproxy.service
[root@haproxy ~]# ll /var/lib/haproxy/
总用量 0
srw------- 1 root root 0 8月 9 11:40 stats
srw------- 1 root root 0 8月 9 11:58 stats1
srw------- 1 root root 0 8月 9 11:58 stats2
haproxy调度算法
1)静态调度算法:
静态算法:按照事先定义好的规则轮询公平调度,不关心后端服务器的当前负载、链接数和响应速度等,且无法实时修改权重,只能靠重启HAProxy生效。
静态算法:static-rr
static-rr:基于权重的轮询调度,不支持权重的运行时调整及后端服务器慢启动,其后端主机数量没有限制。
配置:balance static-rr
静态算法:first
first:根据服务器在列表中的位置,自上而下进行调度,但是其只会当第一台服务器的连接数达到上限,新请求才会分配给下一台服务,因此会忽略服务器的权重设置。
配置:balance first
2)动态调度算法:
基于后端服务器 状态进行调度适当调整,比如优先调度至当前负载较低的服务器,且权重可以在haproxy运行时动态调整无需重启。
动态算法:roundrobin
roundrobin:基于权重的轮询动态调度算法,支持权重的运行时调整,不完全等于lvs中的rr轮训模式,HAProxy中的roundrobin支持慢启动(新加的服务器会逐渐增加转发数),其每个后端backend中最多支持4095个realserver,roundrobin为默认调度算法,且支持对real server权重动态调整。
配置:balance roundrobin
动态算法:leastconn
leastconn加权的最少连接的动态,支持权重的运行时调整和慢启动,即当前后端服务器连接最少的优先调度(新客户端连接),leastconn比较适合长连接的场景使用,比如MySQL等场景。
balance leastconn
3)其他算法:
其他算法:source
源地址hash,基于用户源地址hash并将请求转发到后端服务器,默认为静态即取模方式,但是可以通过hash-type支持的选项更改,后续同一个源地址请求将被转发至同一个后端web服务器,比较适用于session保持/缓存业务等场景。
源地址有两种转发客户端请求到后端服务器的服务器选取计算方式,分别是取模法和一致性hash
配置:
balance source (默认是取模法)
hash-type consistent hash类型:一致性hash
一致性hash示意图
取模法示意图:
其他算法:uri
基于对用户请求的uri做hash并将请求转发到后端指定服务器,也可以通过map-based和consistent定义使用取模法还是一致性hash。
注意:此算法基于应用层,所以只支持 mode http ,不支持 mode tcp
配置:
balance uri
hash-type consistent (uri 一致性hash)
示意图:
其他算法:url_param
url_param对用户请求的url中的 params 部分中的参数name作hash计算,并由服务器总权重相除以后派发至某挑出的服务器;通常用于追踪用户,以确保来自同一个用户的请求始终发往同一个real server
配置:balance url_param name,age #支持对单个及多个url_param值hash(注:一般取一个url_param 值hash,或者一般不使用)
其他算法:hdr
针对用户每个http头部(header)请求中的指定信息做hash,此处由 name 指定的http首部将会被取出并做hash计算,然后由服务器总权重相除以后派发至某挑出的服务器,假如无有效的值,则会使用默认的轮询调度。
配置:
balance hdr(User-Agent)
hash-type consistent
其他算法:rdp-cookie
rdp-cookie对远程桌面的负载,使用cookie保持会话
配置:balance rdp-cookie
算法总结:
# 静态
static-rr--------->tcp/http
first------------->tcp/http
# 动态
roundrobin-------->tcp/http
leastconn--------->tcp/http
# 以下静态和动态取决于 hash_type 是否 consistent
source------------>tcp/http
Uri--------------->http
url_param--------->http
hdr--------------->httpfirst #使用较少
static-rr #做了session共享的web集群
roundrobin
random
leastconn #数据库
source #基于客户端公网IP的会话保持
Uri--------------->http #缓存服务器,CDN服务商
url_param--------->http
hdr #基于客户端请求报文头部做下一步处理
rdp-cookie #很少使用
状态页
状态页配置参考
stats enable # 基于默认的参数启用 stats page
stats hide-version # 将状态页中 haproxy 版本隐藏
stats refresh <delay> # 设定自动刷新时间间隔,默认不自动刷新
stats uri <prefix> # 自定义 stats page uri ,默认值: /haproxy?stats
stats auth <user>:<passwd> # 认证时的账号和密码,可定义多个用户 , 每行指定一个用户
# 默认: no authentication
stats admin { if | unless } <cond> # 启用 stats page 中的管理功能
# 进入配置文件
root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
listen stats
mode http
bind *:9999
stats enable
stats refresh 3
stats uri /status
stats auth fb:fb
[root@haproxy ~]# systemctl restart haproxy.service
测试:
cookie会话保持
配置选项:
cookie name [ rewrite | insert | prefix ][ indirect ] [ nocache ][ postonly ] [
preserve ][ httponly ] [ secure ][ domain ]* [ maxidle <idle> ][ maxlife ]
name : #cookie 的 key 名称,用于实现持久连接
insert : # 插入新的 cookie, 默认不插入 cookie
indirect : # 如果客户端已经有 cookie, 则不会再发送 cookie 信息
ocache: # 当 client 和 hapoxy 之间有缓存服务器(如: CDN )时,不允许中间缓存器缓存 cookie , 因为这会导致很多经过同一个 CDN 的请求都发送到同一台后n端服务
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
listen webcluster
bind *:80
mode http
balance roundrobin
# 添加cookie功能
cookie WEBCOOKIE insert nocache indirect
server web1 172.25.254.10:80 cookie lee1 check inter 2 fall 3 rise 5 weight 1
server web2 172.25.254.20:80 cookie lee2 check inter 2 fall 3 rise 5 weight 1
[root@haproxy ~]# systemctl restart haproxy.service
测试:
[root@haproxy ~]# curl -b WEBCOOKIE=lee1 172.25.254.100
webserver1 - 172.25.254.10
[root@haproxy ~]# curl -b WEBCOOKIE=lee2 172.25.254.100
webserver2 - 172.25.254.20
IP透传
四层透传
[root@webserver1 ~]# vim /etc/nginx/nginx.conf
[root@webserver1 ~]# systemctl restart nginx.service
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
[root@haproxy ~]# systemctl restart haproxy.service
七层透传
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
defaults
mode http
log global
option httplog
option dontlognull
option http-server-close
option forwardfor except 127.0.0.0/8
[root@haproxy ~]# systemctl restart haproxy.service
测试:
ACL的应用实例
ACL
访问控制列表 ACL , Access Control Lists )是一种基于包过滤的访问控制技术
它可以根据设定的条件对经过服务器传输的数据包进行过滤 ( 条件匹配 ) 即对接收到的报文进行匹配和过滤,基于请求报文头部中的源地址、源端口、目标地址、目标端口、请求方法、URL、文件后缀等信息内容进行匹配并执行进一步操作,比如允许其通过或丢弃。
ACL配置选项
# 用 acl 来定义或声明一个 acl
acl <aclname> <criterion> [flags] [operator] [<value>]
acl 名称 匹配规范 匹配模式 具体操作符 操作对象类型
ACL--域名匹配
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
frontend webcluster
bind *:80
mode http
acl domain hdr_dom(host) -i www.fb.orguse_backend webcluster-host if domain
default_backend default-hostbackend webcluster-host
mode http
server web1 172.25.254.10:80backend default-host
mode http
server web2 172.25.254.20:80测试:
[root@client ~]# curl www.fb.org
webserver1 - 172.25.254.10
[root@client ~]# curl www.fb.com
webserver2 - 172.25.254.20
自定义错误页面
haproxy默认使用的错误错误页面
[root@haproxy ~]# rpm -ql haproxy24z-2.4.27-1.el7.zenetys.x86_64 | grep -E http$
/usr/share/haproxy/400.http
/usr/share/haproxy/403.http
/usr/share/haproxy/408.http
/usr/share/haproxy/500.http
/usr/share/haproxy/502.http
/usr/share/haproxy/503.http
/usr/share/haproxy/504.http
[root@haproxy ~]# mkdir /etc/haproxy/errorpage/
[root@haproxy ~]# cp /usr/share/haproxy/503.http /etc/haproxy/errorpage/503.http
[root@haproxy ~]# vim /etc/haproxy/errorpage/503.http[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
也可以将文件命令改为报错后跳转到指定网站
errorloc 503 https://www.baidu.com
https
# 证书制作
[root@haproxy ~]# mkdir -p /etc/haproxy/certs[root@haproxy ~]# openssl req -newkey rsa:2048 -nodes -sha256 -keyout /etc/haproxy/certs/timinglee.org.key -x509 -days 365 -out /etc/haproxy/certs/timinglee.org.crt
# 将证书添加进去
[root@haproxy ~]# ls /etc/haproxy/certs/ timinglee.org.crt timinglee.org.key
[root@haproxy ~]# cat /etc/haproxy/certs/timinglee.org.key /etc/haproxy/certs/timinglee.org.crt > /etc/haproxy/certs/timinglee.pem
[root@haproxy ~]# cat /etc/haproxy/certs/timinglee.pem
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
[root@haproxy ~]# systemctl restart haproxy.service
测试:
全站加密
redirect scheme https if !{ ssl_fc }
Haproxy七层负载均衡差不多就这么多内容,主要还是算法,配置命令其实不是很难,我们下去要多吃透算法,这样有助于我们更好的掌握