Centos7安装haproxy2.7实现4层与7层负载均衡 |
| 发表者:admin分类:应用服务2023-01-09 11:52:44 阅读[1139] |
Centos7安装haproxy2.7实现4层与7层负载均衡
一,集群方案简介
1,集群
1.1 负载均衡集群
lvs 四层负载nginx 4层负载,七层负载 haproxy 四层或七层负载slb 云主机服务,付费F5 硬件设备,付费
1.2 高可用集群
keepalived
2,haproxy简介
HAProxy是一个使用C语言编写的自由及开放源代码软件,其提供高可用性、负载均衡,以及基于TCP和HTTP的应用程序代理。HAProxy是一个免费的负载均衡软件,可以运行于大部分主流的Linux操作系统上。HAProxy提供了L4(TCP)和L7(HTTP)两种负载均衡能力,具备丰富的功能。HAProxy的社区非常活跃,版本更新快速。最关键的是,HAProxy具备媲美商用负载均衡器的性能和稳定性。
3,HAProxy的核心能力和关键特性
3.1 HAProxy的核心功能
- 负载均衡:L4和L7两种模式,支持RR/静态RR/LC/IP Hash/URI Hash/URL_PARAM Hash/HTTP_HEADER Hash等丰富的负载均衡算法
- 健康检查:支持TCP和HTTP两种健康检查模式
- 会话保持:对于未实现会话共享的应用集群,可通过Insert Cookie/Rewrite Cookie/Prefix Cookie,以及上述的多种Hash方式实现会话保持
- SSL:HAProxy可以解析HTTPS协议,并能够将请求解密为HTTP后向后端传输
- HTTP请求重写与重定向
- 监控与统计:HAProxy提供了基于Web的统计信息页面,展现健康状态和流量数据。基于此功能,使用者可以开发监控程序来监控HAProxy的状态
3.2 HAProxy的关键特性
3.2.1 性能
- 采用单线程、事件驱动、非阻塞模型,减少上下文切换的消耗,能在1ms内处理数百个请求。并且每个会话只占用数KB的内存。
- 大量精细的性能优化,如O(1)复杂度的事件检查器、延迟更新技术、Single-buffereing、Zero-copy forwarding等等,这些技术使得HAProxy在中等负载下只占用极低的CPU资源。
- HAProxy大量利用操作系统本身的功能特性,使得其在处理请求时能发挥极高的性能,通常情况下,HAProxy自身只占用15%的处理时间,剩余的85%都是在系统内核层完成的。
- 单个HAProxy进程的处理能力突破了10万请求/秒,并轻松占满了10Gbps的网络带宽。
3.2.2 稳定性
作为建议以单进程模式运行的程序,HAProxy对稳定性的要求是十分严苛的。HAProxy在13年间从未出现过一个会导致其崩溃的BUG,HAProxy一旦成功启动,除非操作系统或硬件故障,否则就不会崩溃。
二,HaProxy配置
1,环境介绍
| 应用名称 | 负载均衡IP | 负载均衡服务端口 | HAproxy工作模式 | 后端IP | 后端服务端口 | 后端服务软件 | 主机系统 | |
|---|---|---|---|---|---|---|---|---|
| haproxy | 192.168.1.247 | 8087 | 四层tcp | 192.168.1.252 | 8081 ,8082 | redis | Centos7 | |
| haproxy | 192.168.1.247 | 8088 | 七层http | 192.168.1.252 | 6379,6380 | nginx | Centos7 | |
| haproxy | 192.168.1.247 | 8089/stats | 监控页面 | Centos7 |
这里 192.168.1.252主机已经安装好docker环境,使用docker安装了两个nginx,两个redis。
haproxy 通过代理tcp端口8087负载到redis,通过http 8088 负载到nginx。8089端口使用监控页面。
2,编译过程
2.1 系统最小化安装与配置
yum groups install "Development Tools"# 安装开发环境工具包
yum install -y pcre pcre-devel openssl-devel systemd-devel# 安装依赖软件包
rpm -ivh http://www.nosuchhost.net/~cheese/fedora/packages/epel-7/x86_64/cheese-release-7-1.noarch.rpmyum install -y lua lua-devel# 更新lua软件包
[root@os-247 ~ ]#cat /etc/selinux/config SELINUX=disabledSELINUXTYPE=targeted #关闭selinux,并重启系统
wget https://www.haproxy.org/download/2.7/src/haproxy-2.7.1.tar.gztar -zxvf haproxy-2.7.1.tar.gz -C /data/cd /data/haproxy-2.7.1make -j $(grep 'processor' /proc/cpuinfo |wc -l) TARGET=linux-glibc USE_OPENSSL=1 USE_ZLIB=1 USE_PCRE=1 USE_SYSTEMD=1make install PREFIX=/data/haproxymkdir /data/haproxy/etc/touch /data/haproxy/etc/haproxy.cfg# 将haproxy软件安装到 /data/haproxy目录,当然也可以安装到默认位置,再创建配置文件 /data/haproxy/etc/haproxy.cfg# 配置文件内容参考后面文章
echo "net.ipv4.ip_nonlocal_bind = 1" >> /etc/sysctl.d/99-sysctl.conf echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.d/99-sysctl.conf sysctl -p# 配置系统内核参数,启用数据包转发
2.2 日志配置
日志文件配置
vim /etc/rsyslog.conf
# 去掉下列注释:# Provides UDP syslog reception$ModLoad imudp$UDPServerAddress *$UDPServerRun 514
# Provides TCP syslog reception$ModLoad imtcp$InputTCPServerRun 514
# 在添加如下内容:
# Save haproxy messages also to haproxy.loglocal2.* /var/log/haproxy.log
# 重起日志服务systemctl enable --now rsyslog systemctl restart rsyslog
# 启动haproxy服务systemctl restart haproxy.service
3,配置文件解析
#--------------全局配置----------------global log 127.0.0.1:514 local2 info # 日志级别: emerg alert crit err warning notice info debug,采用info会打印大量日志,建议采用notice,日志发送到本机rsyslog服务 maxconn 51200 # 最大连接数 chroot /data/haproxy/empty# pidfile /data/haproxy/haproxy.pid # 启动服务配置此次项,这里配置无效 user haproxy group haproxy daemon ulimit-n 1020000#---------------------------------------------------------------------#common defaults that all the 'listen' and 'backend' sections will#use if not designated in their block#---------------------------------------------------------------------defaults mode http #默认的模式mode { tcp|http|health },tcp是4层,http是7层,health只会返回OK log global option dontlognull option redispatch #当serverId对应的服务器挂掉后,强制定向到其他健康的服务器 option abortonclose #当服务器负载很高的时候,自动结束掉当前队列处理比较久的链接 option httplog #日志类别,采用httplog option httpclose #每次请求完毕后主动关闭http通道,ha-proxy不支持keep-alive,只能模拟这种模式的实现# option forwardfor #如果后端服务器需要获得客户端真实ip需要配置的参数,可以从Http Header中获得客户端ip balance roundrobin ##banlance roundrobin 轮询,balance source 保存session值,支持static-rr,leastconn,first,uri等参数 timeout connect 5000ms #连接超时 可用单位 ms,s等 timeout client 30s #客户端超时 timeout server 30s #服务器超时 #timeout check 2000 #=心跳检测超时# maxconn 60000 #默认的最大连接数 retries 3 #三次连接失败就认为是服务器不可用,也可以通过后面设置 #--------------统计页面配置------------------listen stats bind 0.0.0.0:8089 stats enable mode http log global #option httplog #采用http日志格式 stats uri /stats #统计页面url stats realm Haproxy\ Statistics #统计页面密码框上提示文本 stats auth admin:admin #统计页面用户名和密码设置 stats hide-version #隐藏统计页面上HAProxy的版本信息 stats admin if TRUE stats refresh 30s #统计页面自动刷新时间#---------------web设置-----------------------
listen webcluster bind *:8088 mode http option httpchk GET /index.html #后端主机健康检测采用七层http协议检测,server中需要添加check,如果后台index.html访问不到就不再分发给它,也可以不用填写访问路径 log global #日志采用全局配置模式 maxconn 6000 option forwardfor option httpclose balance roundrobin cookie SESSION_COOKIE insert indirect nocache #会话保持策略,insert名为"SESSION_COOKIE"的cookie ,请求定向到该server时会在响应中写入cookie值www02那么只会转到web02主机 server web01 192.168.1.252:8802 weight 2 cookie www01 check maxconn 4000 server web02 192.168.1.252:8801 weight 4 cookie www02 check inter 2000 fall 3 rise 2 maxconn 4000 #check后指定inter, rise, fall三个参数,分别代表健康检查的周期、连续几次成功认为server UP,连续几次失败认为server DOWN,默认值是inter 2000ms rise 2 fall 3,可以省略
frontend webserver bind *:8086 option forwardfor maxconn 10 use_backend server03 default_backend webserver #这里指导默认使用后端主机
backend webserver fullconn 6 balance roundrobin option httpchk GET /index.html# default-server check #采用默认检测配置,会使用4层协议检测后端健康状态,server添加check也是采用4层协议检测 server webs01 192.168.1.252:8803 maxconn 3 minconn 1 check server webs02 192.168.1.252:8804 maxconn 3 minconn 1 check
listen rediscluster bind 192.168.1.247:8087 mode tcp option tcplog maxconn 10000 fullconn 8000 balance leastconn # tcp代理建议使用最小连接 timeout server 15s timeout connect 15s# default-server check #采用默认检测配置,tcp转发会使用4层协议检测后端健康状态,server添加check也是采用4层协议检测 server redis01 192.168.1.252:6381 weight 2 check maxconn 5000 server redis02 192.168.1.252:6380 weight 2 check maxconn 5000 listen https bind 0.0.0.0:443 mode tcp option tcp-check #这里是将ssl当成tcp转发 tcp-check connect option tcplog balance roundrobin server web01 192.168.1.252:5500 check inter 2s fall 3 rise 3 server web02 192.168.1.252:8001 check inter 2s fall 3 rise 3
frontend httpsite bind *:8090 bind *:80 maxconn 800 option forwardfor # acl第一次匹配后,后面的不再生效 acl cluster01 hdr_dom(host) web01.cc web02.cc #创建规则cluster01,匹配域名web01.cc,如Host: web01.com:8090 acl cluster02 hdr_dom(host) web03.cc #创建规则cluster02,匹配域名web03.cc acl cls03 hdr_dom(host) -m beg web. acl www01 hdr_beg(host) -m beg www01. acl www02 hdr_beg(host) www02. img. ftp. acl www03 hdr_dom(host) www03. acl www04 hdr(host) -m beg www04. txt # -m 匹配模式,参数:beg,end,str,dir,dom等 acl www05 hdr_beg(host) -i www05. # -i,忽略大小写 # acl cls06 hdr_end(host) -i -m sub .com # 使用hdr_end(host) 需要使用-m sub,字符串中查找模式,但不是匹配以.com结尾,如: www.comm.link也匹配,效果类似hdr_reg(host) -i \.com# acl cls07 hdr(host) -i -m end .link:8090 # header中指定匹配内容end ,Host参数中端口号也要匹配# acl cls08 hdr_end(host) -i .com:8090 # header中指定匹配内容end ,Host参数中端口号也要匹配 acl cls09 hdr_reg(host) -i \.com # 正规匹配.com acl cls10 hdr_dom(host) -i -m reg \.cn$ # 正规匹配.cn结尾,但是只能使用443或是80端口# acl ui_url path_beg -i /ui/ acl img_url path_beg -i /img acl htm path_end -i .htm .php .jsp acl html path_end -i .html use_backend serverweb01 if cluster01 #当访问域名web01.cc时适用规则cluster01,转发到后端serverweb01主机上 redirect prefix http://micoder.cc if cluster02 #匹配后跳转,类似于rewrite# use_backend serverweb02 if cluster02 #当访问域名web03.cc时适用规则cluster02,转发到后端serverweb03主机上 use_backend serverweb02 if { path_beg /ui/ } #这里配置匹配规则时,acl就不用配置 use_backend serverweb01 if img_url use_backend serverweb01 if cls03 use_backend serverweb01 if www01 use_backend serverweb01 if www02 use_backend serverweb02 if www03 use_backend serverweb02 if www04# use_backend serverweb01 if cls06# use_backend serverweb01 if cls07# use_backend serverweb01 if cls08 use_backend serverweb01 if cls09 use_backend serverweb01 if cls10# use_backend serverweb01 if { path_end .html } #这里配置匹配规则时,acl就不用配置# use_backend serverweb02 if htm
backend serverweb01 fullconn 6 balance roundrobin option httpchk GET /index.html server webs01 192.168.1.252:8803 maxconn 3 minconn 1 check server webs02 192.168.1.252:8804 maxconn 3 minconn 1 check backend serverweb02 fullconn 6 balance roundrobin option httpchk GET /index.html server webs01 192.168.1.252:8801 maxconn 3 minconn 1 check server webs02 192.168.1.252:8802 maxconn 3 minconn 1 check backend server03 fullconn 6 balance roundrobin option httpchk GET /index.html server webs01 192.168.1.252:8801 redir http://www.s3b.cn:8801 weight 10 rise 3 fall 3 check inter 2000 server webs02 192.168.1.252:8802 redir http://www.s3c.cn:8802 weight 10 rise 3 fall 3 check inter 2000 # 匹配后端主机后,转发到域名
# 定义ACL匹配规范# hdr([<name> [,<occ>]]):完全匹配字符串,header的指定信息# hdr_beg([<name> [,<occ>]]):前缀匹配,header中指定匹配内容的begin# hdr_end([<name> [,<occ>]]):后缀匹配,header中指定匹配内容end ,Host参数中端口号也要匹配# hdr_dom([<name> [,<occ>]]):域匹配,header中的domain name# hdr_dir([<name> [,<occ>]]):路径匹配,header的uri路径# hdr_len([<name> [,<occ>]]):长度匹配,header的长度匹配# hdr_reg([<name> [,<occ>]]):正则表达式匹配,自定义表达式(regex)模糊匹配# hdr_sub([<name> [,<occ>]]):子串匹配,header中的uri模糊匹配# dst 目标IP# dst_port 目标PORT# src 源IP# src_port 源PORT# 示例:# hdr(<string>) 用于测试请求头部首部指定内容# hdr_dom(host) 请求的host名称,如 www.magedu.com# hdr_beg(host) 请求的host开头,如 www. img. video. download. ftp.# hdr_end(host) 请求的host结尾,如 .com .net .cn# path_beg 请求的URL开头,如/static、/images、/img、/css# path_end 请求的URL中资源的结尾,如 .gif .png .css .js .jpg .jpeg# 有些功能是类似的,比如以下几个都是匹配用户请求报文中host的开头是不是www:# acl short_form hdr_beg(host) www.# acl alternate1 hdr_beg(host) -m beg www.# acl alternate2 hdr_dom(host) -m beg www.# acl alternate3 hdr(host) -m beg www.
# 字符比较:# -exact match (-m str):# 字符串必须完全匹配模式# -substring match (-m sub):# 在提取的字符串中查找模式,如果其中任何一个被发现,ACL将匹配# -prefix match (-m beg):# 在提取的字符串首部中查找模式,如果其中任何一个被发现,ACL将匹配# -suffix match (-m end):# 将模式与提取字符串的尾部进行比较,如果其中任何一个匹配,则ACL进行匹配# -subdirmatch (-m dir):# 查看提取出来的用斜线分隔("/")的字符串,如果其中任何一个匹配,则ACL进行匹配# -domain match (-m dom):# 查找提取的用点(".")分隔字符串,如果其中任何一个匹配,则ACL进行匹配
# 多个acl作为条件时的逻辑关系:# 与:# 隐式(默认)使用,如"if valid_src valid_port" # 或:# 使用"or"或"||"表示,如"if invalid_src || invalid_port" # 否定:# 使用"!"表示,如"if ! invalid_src"
4,服务脚本
#创建service文件[root@centos7 haproxy]# vim /usr/lib/systemd/system/haproxy.service[Unit]Description=HAProxy Load BalancerAfter=syslog.target network.target
[Service]ExecStartPre=/data/haproxy/sbin/haproxy -f /data/haproxy/etc/haproxy.cfg -c -qExecStart=/data/haproxy/sbin/haproxy -Ws -f /data/haproxy/etc/haproxy.cfg -p /var/run/haproxy.pidExecReload=/bin/kill -USR2
[Install]WantedBy=multi-user.target [root@centos7 haproxy] # systemctl daemon-reload
-f 指定配置文件-c 检查-q 静默模式-p 指定pid
三,配置选项解析
1,haproxy配置文件haproxy.cfg
haproxy的配置文件haproxy.cfg由两大部分组成,分别是global和proxies部分global:全局配置段 进程及安全配合相关的参数 性能调整相关参数 DEBUG参数rpoxies:代理配置段 defaults:为frontend,backend,listen提供默认配置 frontend:前端,相当于nginx中的server {} backend:后端,相当于nginx中的upstream {} listen:同时拥有前端和后端配置,配置简单,生产推荐使用
2,global配置参数说明
chroot #锁定运行目录deamon #以守护进程运行stats socket /var/lib/haproxy/haproxy.sock mode 600 level admin process 1 #socket文件user, group, uid, gid #运行haproxy的用户身份nbproc n #开启的haproxy worker进程数,一般和cpu个数匹配cpu-map 1 0 #绑定haproxy worker 进程至指定CPU,将第1个work进程绑定至0号CPUcpu-map 2 1 #绑定haproxy worker 进程至指定CPU,将第2个work进程绑定至1号CPUmaxconn n #每个haproxy进程的最大并发连接数maxconnrate n #每个进程每秒创建的最大连接数量spread-checks n #后端server状态check随机提前或延迟百分比时间,建议2-5(20%-50%)之间,默认值0pidfile #指定pid文件路径log 127.0.0.1 local2 info #定义全局的syslog服务器;日志服务器需要开启UDP协议,最多可以定义两个
3,proxies配置
defaults [<name>] #默认配置项,针对以下的frontend、backend和listen生效,可以多个name也可以没有namefrontend <name> #前端servername,类似于Nginx的一个虚拟主机 server和LVS服务集群。backend <name> #后端服务器组,等于nginx的upstream和LVS中的RS服务器listen <name> #将frontend和backend合并在一起配置,相对于frontend和backend配置更简洁,生产常用
4,proxies配置-defaults
defaults 配置参数,不需要改动:option redispatch #当server Id对应的服务器挂掉后,强制定向到其他健康的服务器,重新派发option abortonclose #当服务器负载很高时,自动结束掉当前队列处理比较久的连接,针对业务情况选择开启option http-keep-alive #开启与客户端的会话保持option forwardfor #透传客户端真实IP至后端web服务器mode http|tcp #设置默认工作类型,使用TCP服务器性能更好,减少压力timeout http-keep-alive 120s #session 会话保持超时时间,此时间段内会转发到相同的后端服务器timeout connect 120s #客户端请求从haproxy到后端server最长连接等待时间(TCP连接之前),默认单位mstimeout server 600s #客户端请求从haproxy到后端服务端的请求处理超时时长(TCP连接之后),默认单位ms,如果超时,会出现502错误,此值建议设置较大些,访止502错误timeout client 600s #设置haproxy与客户端的最长非活动时间,默认单位ms,建议和timeout server相同timeout check 5s #对后端服务器的默认检测超时时间default-server inter 1000 weight 3 #指定后端服务器的默认设置
5,proxies配置-listen简化版配置
使用listen替换 frontend和backend的配置方式,可以简化设置,通常只用于TCP协议的应用
[root@centos7 ~]# vim /etc/haproxy/haproxy.cfglisten WEB_PORT_80 bind 10.0.0.7:80 mode http option forwardfor #透传客户端真实IP至后端web服务器 server web1 10.0.0.18:80 check inter 3000 fall 2 rise 5
6,proxies配置-fronted
frontend 配置参数:bind: #指定haproxy的监听地址,可以同时监听多个IP或端口,可同时用于listen字段中#格式bind [<address>]:<port_range> [, ...] [param*]#注意:如果需要绑定在非本机的IP,需要开启内核参数:net.ipv4.ip_nonlocal_bind=1backlog <backlog> #针对所有server配置,当前端服务器的连接数达到上限后的后援队列长度,注意:不支持backend范例:listen http_proxy #监听http的多个IP的多个端口和sock文件 bind :80,:443,:8801-8810 bind 10.0.0.1:10080,10.0.0.1:10443 bind /var/run/ssl-frontend.sock user root mode 600 accept-proxylisten http_https_proxy #https监听 bind :80 bind :443 ssl crt /etc/haproxy/site.pem #公钥和私钥公共文件listen http_https_proxy_explicit #监听ipv6、ipv4和unix sock文件 bind ipv6@:80 bind ipv4@public_ssl:443 ssl crt /etc/haproxy/site.pem bind unix@ssl-frontend.sock user root mode 600 accept-proxylisten external_bind_app1 #监听file descriptor bind "fd@${FD_APP1}"生产范例:frontend magedu_web_port #可以采用后面形式命名:业务-服务-端口号 bind :80,:8080 bind 10.0.0.7:10080,:8801-8810,10.0.0.17:9001-9010 mode http|tcp #指定负载协议类型 use_backend <backend_name> #调用的后端服务器组名称
7,proxies配置-backend
定义一组后端服务器,backend服务器将被frontend进行调用。注意: backend 的名称必须唯一,并且必须在listen或frontend中事先定义才可以使用,否则服务无法启动mode http|tcp #指定负载协议类型,和对应的frontend必须一致option #配置选项server #定义后端real server,必须指定IP和端口注意:option后面加 httpchk,smtpchk,mysql-check,pgsql-check,ssl-hello-chk方法,可用于实现更多应用层检测功能。server 配置#针对一个server配置check #对指定real进行监控状态检查,默认不开启 addr <IP> #可指定的健康状态监测IP,可以是专门的数据网段,减少业务网络的流量 port <num> #指定的健康状态监测端口 inter <num> #健康状态检查间隔时间,默认2000 ms fall <num> #后端服务器从线上转为线下的检查的连续失效次数,默认为3 rise <num> #后端服务器从下线恢复上线的检查的连续有效次数,默认为2weight <weight> #权重,默认为1backup #将后端服务器标记为备份状态,只有所有服务器宕机时提供服务,类似sorry serverdisabled #将后端服务器标记为不可用状态redir http://www.baiud.com #临时重定向(302)至其它URL,只适用于http模式maxconn <maxconn> #当前后端server的最大并发连接数
四,HAProxy关键配置详解
总览
HAProxy的配置文件共有5个域
- global:用于配置全局参数
- default:用于配置所有frontend和backend的默认属性
- frontend:用于配置前端服务(即HAProxy自身提供的服务)实例
- backend:用于配置后端服务(即HAProxy后面接的服务)实例组
- listen:frontend+backend的组合配置,可以理解成更简洁的配置方法
global域的关键配置
- daemon:指定HAProxy以后台模式运行,通常情况下都应该使用这一配置
- user [username] :指定HAProxy进程所属的用户
- group [groupname] :指定HAProxy进程所属的用户组
- log [address] [device] [maxlevel] [minlevel]:日志输出配置,如log 127.0.0.1 local0 info warning,即向本机rsyslog或syslog的local0输出info到warning级别的日志。其中[minlevel]可以省略。HAProxy的日志共有8个级别,从高到低为emerg/alert/crit/err/warning/notice/info/debug
- pidfile :指定记录HAProxy进程号的文件绝对路径。主要用于HAProxy进程的停止和重启动作。
- maxconn :HAProxy进程同时处理的连接数,当连接数达到这一数值时,HAProxy将停止接收连接请求
frontend域的关键配置
- acl [name] [criterion] [flags] [operator] [value]:定义一条ACL,ACL是根据数据包的指定属性以指定表达式计算出的true/false值。如"acl url_ms1 path_beg -i /ms1/"定义了名为url_ms1的ACL,该ACL在请求uri以/ms1/开头(忽略大小写)时为true
- bind [ip]:[port]:frontend服务监听的端口
- default_backend [name]:frontend对应的默认backend
- disabled:禁用此frontend
- http-request [operation] [condition]:对所有到达此frontend的HTTP请求应用的策略,例如可以拒绝、要求认证、添加header、替换header、定义ACL等等。
- http-response [operation] [condition]:对所有从此frontend返回的HTTP响应应用的策略,大体同上
- log:同global域的log配置,仅应用于此frontend。如果要沿用global域的log配置,则此处配置为log global
- maxconn:同global域的maxconn,仅应用于此frontend
- mode:此frontend的工作模式,主要有http和tcp两种,对应L7和L4两种负载均衡模式
- option forwardfor:在请求中添加X-Forwarded-For Header,记录客户端ip
- option http-keep-alive:以KeepAlive模式提供服务
- option httpclose:与http-keep-alive对应,关闭KeepAlive模式,如果HAProxy主要提供的是接口类型的服务,可以考虑采用httpclose模式,以节省连接数资源。但如果这样做了,接口的调用端将不能使用HTTP连接池
- option httplog:开启httplog,HAProxy将会以类似Apache HTTP或Nginx的格式来记录请求日志
- option tcplog:开启tcplog,HAProxy将会在日志中记录数据包在传输层的更多属性
- stats uri [uri]:在此frontend上开启监控页面,通过[uri]访问
- stats refresh [time]:监控数据刷新周期
- stats auth [user]:[password]:监控页面的认证用户名密码
- timeout client [time]:指连接创建后,客户端持续不发送数据的超时时间
- timeout http-request [time]:指连接创建后,客户端没能发送完整HTTP请求的超时时间,主要用于防止DoS类攻击,即创建连接后,以非常缓慢的速度发送请求包,导致HAProxy连接被长时间占用
- use_backend [backend] if|unless [acl]:与ACL搭配使用,在满足/不满足ACL时转发至指定的backend
backend域的关键配置
- acl:同frontend域
- balance [algorithm]:在此backend下所有server间的负载均衡算法,常用的有roundrobin和source,完整的算法说明见官方文档configuration.html#4.2-balance
- cookie:在backend server间启用基于cookie的会话保持策略,最常用的是insert方式,如cookie HA_STICKY_ms1 insert indirect nocache,指HAProxy将在响应中插入名为HA_STICKY_ms1的cookie,其值为对应的server定义中指定的值,并根据请求中此cookie的值决定转发至哪个server。indirect代表如果请求中已经带有合法的HA_STICK_ms1 cookie,则HAProxy不会在响应中再次插入此cookie,nocache则代表禁止链路上的所有网关和缓存服务器缓存带有Set-Cookie头的响应。
- default-server:用于指定此backend下所有server的默认设置。具体见下面的server配置。
- disabled:禁用此backend
- http-request/http-response:同frontend域
- log:同frontend域
- mode:同frontend域
- option forwardfor:同frontend域
- option http-keep-alive:同frontend域
- option httpclose:同frontend域
- option httpchk [METHOD] [URL] [VERSION]:定义以http方式进行的健康检查策略。如option httpchk GET /healthCheck.html HTTP/1.1
- option httplog:同frontend域
- option tcplog:同frontend域
- server [name] [ip]:[port] [params]:定义backend中的一个后端server,[params]用于指定这个server的参数,常用的包括有:
check:指定此参数时,HAProxy将会对此server执行健康检查,检查方法在option httpchk中配置。同时还可以在check后指定inter, rise, fall三个参数,分别代表健康检查的周期、连续几次成功认为server UP,连续几次失败认为server DOWN,默认值是inter 2000ms rise 2 fall 3cookie [value]:用于配合基于cookie的会话保持,如cookie ms1.srv1代表交由此server处理的请求会在响应中写入值为ms1.srv1的cookie(具体的cookie名则在backend域中的cookie设置中指定)maxconn:指HAProxy最多同时向此server发起的连接数,当连接数到达maxconn后,向此server发起的新连接会进入等待队列。默认为0,即无限maxqueue:等待队列的长度,当队列已满后,后续请求将会发至此backend下的其他server,默认为0,即无限weight:server的权重,0-256,权重越大,分给这个server的请求就越多。weight为0的server将不会被分配任何新的连接。所有server默认weight为1
- timeout connect [time]:指HAProxy尝试与backend server创建连接的超时时间
- timeout check [time]:默认情况下,健康检查的连接+响应超时时间为server命令中指定的inter值,如果配置了timeout check,HAProxy会以inter作为健康检查请求的连接超时时间,并以timeout check的值作为健康检查请求的响应超时时间
- timeout server [time]:指backend server响应HAProxy请求的超时时间
default域
上文所属的frontend和backend域关键配置中,除acl、bind、http-request、http-response、use_backend外,其余的均可以配置在default域中。default域中配置了的项目,如果在frontend或backend域中没有配置,将会使用default域中的配置。
listen域
listen域是frontend域和backend域的组合,frontend域和backend域中所有的配置都可以配置在listen域下
定义ACL匹配规范
定义ACL匹配规范hdr([<name> [,<occ>]]):完全匹配字符串,header的指定信息hdr_beg([<name> [,<occ>]]):前缀匹配,header中指定匹配内容的beginhdr_end([<name> [,<occ>]]):后缀匹配,header中指定匹配内容endhdr_dom([<name> [,<occ>]]):域匹配,header中的domain namehdr_dir([<name> [,<occ>]]):路径匹配,header的uri路径hdr_len([<name> [,<occ>]]):长度匹配,header的长度匹配hdr_reg([<name> [,<occ>]]):正则表达式匹配,自定义表达式(regex)模糊匹配hdr_sub([<name> [,<occ>]]):子串匹配,header中的uri模糊匹配dst 目标IPdst_port 目标PORTsrc 源IPsrc_port 源PORT示例:hdr(<string>) 用于测试请求头部首部指定内容hdr_dom(host) 请求的host名称,如 www.magedu.comhdr_beg(host) 请求的host开头,如 www. img. video. download. ftp.hdr_end(host) 请求的host结尾,如 .com .net .cnpath_beg 请求的URL开头,如/static、/images、/img、/csspath_end 请求的URL中资源的结尾,如 .gif .png .css .js .jpg .jpeg有些功能是类似的,比如以下几个都是匹配用户请求报文中host的开头是不是www:acl short_form hdr_beg(host) www.acl alternate1 hdr_beg(host) -m beg www.acl alternate2 hdr_dom(host) -m beg www.acl alternate3 hdr(host) -m beg www.acl alternate3 hdr_end -m end .com
转载请标明出处【Centos7安装haproxy2.7实现4层与7层负载均衡】。
《www.micoder.cc》
虚拟化云计算,系统运维,安全技术服务.
| Tags: | [阅读全文...] |
最新评论