haproxy七层代理

1.haproxy是什么?为什么要用?

haproxy和LVS都是用于LB负载均衡,将特定的业务(web服务、网络流量)分担到一个或多个服务器上,从而解决单个服务器瓶颈问题,提高公司业务的并发处理能力,保证业务的高可用性,方便业务后期的水平动态发展。

haproxy和LVS的区别:

LVS是四层负载均衡,通过ip+port决定负载均衡到哪一台服务器。而haproxy是七层负载均衡,通过虚拟url或主机ip进行流量识别,根据应用层进行解析,然后决定是否需要负载均衡。也就是说LVS只识别到传输层的端口,也就是四层,而haproxy会到第七层应用层进行解析。

1.分层位置:四层负载均衡在传输层及以下,七层负载均衡在应用层及以下

2.性能:四层负载均衡架构无需解析报文消息内容,在网络吞吐量与处理能力上较高:七层可支持解析应用层报文消息内容,识别URL、Cookie、HTTP header等信息。

3.原理:四层负载均衡是基于ip+port;七层是基于虚拟的URL或主机IP等。

4.功能类比:四层负载均衡类似于路由器;七层类似于代理服务器。

5.安全性:四层负载均衡无法识别DDoS攻击;七层可防御SYN Cookie/Flood攻击
 

2.基础实验练习

2.1 haproxy基本部署负载均衡的实现

2.1.1 实验图

根据下图新建三台虚拟机进行配置,网络模式为NAT

配IP的时候注意自己NAT网络模式的网段

2.1.2 servera的配置

1.配置ip为172.25.254.10/24

2.配置nginx

        yum  install  nginx  -y

        echo   webserver1 - 172.25.254.10  >  /user/share/nginx/html/index.html

        systemctl  enable  --now  nginx

        systemctl  stop  firewalld

3.访问测试

        curl  172.25.254.10

2.1.3 serverb的配置

1.配置ip为172.25.254.20/24

2.配置nginx

        yum  install  nginx  -y

        echo   webserver2 - 172.25.254.20  >  /user/share/nginx/html/index.html

        systemctl  enable  --now  nginx

        systemctl  stop  firewalld

3.访问测试

        curl  172.25.254.20

2.1.4 haproxy服务器配置

1.安装haproxy

        dnf  install  haproxy  -y

2.修改/etc/haproxy/haproxy.cfg文件配置

vim /haproxy/haproxy.cfg

#
#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
 frontend webcluster
    bind *:80
    mode http
    use_backend webcluster-host
 
 backend webcluster-host
    balance roundrobin
    server web1 172.25.254.10:80
    server web2 172.25.254.20:80

或者写成

#
#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
listen webcluster
    bind *:80
    mode http
    balance roundrobin
    server web1 172.25.254.10:80
    server web2 172.25.254.20:80

上面两种写法都可以完成基本配置,ip是servsera和serverb的ip,ip前面的web1是服务名称,可以自定义

3.重启haproxy服务

修改完配置文件必须要重启服务

systemctl  restart  haproxy

4.去172.25.254.200的客户机做测试

首先172.25.254.200要能ping通haproxy服务器

访问会负载均衡交替访问10和20

如果将10的httpd服务停止,haproxy会自动只访问存活的20主机

测试:

[root@localhost ~]# curl 172.25.254.100
webserver1 - 172.25.254.10
[root@localhost ~]# curl 172.25.254.100
webserver2 - 172.25.254.20
[root@localhost ~]# curl 172.25.254.100
webserver1 - 172.25.254.10

去servera执行systemctl  stop  nginx

[root@localhost ~]# curl 172.25.254.100
webserver2 - 172.25.254.20
[root@localhost ~]# curl 172.25.254.100
webserver2 - 172.25.254.20
[root@localhost ~]# curl 172.25.254.100
webserver2 - 172.25.254.20

去servera执行systemctl  restart  nginx

[root@localhost ~]# curl 172.25.254.100
webserver1 - 172.25.254.10
[root@localhost ~]# curl 172.25.254.100
webserver2 - 172.25.254.20
[root@localhost ~]# curl 172.25.254.100
webserver1 - 172.25.254.10
[root@localhost ~]# curl 172.25.254.100
webserver2 - 172.25.254.20
[root@localhost ~]# 

后面我们的实验基本只需要修改haproxy的配置文件然后重启,haproxy重在理解,想认真学懂的朋友建议每天学一两章。

3.haproxy配置文件每部分介绍

3.1 global全局配置

3.1.1 在haproxy.cfg的位置

在/etc/haproxy/haproxy.cfg配置文件中,最上面的global是配置全局参数的位置

3.1.2 global 参数说明
global 配置参数说明
参数作用
chroot锁定运行目录
deamon以守护进程运行
user,group,uid,gid运行haproxy的用户身份
stats sockeet套接字文件
nbproc  N 设置进程数,默认1
nbthread 1(和nbproc互斥)指定每个进程的线程数,默认1,nbproc和nbthread只能设置一个
cpu-map n mn从1开始,m从0开始,将第n个进程绑定至m号CPU
maxconn  N每个进程的最大并发连接数
maxsslconn  N每个进程ssl最大连接数,用于haproxy配置了证书的场景
maxconnrate  N每个进程每秒创建的最大连接数量
spread-checks  N后端server状态check随机提前或延迟百分比时间,建议2-5(20%-50%之间),默认值0
pidfile指定pid文件路径
log  127.0.0.1  local2  info定义全局的syslog服务器;日志服务器需要开启UDP协议,最多可定义两个
3.1.3 global实验演示
3.1.3.1 设置多线程

1.添加以下内容

vim  /etc/haproxy/haproxy.cfg

保存退出重启服务

systemctl  restart  haproxy

查看线程

3.1.3.2 haproxy日志重定向

让haproxy日志生成到我们指定的目录文件

1.在haproxy配置文件中定义全局日志服务器local2,127.0.0.1是本地,配置文件默认有写

vim  /etc/haproxy/haproxy.cfg

 保存退出重启服务

systemctl   restart   haproxy

 2.修改日志配置文件,实现自定义日志

 这里可以先执行cat /var/log/haproxy.log,可以看到这个日志是不存在的,在配置完下面之后会有这个日志

上面global全局配置参数表格中最后一个有说,日志服务器需要开启UDP协议,所有进入配置文件需要写两个东西,1.打开UDP,2.日志重定向路径

vim /etc/rsyslog.conf 

重启日志服务

systemctl restart rsyslog.service

3.测试查看日志

去修改一下haproxy配置文件并保存重启服务,使日志产生

查看我们重定向的日志

3.2 proxies配置

3.2.1 proxies参数说明
参数作用
defaults默认配置项,针对以下的frontend、backend和listen生效,可以有0个或多个name
frontend前端servername,类似于Nginx的一个虚拟主机server和LVS服务集群
backend后端服务器组,等于nginx的upstream和LVS中的RS服务器
listen将frontend和backend合并在一起配置,使配置更简洁,生产常用

 注意:name只能数字、大小写字母、(_)下划线、(-)中划线、(.)点、(:)冒号

3.2.2 defaults
在haproxy.cfg的位置

 defaults参数说明
proxies-defaults
参数功能
option abortonclose当服务器负载很高时,自动结束掉当前队列处理比较久的连接,针对业务情况选择开启
option redispatch当server Id对应的服务器挂掉后,强制定向到其他健康服务器,重新派发
option http-keep-alive开启与客户端的会话保持
option forwardfor

透传客户端真实IP至后端web服务器(在apache配置文件中加入:

%{X-Forwarded-For}i 后在webserer中看日志即可看到地址透传信息)

mode http | tcp设置默认工作类型,使用TCP服务器性能更好,减少压力
timeout http-keepalive 120ssession 会话保持超时时间,此时间段内会转发到相同的后端服务器
timeout connect 120s客户端请求从haproxy到后端server最长连接等待时间(TCP连接之前),默认单位ms
timeout server 600s客户端请求从haproxy到后端服务端的请求处理超时时长(TCP连接之后),默认单位ms,如果超时,会出现502错误,此值建议设置较大些,访止502错误
timeout client 600s设置haproxy与客户端的最长非活动时间,默认单位ms,建议和 timeout server相同
timeout check 5s对后端服务器的默认检测超时时间
default-server inter 1000 weight 3指定后端服务器的默认设置

在配置文件中default是默认生效,当frontend、backend、listen中没有才生效default中的,学过C语言的可以理解为default是一个全局变量

3.2.3 frontend
在haproxy.cfg的位置

 frontend配置参数

bind:指定HAProxy的监听地址, 可以是IPV4或IPV6, 可以同时监听多个IP或端口, 可同时用千listen字段中
格式:

bind [<address>]:<port_range> [, ... ] [param*]
#注意:如果需要绑定在非本机的IP, 需要开启内核参数:net.ipv4.ip_nonlocal_bind=l
backlog <backlog>#针对所有server配置,当前端服务器的连接数达到上限后的后援队列长度, 注意:不支待backend

3.2.4 backend
在haproxy.cfg的位置

  • 定义一组后端服务器,backend服务器将被frontend进行调用。
  • 注意:back end的名称必须唯—并且必须在listen或frontend中事先定义才可以使用否则服务启动

mode http l tcp                #指定负载协议类型,和对应的frontend必须一致
option                             #配置选项
server                             #定义后端real server,必须指定IP和端口

注意:option后面加httpchk, smtpchk,mysql-check,pgsql-check, ssl-hello-chk方法, 可用于实现更
多应用层检测功能。

server配置参数说明

#针对一个server配置
check           #对指定real进行健康状态检查, 如果不加此设置, 默认不开启检查,只有check后面没有其它配置也可以启用检查功能
                    #默认对相应的后端服务器IP和端口,利用TCP连接进行周期性健康性检查,注意必须指定端口才能实现健康性检查
addr <IP>    #可指定的健康状态监测IP, 可以是专门的数据网段, 减少业务网络的流量
port <num> #指定的健康状态监测端口
inter <num> #健康状态检查间隔时间, 默认2000 ms
fall <num>   #后端服务器从线上转为线下的检查的连续失效次数, 默认为3
rise <num>  #后端服务器从下线恢复上线的检查的连续有效次数, 默认为2
weight <weight> #默认为1, 最大值为256, 0 (状态为蓝色)表示不参与负载均衡, 但仍接受持久连接
backup        #将后端服务器标记为备份状态,只在所有非备份主机down机时提供服务, 类似于Sorry Server
disabled      #将后端服务器标记为不可用状态, 即维护状态, 除了持久模式, 将不再接受连接,状态 为深黄色,优雅下线,不再接受新用户的请求
redirect prefix http://www.baidu.com/ #将请求临时(302) 重定向至其它URL, 只适用于http模式
redir http://www.baidu.com    #请求临时(302) 重定向至其它URL, 只适用于http模式
maxconn <maxconn>            #当前后端server的最大并发连接数

3.2.5 listen

使用listen替换frontend和backend的配置方式, 可以简化设置, 通常只用千TCP协议的应用

4.socat工具

对服务器动态权重和其它状态可以利用socat工具进行调整, Socat是Linux下的—个多功能的网络工具, 名字来由是Socket CAT, 相当于netCAT的增强版.Socat的主要特点就是在两个数据流之间建立双向通道, 且支持众多协议和链接方式。如IP、TCP 、UDP、1Pv6 、Socket文件等

范例:利用工具socat对服务器动态权重调整

查看帮助

haproxy~]#socat -h
haproxy~]#echo "help" I socat stdio /var/lib/haproxy/haproxy.sock
The following commands are valid at this level :
help : this message
prompt
quit
。。。省略。。。
toggle interactive mode with prompt
disconnect
enable server : enable a disabled server (use 'set server' instead) #启用服务器
set maxconn server : change a server's maxconn setting
set server : change a server's state, weight or address #设置服务器
get weight : report a server's current weight #查看权重
set weight : change a server's weight (deprecated) #设置权重
show startup-logs : report logs emitted during HAProxy startup
how peers [peers section]: dump some information about all the peers or this
peers section
set maxconn global : change the per-process maxconn setting
set rate-limit : change a rate limiting value
set severity-output [nonelnumberlstring] : set presence of severity level in
feedback information
set timeout : change a timeout setting
show env [var] : dump environment variables known to the process
show cli sockets : dump list of cli sockets
show cli level : display the level of the current CLI session
show fd [num] : dump list of file descriptors in use
。。。省略。。。

常用示例:

[root@haproxy~]#echo show info I socat stdio /var/lib/haproxy/haproxy.sock
[root@haproxy~]#echo show servers state I socat stdio
/var/lib/haproxy/haproxy.sock
[root@haproxy~]#echo "get weight webserver_80/webserverl" I socat stdio
/var/lib/haproxy/haproxy.sock
[root@haproxy~]#echo "set weight webserver_80/webserverl 1" I socat stdio
/var/lib/haproxy/haproxy.sock
[root@haproxy~]#echo "disable server webserver_80/webserverl " I socat stdio
/var/lib/haproxy/haproxy.sock
[root@haproxy~]#echo "enable server webserver_80/webserverl " I socat stdio
/var/lib/haproxy/haproxy.sock

针对多进程处理方法

如果开启多进程那么我们在对进程的sock文件进行操作时其对进程的操作时随机的
如果需要指定操作进程那么需要用多soct文件方式来完成

vim /etc/haproxy/haproxy.cfg

...上面内容省略...
stats socket /var/lib/haproxy/haproxy.sockl mode 600 level admin process 1
stats socket /var/lib/haproxy/haproxy.sock2 mode 600 level admin process 2
nbproc 2
cpu-map 1 0
cpu-map 2 1
...上面内容省略..

这样每个进程就会有单独的sock文件来进行单独管理

[root@haproxy~]#ll /var/lib/haproxy/
total 0
srw------- 1 root root O Jul 9 23:13 haproxy.sockl
srw------- 1 root root O Jul 9 23:13 haproxy.sock2
srwxr-xr-x 1 root root O Jul 8 16:02 stats

5.haproxy的算法(运维面试重点问)

HAProxy通过固定参数balance指明对后端服务器的调度算法
balance参数可以配置在listen或backend选项中。
HAProxy的调度算法分为静态和动态调度算法
有些算法可以根据参数在静态和动态算法中相互转换。

#静态
static-rr--------->tcp/http
first------------->tcp/http
#动态
roundrobin-------->tcp/http
leastconn--------->tcp/http
random------------>tcp/http
#以下静态和动态取决于hash_type是否consistent
source------------>tcp/http
Uri--------------->h ttp
url_param--------->http
hdr--------------->h ttp

5.1 静态算法

静态算法:按照事先定义好的规则轮询公平调度, 不关心后端服务器的当前负载、连接数和响应速度等, 且无法实时修改权重(只能为0和1,不支待其它值), 只能靠重启haproxy生效。

5.1.1 static-rr

static-rr是基于权重的轮询调度

  •  不支持运行时利用socat进行权重的动态调整(只支持0和1,不支持其它值)
  •  不支持端服务器漫启动
  •  其后端主机数量没有限制, 相当于LVS中的wrr

慢启动是指在服务器刚刚启动上不会把他所应该承担的访问压力全部给它, 而是先给一部分, 当没问题后在给一部分

示例:

haproxy~]#vim /etc/haproxy/haproxy.cfg
...上面内容省略...
listen webserver_80
bind 172.25.254.100:80
mode http
balance static-rr
server webserverl 172.25.254.10:80 weight 2 check inter 3s fall 3 rise 5
server webserver2 172.25.254.20:80 weight 1 check inter 3s fall 3 rise 5

...下面内容省略...

5.1.1 static-first
  •  根据服务器在列表中的位置, 自上而下进行调度
  •  其只会当第—台服务器的连接数达到上限,新请求才会分配给下一台服务
  •  其会忽略服务器的权重设置
  •  不支持用socat进行动态修改权重可以设置0和1,可以设置其它值但无效

示例:

haproxy~]#vim /etc/haproxy/haproxy.cfg
...上面内容省略...
listen webserver_80
bind 172.25.254.100:80
mode http
balance first
server webserverl 172.25.254.10:80 maxconn 3 check inter 3s fall 3 rise 5
server webserver2 172.25.254.20: 80 check inter 3s fall 3 rise 5
...上面内容省略...

测试效果:

#在两台主机上分别执行此循环, 可以观察是否20被调度到
while true;do curl 172.25.254.100 ; sleep O.l;done

5.2 动态算法

  •  基于后端服务器状态进行调度适当调整,
  •  新请求将优先调度至当前负载较低的服务器
  •  权重可以在haprox疫i行时动态调整无需重启
5.2.1 roundrobin

1.基于权重的轮询动态调度算法,
2.支持权重的运行时调整,不同于lvs中的rr轮训模式,
3.   HAProxy中的roundrobin支持慢启动(新加的服务器会逐渐增加转发数),
4.其每个后端backend中最多支持4095个real server,
5.支待对real server权重动态调整,
6.   roundrobin为默认调度算法此算法使用广泛

示例:

haproxy~]#vim /etc/haproxy/haproxy.cfg
...上面内容省略...
listen webserver_80
bind 172.25.254.100:80
mode http
balance roundrobin
server webserverl 172.25.254.10:80 weight 1 check inter 3s fall 3 rise 5
server webserver2 172.25.254.20:80 weight 1 check inter 3s fall 3 rise 5
...上面内容省略...

动态调整权重

[root@haproxy~]#echo "set weight webserver_80/webserverl 2" I socat stdio
/var/lib/haproxy/haproxy.sock

5.2.2 leastconn
  •  leastconn加权的最少连接的动态

  •  支持权重的运行时调整和慢启动, 即:根据当前连接最少的后端服务器而非权重进行优先调度(新客户端连接)

  • 比较适合长连接的场景使用,比如:MySQL等场景。

示例:

haproxy~]#vim /etc/haproxy/haproxy.cfg
...上面内容省略...
listen webserver_80
bind 172.25.254.100:80
mode http
balance leastconn
server webserverl 172.25.254.10:80 weight 1 check inter 3s fall 3 rise 5
server webserver2 172.25.254.20:80 weight 1 check inter 3s fall 3 rise 5
...上面内容省略...

5.3 其他算法

其它算法即可作为静态算法,又可以通过选项成为动态算法

5.3.1 source

源地址hash, 基于用户源地址hash并将请求转发到后端服务器,后续同—个源地址请求将被转发至同—个后端web服务器。此方式当后端服务器数据量发生变化时,会导致很多用户的请求转发至新的后端服务器,默认为静态方式,但是可以通过hash-type支持的选I觅巨改这个算法一般是在不插入Cookie的TCP模式下使用,也可给拒绝会话cookie的客户提供最好的会话粘性,适用于session会话保持但不支持cookie和缓存的场景源地址有两种转发客户端请求到后端服务器的服务器选取计算方式,分别是取模法和一致性hash

示例:

haproxy~]#vim /etc/haproxy/haproxy.cfg
...上面内容省略...
listen webserver_80
bind 172.25.254.100:80
mode http
balance source
server webserverl 172.25.254.10:80 weight 1 check inter 3s fall 3 rise 5
server webserver2 172.25.254.20:80 weight 1 check inter 3s fall 3 rise 5
...上面内容省略...

测试:

[root@nodelO~]#for N in {1..6}; do curl 172.25.254.100; done
RSl server - 172.25.254.10
RSl server - 172.25.254.10
RSl server - 172.25.254.10
RSl server - 172.25.254.10
RSl server - 172.25.254.10
RSl server - 172.25.254.10

如果访问客户端时—个家庭, 那么所有的家庭的访问流量都会被定向到一台服务器, 这时source算法的缺陷

5.3.2 map-base取模法
  • map-based:取模法, 对source地址进行hash计算, 再基于服务器总权重的取模, 最终结果决定将此请求转发至对应的后端服务器。
  • 此方法是静态的, 即不支持在线调整权重, 不支待慢启动, 可实现对后端服务器均衡调度
  • 缺点是当服务器的总权重发生变化时, 即有服务器上线或下线, 都会因总权重发生变化而导致调度结果整体改变, hash-type指定的默认值为此算法

所谓取模运算, 就是计算两个数相除之后的余数, 10%7=3, 7%4=3
map-based算法:基千权重取模, hash(source_ip)%所有后端服务器相加的总权重
比如当源hash值时1111, 1112, 1113 , 三台服务器a b c的权重均为1'
即abc的调度标签分别会被设定为0 1 2 (1111%3 =1, 1112%3 =2, 1113 %3 =0)
假设a为0, 1113 主机会被调度到a上,
如果a下线后, 权重数量发生变化
1111 %2=1, 1112%2=0, 1113%2=1
1112和1113被调度到的主机都发生变化, 这样会导致会话丢失

取模法配置示例:

haproxy~]#vim /etc/haproxy/haproxy.cfg
...上面内容省略...
listen webserver_80
bind 172.25.254.100:80
mode http
balance source
server webserverl 172.25.254.10:80 weight 1 check inter 3s fall 3 rise 5
server webserver2 172.25.254.20:80 weight 1 check inter 3s fall 3 rise 5
...上面内容省略...

#不支持动态调整权重值


[root@haproxy~]#echo "set weight webserver_80/webserverl 2" I socat stdio
/var/lib/haproxy/haproxy.sock
Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.

#只能动态上线和下线

[root@haproxy~]#echo "set weight webserver_80/webserverl O" I socat stdio
/var/lib/haproxy/haproxy.sock
[root@haproxy~]#echo "get weight webserver_80/webserverl" I socat stdio
/var/lib/haproxy/haproxy.sock
0 (initial 1)

5.3.3 一致性hash

会话持久化

  • Source IP: 当客户端连接到后端服务器时,HAProxy 可以通过客户端的 IP 地址进行哈希,并始终将该 IP 的请求转发到同一个后端服务器,从而实现会话的一致性。

  • Cookie: 使用插入或被动模式的 Cookie 进行会话跟踪,确保来自同一用户的请求总是被定向到相同的后端服务器。

一致性哈希, 当服务器的总权重发生变化时, 对调度结果影响是局部的, 不会引起大的变动hash (o)mod n
该hash算法是动态的, 支持使用socat等工具进行在线权重调整, 支待慢启动

算法:

1、后端服务器哈希环点keyA=hash (后端服务器虚拟ip)%(2A32)
2、客户机哈希环点keyl=hash(cl ient_ip)%(2A32) 得到的值在[ 0---4294967295] 之间,
3 、将keyA和keyl都放在hash环上, 将用户请求调度到离keyl最近的keyA对应的后端服务器

hash环偏斜问题

增加虚拟服务器IP数量, 比如: 一个后端服务器根据权重为1生成1000个虚拟IP, 再hash。而后端服务器权重为2则生成2000的虚拟IP, 再bash ,最终在hash环上生成3000个节点, 从而解决hash环偏斜问题

 一致性hash配置示例

haproxy~]#vim /etc/haproxy/haproxy.cfg
...上面内容省略...
listen webserver_80
bind 172.25.254.100:80
mode http
balance source
hash-type consistent
server webserverl 172.25.254.10:80 weight 1 check inter 3s fall 3 rise 5
server webserver2 172.25.254.20:80 weight 1 check inter 3s fall 3 rise 5
...上面内容省略...

5.3.4 uri

基于对用户请求的URI的左半部分或整个uri做hash, 再将hash结果对总权重进行取模后
根据最终结果将请求转发到后端指定服务器
适用千后端是缓存服务器场景
默认是静态算法, 也可以通过hash-type指定map-based和consistent, 来定义使用取模法还是一致性hash

注意:此算法基千应用层,所以只支持mode http , 不支持mode tcp

<scheme>://<user>:<password>@<host>:<port>/<path>;<params>?<query>#<frag>
左半部分: /<path>;<params>
整个uri, /<path>;<params>?<query>#<frag>

uri取模法配置示例

haproxy~]# vim /etc/haproxy/haproxy.cfg
...上面内容省略...
listen webserver_80
bind 172.25.254.100:80
mode http
balance uri
server webserverl 172.25.254.10:80 weight 1 check inter 3s fall 3 rise 5
server webserver2 172.25.254.20:80 weight 1 check inter 3s fall 3 rise 5
...上面内容省略...

uri 一致性hash配置示例

haproxy~]#vim /etc/haproxy/haproxy.cfg
...上面内容省略...
listen webserver_80
bind 172.25.254.100:80
mode http
balance uri
hash-type consistent
server webserverl 172.25.254.10:80 weight 1 check inter 3s fall 3 rise 5
server webserver2 172.25.254.20:80 weight 1 check inter 3s fall 3 rise 5
...上面内容省略...

访问测试

访间不同的uri, 确认可以将用户同样的请求转发至相同的服务器

root@rsl~]#echo RSl 172.25.254.10 i ndexl > /var /www/html /i ndexl. html
[root@rsl~]#echo RSl 172.25.254.10 index2 > /var/www/html/index2.html
[root@rsl~]#echo RSl 172.25.254.10index3 > /var/www/html/index3.html
[root@rs2~]#echo RSl 172.25.254.20 i ndexl > /var /www/html /i ndexl. html
[root@rs2~]#echo RSl 172.25.254.20 index2 > /var/www/html/index2.html
[root@rs2~]#echo RSl 172.25.254.20 index3 > /var/www/html/index3.html
[root@nodelO~]#curl 172.25.254.100/index.html
RS2 server - 172.25.254.20
[root@nodelO~]#curl 172. 25. 254.100/i ndexl. html
RSl 172.25.254.10 indexl
[root@nodelO~]#curl 172.25.254.100/index2.html
RSl 172.25.254.20 index2
[root@nodelO~]#curl 172.25.254.100/index3.html
RSl 172.25.254.10 index3

5.3.5 url_param

url_param对用户请求的url中的params部分中的—个参数key对应的value值作hash计算,并由服务器总权重相除以后派发至某挑出的服务器后端搜索同—个数据会被调度到同—个服务器, 多用与电商

通常用于追踪用户, 以确保来自同—个用户的请求始终发往同—个real server如果无没key, 将按roundrobin算法

#假设:
url = http://www.timinglee.com/foo/bar/index.php?key=value
#则:
host = "www.timinglee.com"
url_param = "key=value"

url_param取模法配置示例

haproxy~]#vim /etc/haproxy/haproxy.cfg
...上面内容省略...
listen webserver_80
bind 172.25.254.100:80
mode http
balance url_param name,userid #支持对多个url_param hash
server webserverl 172.25.254.10:80 weight 1 check inter 3s fall 3 rise 5
server webserver2 172.25.254.20:80 weight 1 check inter 3s fall 3 rise 5
...上面内容省略...

url_param一致性hash配置示例

haproxy~]#vim /etc/haproxy/haproxy.cfg
...上面内容省略...
listen webserver_80
bind 172.25.254.100:80
mode http
balance url_param name,userid
hash-type consistent
#支持对多个url_param hash
server webserverl 172.25.254.10:80 weight 1 check inter 3s fall 3 rise 5
server webserver2 172.25.254.20:80 weight 1 check inter 3s fall 3 rise 5
...上面内容省略...

测试访问

[root@nodelO~]#curl 172.25.254.100/index3.html?userid=lll
RSl 172.25.254.10 index3
[root@nodelO~]#curl 172.25.254.100/index3.html?userid=222
RSl 172.25.254.10 index3
[root@nodelO~]#curl 172.25.254.100/index3.html?userid=333
RSl 172.25.254.20 index3
[root@nodelO~]#curl 172.25.254.100/index3.html?userid=444
RSl 172.25.254.10 index3

5.3.6 hdr

针对用户每个http头部(header)请求中的指定信息做hash,
此处由name指定的http首部将会被取出并做hash计算,
然后由服务器总权重取模以后派发至某挑出的服务器, 如果无有效值, 则会使用默认的轮询调度。

hdr取模法配置示例

haproxy~]#vim /etc/haproxy/haproxy.cfg
...上面内容省略...
listen webserver_80
bind 172.25.254.100:80
mode http
balance hdr(user-Agent)
server webserverl 172.25.254.10:80 weight 1 check inter 3s fall 3 rise 5
server webserver2 172.25.254.20:80 weight 1 check inter 3s fall 3 rise 5
...上面内容省略...

一致性hash配置示例

haproxy~]#vim /etc/haproxy/haproxy.cfg
...上面内容省略...
listen webserver_80
bind 172.25.254.100:80
mode http
balance hdr(User-Agent)
hash-type consistent
server webserverl 172.25.254.10:80 weight 1 check inter 3s fall 3 rise 5
server webserver2 172.25.254.20:80 weight 1 check inter 3s fall 3 rise 5
...上面内容省略...

测试访问

[root@nodelO~]#curl -v 172.25.254.100
[root@nodelO~]#curl -vA "fi refox" 172. 25. 254.100
[root@nodelO~]#curl -vA "sougou" 172.25.254.100

5.4 各个算法使用场景

first #使用较少
static-rr
roundrobin
random
#做了session共享的web集群
leastconn
source
#数据库
#基千客户端公网IP的会话保持
uri--------------->http
url_param--------->http
hdr

6.高级功能及配置

6.1 状态页

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 } #启用stats page中的管理功能

6.2 基于cookie的会话保持

在一个浏览器访问后,会记住选择,之后刷新一直是该后端主机,另一个浏览器访问则是另一个后端主机

[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg 
    balance roundrobin
    cookie WEBCOOKIE insert nocache indirect
    server web1 172.25.250.10:80 cookie aaa1 check inter 2 fall 3 rise 5 weight 2
    server web2 172.25.250.20:80 cookie aaa2 check inter 2 fall 3 rise 5 weight 1
[root@haproxy ~]# systemctl restart haproxy.service 
[root@haproxy ~]# curl -b WEBCOOKIE=aaa1 172.25.250.100
webserver1 - 172.25.250.10
[root@haproxy ~]# curl -b WEBCOOKIE=aaa2 172.25.250.100
webserver2 - 172.25.250.20

6.3 backend server信息

session rate(每秒的连接会话信息): Errors(错误统计信息):

cur:每秒的当前会话数量 : Req:错误请求量

max:每秒新的最大会话数量 conn:错误链接量

limit:每秒新的会话限制量 Resp:错误响应量

sessions(会话信息): Warnings(警告统计信息):

cur:当前会话量 Retr:重新尝试次数

max:最大会话量 Redis:再次发送次数

limit: 限制会话量

Total:总共会话量 Server(real server信息):

LBTot:选中一台服务器所用的总时间 Status:后端机的状态,包括UP和DOWN Last:和服务器的持续连接时间 LastChk:持续检查后端服务器的时间

Wght:权重

Bytes(流量统计): Act:活动链接数量

In:网络的字节输入总量 Bck:备份的服务器数量

Out:网络的字节输出总量 Chk:心跳检测时间

Dwn:后端服务器连接后都是DOWN的数量

Denied(拒绝统计信息): Dwntme:总的downtime时间

Req:拒绝请求量 Thrtle:server 状态

Resp:拒绝回复量

6.4 ip透传

七层代理

七层代理 mode--->http
#webserver1 和webserver2
[root@webserver1 ~]# systemctl disable nginx
[root@webserver1 ~]# systemctl stop nginx
[root@webserver1 ~]# dnf install httpd -y
[root@webserver1 ~]# echo webserver1 - 172.25.250.10 > /var/www/html/index.html
[root@webserver1 ~]# vim /etc/httpd/conf/httpd.conf 

[root@webserver1 ~]# vim /etc/httpd/conf/httpd.conf 
LogFormat "%{X-Forwarded-For}i %h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
 
[root@webserver2 ~]# vim /etc/nginx/nginx.conf
19                       '"$proxy_protocol_addr"'
​#测试
[root@webserver1 ~]# tail -n 3 /etc/httpd/logs/access_log
[root@webserver2 ~]# tail -3 /var/log/nginx/access.log

四层代理

四层代理mode--->tcp
看不到IP地址
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg 
    mode tcp
    server web1 172.25.250.10:80  check inter 2 fall 3 rise 5 weight 2
    server web2 172.25.250.20:80 send-proxy check inter 2 fall 3 rise 5 weight 1
[root@haproxy ~]# systemctl restart haproxy.service

#webserver2
[root@webserver2 ~]# 
 vim /etc/nginx/nginx.conf
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request"'
    ' "$proxy_protocol_addr"'
    
server {
        listen       80 proxy_protocol;

[root@webserver2 ~]# systemctl restart nginx
 
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
 server web1 172.25.250.10:80 check inter 2 fall 3 rise 5 weight 2
    server web2 172.25.250.20:80 send-proxy check inter 2 fall 3 rise 5 weight 1
[root@haproxy ~]# systemctl restart haproxy.service

 

测试 

 

6.5 基于文件后缀名实现动静分离

[root@webserver1 ~]# dnf install php -y
[root@webserver1 ~]# systemctl restart httpd
[root@webserver1 ~]# vim /var/www/html/index.php
[root@webserver1 ~]# cat /var/www/html/index.php 
<?php
    phpinfo();
?>

#haproxy
frontend webcluster
    bind *:80
    mode http
    acl static path_end -i .html .jpg .png .css .js
    acl php    path_end -i .php
    use_backend webcluster-host if php
    default_backend default-host

6.6 基于访问路径实现动静分离

[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
 frontend test_acl
      bind *:80
      mode http
      acl test  path_sub -m sub test
      acl php    path_sub -m sub php
      use_backend  webcluster-host if php
      default_backend default_host
  
  backend webcluster-host
       mode http
       server web1 172.25.254.10:80 check inter 3 fall 3 rise 5
 
   backend default_host
       mode http
       server web2 172.25.254.20:80 check inter 3 fall 3 rise 5

  配置server端

 [root@webserver1 ~]# mkdir -p /var/www/html/php
[root@webserver1 ~]# cp /var/www/html/index.php /var/www/html/php/
 
 
[root@webserver2 ~]# mkdir /usr/share/nginx/html/test -p
[root@webserver2 ~]# echo webserver2-20 > /usr/share/nginx/html/test/index.html

7.自定义HAProxy错误界面

基于自定义的错误页面文件


#webserver1\2主机上
system stop httpd

#haproxy主机上
[root@haproxy ~]# mkdir /etc/haproxy/errorpage -p
[root@haproxy ~]# vim /etc/haproxy/errorpage/503.http
HTTP/1.0 503 Service Unavailable
Cache-Control: no-cache
Connection: close
Content-Type: text/html;charset=UTF-8

<html><body><h1>什么动物生气最安静</h1>
大猩猩!!
</body></html>

[root@haproxy ~]# vim /etc/haproxy/haproxy.conf
defaults
    errorfile 503   /etc/haproxy/errorpage/503.http
[root@haproxy ~]# systemctl restart haproxy.service 

​然后用浏览器去访问

172.25.250.130

8.haproxy 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 356 -out /etc/haproxy/certs/timinglee.org.crt
CD shannxi XIan timinglee webserver www.timinglee.org admin@timinglee.org

[root@haproxy ~]# ls /etc/haproxy/certs/
[root@haproxy ~]# cat /etc/haproxy/certs/timinglee.org.key /etc/haproxy/certs/timinglee.org.crt > /etc/haproxy/certs/timinglee.pem

[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
frontend webcluster
    bind *:80
    mode http
    redirect scheme https if !{ ssl_fc }  #全网站加密

backend webcluster-host
    mode http
    server web1 172.25.250.10:80 check inter 2 fall 2 rise 5

backend default-host
    mode http
    server web2 172.25.250.20:80 check inter 2 fall 2 rise 5

listen web-https
    bind *:443 ssl crt /etc/haproxy/certs/timinglee.pem
    mode http
    balance roundrobin
    server web1 172.25.250.10:3306 check inter 2 fall 2 rise 5
    server web2 172.25.250.20:3306 check inter 2 fall 2 rise 5

[root@haproxy ~]# systemctl restart haproxy
[root@haproxy ~]# netsata -antup | grep 443

开启web1,web2服务
访问https://172.25.250.130

  • 20
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值