康乐_SH

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

1、简述keepalived工作原理

简述keepalived工作原理
1.VRRP
VRRP(Virtual Router Redundancy Protocol):虚拟路由冗余协议,解决静态网关单点故障风险,它能够保证当个别节点宕机时,整个网络可以不间断地运行。
物理层:路由器、三层交换机
软件层:keepalived
1.1VRRP相关术语
虚拟路由器:Virtual Router
虚拟路由器标识:VRID(0-255),唯一标识虚拟路由器
VIP:Virtual IP,虚拟路由器的IP地址
VMAC:Virutal MAC(00-00-5e-00-01-VRID)
物理路由器:
master:主设备,虚拟路由器中承担报文转发任务的路由器
backup:备用设备,master路由器出现故障时,能够代替master路由器工作的路由器
priority:优先级,VRRP根据优先级来确定虚拟路由器中每台路由器的地位
1.2VRRP相关技术
通告:心跳,优先级等;周期性
工作方式:抢占式,非抢占式
抢占模式:在抢占模式下,如果Backup设备的优先级比当前Master设备的优先级高,则主动将自己切换成Master。
非抢占模式:在非抢占模式下,只要Master设备没有出现故障,Backup设备即使随后被配置了更高的优先级也不会成为Master设备。

安全认证:

无认证:不进行任何VRRP报文的合法性认证,不提供安全性保障

简单字符认证:预共享密钥,在一个有可能受到安全威胁的网络中,可以将认证方式设置为简单字符认证

MD5认证:在一个非常不安全的网络中,可以将认证方式设置为MD5认证

工作模式:
主/备:单虚拟路由器
主/主:主/备(虚拟路由器1),备/主(虚拟路由器2)

1.3VRRP工作过程
(1)VRRP备份租中的设备根据优先级选举出Master。Master设备通过发送免费ARP报文,将虚拟MAC地址通知给与它连接的设备或者主机,从而承担报文转发任务。
(2)Master设备周期性向备份组内所有Backup设备发送VRRP通告报文,以公布其配置信息(优先级等)和工作状况。
(3)如果Master设备出现故障,VRRP备份组中的Backup设备将根据优先级重新选举新的Master。
(4)VRRP备份组状态切换时,Master设备由一台设备切换为另外一台设备,新的Master设备会立即发送携带虚拟路由器的虚拟MAC地址和虚拟IP地址信息的免费ARP报文,刷新与它连接的主机或设备中的MAC表项,从而把用户流量引到新的Master设备上来,整个过程对用户完全透明。
(5)原Master设备故障恢复时,若该设备为IP地址拥有者(优先级为255),将直接切换至Master状态。若该设备优先级小于255,将首先切换至Backup状态,且其优先级恢复为故障前配置的优先级。
(6)Backup设备的优先级高于Master设备时,由Backup设备的工作方式(抢占方式和非抢占方式)决定是否重新选举Master。

2.keepalived介绍
keepalived基于VRRP协议的软件实现,原生设计目的为了高可用ipvs服务
ipvs,IP虚拟服务器(IP Virtual Server,简写为IPVS)。是运行在LVS下的提供负载均衡功能的一种技术。
keepalived的功能:
基于vrrp协议完成地址流动
为vip地址所在的节点生成ipvs规则(在配置文件中预先定义)
为ipvs集群的各RS(后端的Real Server)做健康状态检测
基于脚本调佣接口完成脚本中定义的功能,进而影响集群事务,以此支持nginx、haproxy等服务
2.1Keepalived架构
用户空间核心组件:
vrrp stack:VIP消息通知
checkers:监测real server
system call:实现vrrp协议状态转换时调用脚本的功能
SMTP:邮件组件
IPVS wrapper:生成IPVS规则
WatchDog:监控进程
控制组件:提供keepalived。conf的解析器,完成Keepalived配置
IO复用器:针对网络目的而优化的自己的线程抽象
内存管理组件:为某些通用的内存管理功能(例如分配,重新分配,发布等)提供访问权限
3.keepalived的工作原理
Keepalived高可用对之间是通过VRRP进行通信的,VRRP是通过竞选机制来确定主备节点的,主节点的优先级高于备节点,因此,工作时主节点会优先获得所有的资源,备节点处于等待状态,当主节点出现故障时,备节点就会接管主节点的资源,然后顶替主节点对外提供服务。

在Keepalived服务对之间,只有作为主节点的服务器会一直发送 VRRP广播包,告诉备节点它还活着,此时备节点不会抢占主节点资源,当主节点不可用时,即备节点监听不到主节点发送的广播包时,备节点就会启动相关服务接管主节点的资源,保证业务的连续性。接管速度最快可以小于1秒。
3.1Keepalived高可用故障切换转移原理
Keepalived高可用服务之间的故障切换转移,还是通过VRRP(Vritual Route Redundancy Protocol,虚拟路由冗余协议)来实现的 。

在Keepalived服务正常工作时,Master主节点会不断向Backup备节点发送心跳消息(多播的方式),用来告诉备用节点自己还活着,当Master主节点出现故障时,就无法发送心跳消息,备节点也就因此无法继续检测到来自Master主节点的心跳消息了,于是调用自身的接管程序,接管Master主节点的IP资源及服务。而当Master主节点恢复时,Backup备节点默认又会主动释放主节点故障时自身接管的IP资源及服务,恢复到原来的备用角色。
4.脑裂
4.1什么是脑裂?
在高可用(HA)系统中,当联系2个节点的“心跳线”断开时,本来为一整体、动作协调的HA系统,就分裂成为2个独立的个体。
由于相互失去了联系,都以为是对方出了故障。两个节点上的HA软件像“裂脑人”一样,争抢“共享资源”、争起“应用服务”,就会发生严重后果。共享资源被瓜分、两边“服务”都起不来了;或者两边“服务”都起来了,但同时读写“共享存储”,导致数据损坏
4.2造成脑裂的原因有哪些?
高可用服务器对之间心跳线链路发生故障,导致无法正常通信。
因心跳线坏了(包括断了,老化)。
因网卡及相关驱动坏了,ip配置及冲突问题(网卡直连)
因心跳线间连接的设备故障(网卡及交换机)
因仲裁的机器出问题(采用仲裁的方案)
高可用服务器上开启了 iptables防火墙阻挡了心跳消息传输。
高可用服务器上心跳网卡地址等信息配置不正确,导致发送心跳失败
其他服务配置不当等原因,如心跳方式不同,心跳广插冲突、软件Bug等。
4.3如何解决keepalived脑裂问题?
同时使用串行电缆和以太网电缆连接、同时使用两条心跳线路,这样一条线路断了,另外一条还是好的,依然能传送心跳消息
当检查脑裂时强行关闭一个心跳节点(这个功能需要特殊设备支持,如stonith、fence)相当于备节点接收不到心跳消息,通过单独的线路发送关机命令关闭主节点的电源
做好对脑裂的监控报警
解决常见方案:

如果开启防火墙,一定要让心跳消息通过,一般通过允许IP段的形式解决
可以拉一条以太网网线或者串口线作为主被节点心跳线路的冗余
开发检测程序通过监控软件检测脑裂


2、编译安装haproxy

Centos7编译安装haproxy2.4.16
环境准备
由于CentOS7之前版本自带的lua版本比较低并不符合HAProxy要求的lua最低版本(5.3)的要求,因此需要编译安装较新版本的lua环境,然后才能编译安装HAProxy,过程如下:
#当前系统版本
[root@centos7 ~]#lua -v
Lua 5.1.4 Copyright (C) 1994-2008 Lua.org, PUC-Rio

#安装基础命令及编译依赖环境
[root@centos7 ~]#yum install gcc readline-devel -y
[root@centos7 ~]#tar xf lua-5.4.4.tar.gz -C /usr/local/src/
[root@centos7 ~]#cd /usr/local/src/lua-5.4.4/
[root@centos7 lua-5.4.4]#make linux test

#查看编译安装的版本
[root@centos7 lua-5.4.4]#src/lua -v
Lua 5.4.4 Copyright (C) 1994-2022 Lua.org, PUC-Rio

1编译安装HAProxy
#HAProxy 2.0以上版本编译参数:
[root@centos7 ~]#yum -y install gcc openssl-devel pcre-devel systemd-devel
[root@centos7 lua-5.4.4]# cd ~
[root@centos7 ~]#tar xf haproxy-2.4.16.tar.gz -C /usr/local/src、
[root@centos7 ~]#cd /usr/local/src/haproxy-2.4.16/

#查看安装方法
[root@centos7 haproxy-2.4.16]#less INSTALL
[root@centos7 haproxy-2.4.16]#less Makefile

#参考INSTALL文件进行编译安装
[root@centos7 haproxy-2.4.16]#make ARCH=x86_64 TARGET=linux-glibc USE_PCRE=1 USE_OPENSSL=1 USE_ZLIB=1 USE_SYSTEMD=1 USE_LUA=1 LUA_INC=/usr/local/src/lua-5.4.4/src/ LUA_LIB=/usr/local/src/lua-5.4.4/src/
[root@centos7 haproxy-2.4.16]#make install PREFIX=/apps/haproxy
[root@centos7 haproxy-2.4.16]#ln -s /apps/haproxy/sbin/haproxy /usr/sbin/

#查看生成的文件
[root@centos7 haproxy-2.4.16]#tree /apps/haproxy/
/apps/haproxy/
├── doc
│ └── haproxy
│ ├── 51Degrees-device-detection.txt
│ ├── architecture.txt
│ ├── close-options.txt
│ ├── configuration.txt
│ ├── cookie-options.txt
│ ├── DeviceAtlas-device-detection.txt
│ ├── intro.txt
│ ├── linux-syn-cookies.txt
│ ├── lua.txt
│ ├── management.txt
│ ├── netscaler-client-ip-insertion-protocol.txt
│ ├── network-namespaces.txt
│ ├── peers.txt
│ ├── peers-v2.0.txt
│ ├── proxy-protocol.txt
│ ├── regression-testing.txt
│ ├── seamless_reload.txt
│ ├── SOCKS4.protocol.txt
│ ├── SPOE.txt
│ └── WURFL-device-detection.txt
├── sbin
│ └── haproxy
└── share
└── man
└── man1
└── haproxy.1

6 directories, 22 files

1.1验证HAProxy版本
#验证HAProxy版本:
[root@centos7 haproxy-2.4.16]#which haproxy
/usr/sbin/haproxy
[root@centos7 haproxy-2.4.16]#haproxy -v
HAProxy version 2.4.16-9d532c4 2022/04/29 - https://haproxy.org/
Status: long-term supported branch - will stop receiving fixes around Q2 2026.
Known bugs: http://www.haproxy.org/bugs/bugs-2.4.16.html
Running on: Linux 3.10.0-1062.el7.x86_64 #1 SMP Wed Aug 7 18:08:02 UTC 2019 x86_64

#大写-V选项显示版本和帮助用法
[root@centos7 haproxy-2.4.16]# haproxy -v
HAProxy version 2.4.16-9d532c4 2022/04/29 - https://haproxy.org/
Status: long-term supported branch - will stop receiving fixes around Q2 2026.
Known bugs: http://www.haproxy.org/bugs/bugs-2.4.16.html
Running on: Linux 3.10.0-1062.el7.x86_64 #1 SMP Wed Aug 7 18:08:02 UTC 2019 x86_64
[root@centos7 haproxy-2.4.16]# haproxy -V
HAProxy version 2.4.16-9d532c4 2022/04/29 - https://haproxy.org/
Status: long-term supported branch - will stop receiving fixes around Q2 2026.
Known bugs: http://www.haproxy.org/bugs/bugs-2.4.16.html
Running on: Linux 3.10.0-1062.el7.x86_64 #1 SMP Wed Aug 7 18:08:02 UTC 2019 x86_64
Usage : haproxy [-f <cfgfile|cfgdir>]* [ -vdVD ] [ -n <maxconn> ] [ -N <maxpconn> ]
[ -p <pidfile> ] [ -m <max megs> ] [ -C <dir> ] [-- <cfgfile>*]
-v displays version ; -vv shows known build options.
-d enters debug mode ; -db only disables background mode.
-dM[<byte>] poisons memory with <byte> (defaults to 0x50)
-V enters verbose mode (disables quiet mode)
-D goes daemon ; -C changes to <dir> before loading files.
-W master-worker mode.
-Ws master-worker mode with systemd notify support.
-q quiet mode : don't display messages
-c check mode : only check config files and exit
-n sets the maximum total # of connections (uses ulimit -n)
-m limits the usable amount of memory (in MB)
-N sets the default, per-proxy maximum # of connections (0)
-L set local peer name (default to hostname)
-p writes pids of all children to this file
-de disables epoll() usage even when available
-dp disables poll() usage even when available
-dS disables splice usage (broken on old kernels)
-dG disables getaddrinfo() usage
-dR disables SO_REUSEPORT usage
-dL dumps loaded object files after config checks
-dr ignores server address resolution failures
-dV disables SSL verify on servers side
-dW fails if any warning is emitted
-dD diagnostic mode : warn about suspicious configuration statements
-sf/-st [pid ]* finishes/terminates old pids.
-x <unix_socket> get listening sockets from a unix socket
-S <bind>[,<bind options>...] new master CLI

1.2准备HAProxy启动文件
#创建service文件
[root@centos7 haproxy-2.4.15]#vim /usr/lib/systemd/system/haproxy.service
[Unit]
Description=HAProxy Load Balancer
After=syslog.target network.target

[Service]
ExecStartPre=/usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -c -q
ExecStart=/usr/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /var/lib/haproxy/haproxy.pid
ExecReload=/bin/kill -USR2 $MAINPID
LimitNOFILE=100000

[Install]
WantedBy=multi-user.target

[root@centos7 haproxy-2.4.16]#systemctl daemon-reload

1.3配置文件
#创建自定义的配置文件
[root@centos7 haproxy-2.4.16]#mkdir /etc/haproxy
[root@centos7 haproxy-2.4.16]#vim /etc/haproxy/haproxy.cfg
global
maxconn 100000
chroot /apps/haproxy
stats socket /var/lib/haproxy/haproxy.sock mode 600 level admin
#uid 99
#gid 99
user haproxy
group haproxy
daemon
#nbproc 4
#cpu-map 1 0
#cpu-map 2 1
#cpu-map 3 2
#cpu-map 4 3
pidfile /var/lib/haproxy/haproxy.pid
log 127.0.0.1 local2 info

defaults
option http-keep-alive
option forwardfor
maxconn 100000
mode http
timeout connect 300000ms
timeout client 300000ms
timeout server 300000ms

listen stats
mode http
bind 0.0.0.0:9999
stats enable
log global
stats uri /haproxy-status
stats auth haadmin:123456

listen web_port
bind 10.0.0.7:80
mode http
log global
server web1 127.0.0.1:8080 check inter 3000 fall 2 rise 5

1.4启动 haproxy
#准备socket文件目录
[root@centos7 haproxy-2.4.16]#mkdir /var/lib/haproxy

#设置用户和目录权限
[root@centos7 haproxy-2.4.16]#useradd -r -s /sbin/nologin -d /var/lib/haproxy haproxy
[root@centos7 haproxy-2.4.16]#systemctl enable --now haproxy

1.5验证 haproxy 状态
[root@centos7 haproxy-2.4.16]#systemctl status haproxy
● haproxy.service - HAProxy Load Balancer
Loaded: loaded (/usr/lib/systemd/system/haproxy.service; enabled; vendor preset: disabled)
Active: active (running) since 六 2022-04-30 19:07:40 CST; 9s ago
Process: 2481 ExecStartPre=/usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -c -q (code=exited, status=0/SUCCESS)
Main PID: 2484 (haproxy)
CGroup: /system.slice/haproxy.service
├─2484 /usr/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /var/lib/haproxy/haproxy...
└─2488 /usr/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /var/lib/haproxy/haproxy...

4月 30 19:07:40 centos7.magedu.com systemd[1]: Starting HAProxy Load Balancer...
4月 30 19:07:40 centos7.magedu.com systemd[1]: Started HAProxy Load Balancer.
4月 30 19:07:40 centos7.magedu.com haproxy[2484]: [NOTICE] (2484) : New worker #1 (2488) forked
4月 30 19:07:40 centos7.magedu.com haproxy[2484]: [WARNING] (2488) : Server web_port/web1 i...e.
4月 30 19:07:40 centos7.magedu.com haproxy[2484]: [NOTICE] (2488) : haproxy version is 2.4...c4
4月 30 19:07:40 centos7.magedu.com haproxy[2484]: [NOTICE] (2488) : path to executable is ...xy
4月 30 19:07:40 centos7.magedu.com haproxy[2484]: [ALERT] (2488) : proxy 'web_port' has n...e!
Hint: Some lines were ellipsized, use -l to show in full.

[root@centos7 haproxy-2.4.16]#pstree -p |grep haproxy
|-haproxy(2484)---haproxy(2488)-+-{haproxy}(2489)
| |-{haproxy}(2490)
| `-{haproxy}(2491)

 

3、总结haproxy各调度算法的实现方式及其应用场景

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

1.静态算法

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

1.1static-rr

static-rr:基于权重的轮询调度,该算法与轮询算法相似,只是它是静态的,这意味着动态更改服务器的权重不会产生任何影响,不支持运行时利用socat进行权重的动态调整(只支持0和1,不支持其它值)及后端服务器慢启动,其后端主机数量没有限制,相当于LVS中的wrr。

#static-rr轮询算法实现
[root@centos7 ~]# vim /etc/haproxy/conf.d/test.cfg
frontend WEB_PORT_80
    bind 10.0.0.7:80
    balance static-rr
    mode http
    use_backend web_port_http_nodes

backend web_port_http_nodes
    mode http
    option forwardfor
    server 10.0.0.18 10.0.0.18:80 check inter 3000 fall 3 rise 5
    server 10.0.0.28 10.0.0.28:80 check inter 3000 fall 3 rise 5

[root@centos7 ~]# systemctl reload haproxy

[root@openeuler22 ~]# curl 10.0.07
10.0.0.18
[root@openeuler22 ~]# curl 10.0.07
10.0.0.28
[root@openeuler22 ~]# curl 10.0.07
10.0.0.18
[root@openeuler22 ~]# curl 10.0.07
10.0.0.28
[root@openeuler22 ~]# curl 10.0.07
10.0.0.18
[root@openeuler22 ~]# curl 10.0.07
10.0.0.28

1.2first

first:根据服务器在列表中的位置,自上而下进行调度,但是其只会当第一台服务器的连接数达到上限,新请求才会分配给下一台服务,因此会忽略服务器的权重设置,此方式使用较少。
不支持用cocat进行动态修改权重,可以设置0和1,可以设置其它值但无效,该算法忽略服务器权重。

#first算法实现
[root@centos7 ~]# vim /etc/haproxy/conf.d/test.cfg
frontend WEB_PORT_80
    bind 10.0.0.7:80
    balance first
    mode http
    use_backend web_port_http_nodes

backend web_port_http_nodes
    mode http
    option forwardfor
    server 10.0.0.18 10.0.0.18:80 check inter 3000 fall 3 rise 5
    server 10.0.0.28 10.0.0.28:80 check inter 3000 fall 3 rise 5

[root@centos7 ~]# systemctl reload haproxy

#测试,同时运行下面命令,观察结果
[root@openeuler22 ~]# while true;do curl http://10.0.0.7/index.html ; sleep 0.1 ; done
[root@openeuler22 ~]# curl 10.0.0.7
10.0.0.18
[root@openeuler22 ~]# curl 10.0.0.7
10.0.0.28
[root@openeuler22 ~]# curl 10.0.0.7
10.0.0.18
[root@openeuler22 ~]# curl 10.0.0.7
10.0.0.18
[root@openeuler22 ~]# curl 10.0.0.7
10.0.0.28
[root@openeuler22 ~]# curl 10.0.0.7

 

2.动态算法

动态算法:基于后端服务器状态进行调度适当调整,新请求将优先调度至当前负载较低的服务器,且权重可以在haproxy运行时动态调整无需重启。

2.1roundrobin

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

#实现roundrobin
[root@centos7 ~]# vim /etc/haproxy/conf.d/test.cfg

frontend WEB_PORT_80
    bind 10.0.0.7:80
    balance roundrobin
    mode http
    use_backend web_port_http_nodes

backend web_port_http_nodes
    mode http
    option forwardfor
    server 10.0.0.18 10.0.0.18:80 check inter 3000 fall 3 rise 5
    server 10.0.0.28 10.0.0.28:80 check inter 3000 fall 3 rise 5

[root@centos7 ~]# systemctl reload haproxy

#实现roundrobin
[root@centos7 ~]# vim /etc/haproxy/conf.d/test.cfg

frontend WEB_PORT_80
    bind 10.0.0.7:80
    balance roundrobin
    mode http
    use_backend web_port_http_nodes

backend web_port_http_nodes
    mode http
    option forwardfor
    server 10.0.0.18 10.0.0.18:80 check inter 3000 fall 3 rise 5
    server 10.0.0.28 10.0.0.28:80 check inter 3000 fall 3 rise 5

[root@centos7 ~]# systemctl reload haproxy

#实现roundrobin
[root@centos7 ~]# vim /etc/haproxy/conf.d/test.cfg

frontend WEB_PORT_80
    bind 10.0.0.7:80
    balance leastconn
    mode http
    use_backend web_port_http_nodes

backend web_port_http_nodes
    mode http
    option forwardfor
    server 10.0.0.18 10.0.0.18:80 check inter 3000 fall 3 rise 5
    server 10.0.0.28 10.0.0.28:80 check inter 3000 fall 3 rise 5

[root@centos7 ~]# systemctl reload haproxy

2.3random

在1.9版本开始增加random的负载均衡算法,其基于随机数作为一致性的hash的key,随机负载均衡对于大型服务器场或经常添加或删除服务器非常有用,支持weight的动态调整,weight较大的主机有更大概率获得型请求。

#实现random
[root@centos7 ~]# vim /etc/haproxy/conf.d/test.cfg

frontend WEB_PORT_80
    bind 10.0.0.7:80
    balance random
    mode http
    use_backend web_port_http_nodes

backend web_port_http_nodes
    mode http
    option forwardfor
    server 10.0.0.18 10.0.0.18:80 check inter 3000 fall 3 rise 5
    server 10.0.0.28 10.0.0.28:80 check inter 3000 fall 3 rise 5

[root@centos7 ~]# systemctl reload haproxy

3.其它算法

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

3.1source

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

3.1.1map-base取模法

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

[root@centos7 ~]# vim /etc/haproxy/conf.d/test.cfg

frontend WEB_PORT_80
    bind 10.0.0.7:80
    balance source
    hash-type map-based
    mode http
    use_backend web_port_http_nodes

backend web_port_http_nodes
    mode http
    option forwardfor
    server 10.0.0.18 10.0.0.18:80 check inter 3000 fall 3 rise 5
    server 10.0.0.28 10.0.0.28:80 check inter 3000 fall 3 rise 5

[root@centos7 ~]# systemctl reload haproxy

3.1.2一致性hash

一致性哈希,当服务器的总权重发生变化时,对调度结果影响是局部的,不会引起大的变化,hash (0)mod n,该hash算法是动态的,支持使用socat等工具进行在线权重调整,支持慢启动。
算法:
1、key1=hash(source_ip)%(2^32) [0---4294967295]

2、keyA=hash(后端服务器虚拟ip)%(2^32)

3、将key1和keyA都放在hash环上,将用户请求调度到离key1最近的keyA对应的后端服务器

#实现roundrobin
[root@centos7 ~]# vim /etc/haproxy/conf.d/test.cfg

frontend WEB_PORT_80
    bind 10.0.0.7:80
    balance source
    hash-type consistent
    mode http
    use_backend web_port_http_nodes

backend web_port_http_nodes
    mode http
    option forwardfor
    server 10.0.0.18 10.0.0.18:80 check inter 3000 fall 3 rise 5
    server 10.0.0.28 10.0.0.28:80 check inter 3000 fall 3 rise 5

[root@centos7 ~]# systemctl reload haproxy

3.2uri

基于对用户请求的URI的左半部分或整个uri做hash,再将hash结果对总权重进行取模后,根据最终结果请求转发到后端指定服务器,这确保了只要没有服务器启动或关闭,相同的URI将始终定向到同一台服务器。适用于后端是缓存服务器场景,以最大限度地提高缓存命中率。默认是静态算法,也可以通过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>

[root@rs1 ~]# cd /var/www/html/
[root@rs1 html]# ls
index.html
[root@rs1 html]# for i in {1..10};do echo test$i on 10.0.0.18 > test$i.html;done
[root@rs1 html]# ls
index.html   test1.html  test3.html  test5.html  test7.html  test9.html
test10.html  test2.html  test4.html  test6.html  test8.html

[root@rs2 ~]# cd /var/www/html/
[root@rs2 html]# ls
index.html
[root@rs2 html]# for i in {1..10};do echo test$i on 10.0.0.18 > test$i.html;done
[root@rs2 html]# ls
index.html   test1.html  test3.html  test5.html  test7.html  test9.html
test10.html  test2.html  test4.html  test6.html  test8.html

#uri 一致性hash
[root@centos7 ~]# vim /etc/haproxy/conf.d/test.cfg

frontend WEB_PORT_80
    bind 10.0.0.7:80
    balance uri
    hash-type consistent
    mode http
    use_backend web_port_http_nodes

backend web_port_http_nodes
    mode http
    option forwardfor
    server 10.0.0.18 10.0.0.18:80 check inter 3000 fall 3 rise 5
    server 10.0.0.28 10.0.0.28:80 check inter 3000 fall 3 rise 5

[root@centos7 ~]# systemctl reload haproxy

3.3url_param

url_param对用户请求的url中的params部分中的一个参数key对应的value值作hash计算,并由服务器总权重相除以后派发至某挑出的服务器;通常用于追踪用户,以确保来自同一个用户的请求始终发网同一个real server,如果没key,将按roundrobin算法。
#假设:

url = http://www.baidu.com/foo/bar/index.php?key=value

#则:

host = "www.baidu.com"

url_param = "key=value"

#实现url_param
[root@centos7 ~]# vim /etc/haproxy/conf.d/test.cfg

frontend WEB_PORT_80
    bind 10.0.0.7:80
    balance url_param userid
    hash-type consistent
    mode http
    use_backend web_port_http_nodes

backend web_port_http_nodes
    mode http
    option forwardfor
    server 10.0.0.18 10.0.0.18:80 check inter 3000 fall 3 rise 5
    server 10.0.0.28 10.0.0.28:80 check inter 3000 fall 3 rise 5

[root@centos7 ~]# systemctl reload haproxy

3.4hdr

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

#实现url_param
[root@centos7 ~]# vim /etc/haproxy/conf.d/test.cfg

frontend WEB_PORT_80
    bind 10.0.0.7:80
    balance hdr(User-Agent)
    hash-type consistent
    mode http
    use_backend web_port_http_nodes

backend web_port_http_nodes
    mode http
    option forwardfor
    server 10.0.0.18 10.0.0.18:80 check inter 3000 fall 3 rise 5
    server 10.0.0.28 10.0.0.28:80 check inter 3000 fall 3 rise 5

[root@centos7 ~]# systemctl reload haproxy

3.5rdp-cookie

rdp-cookie对windows远程桌面的负载,使用cookie保持会话,默认是静态,也可以通过hash-type指定map-based和consistent,来定义使用取模法还是一致性hash。
 
 

[root@centos7 ~]# vim /etc/haproxy/conf.d/windows_rdp.cfg

listen hkping_RDP_3389
mode tcp
bind 172.16.0.100:3389
balance rdp-cookie
hash-type consistent
server rdp0 10.0.0.200:3389 check fall 3 rise 5 inter 2000 weight 1

[root@centos7 ~]# systemctl reload haproxy

3.6算法总结和使用场景

#静态
static-rr #tcp/http #做session共享的web集群
first #tcp/http #较少使用
#动态
roundrobin #tcp/http #默认算法,常用
leastconn #tcp/http #数据库
random #tcp/http
#其它静态和动态取决于hash_type是否consistent
source #tcp/http
uri #http #缓存服务器,CDN服务商,蓝汛、百度、阿里云、腾讯
url_param #http #可以实现session保持
hdr #http #基于客户端请求报文头部做下一步处理
rdp-cookie #tcp #基于windows主机,很少使用


4、使用haproxy的ACL实现基于文件后缀名的动静分离

1ACL

访问控制列表(ACL,Access Control Lists)是一种基于包过滤的访问控制技术,它可以根据设定条件对经过服务器传输的数据包进行过滤(条件匹配),即对接收到的报文进行匹配和过滤,基于请求报文头部的源地址、源端口、目标地址、目标端口、请求方法、URL、文件后缀等信息内容进行匹配并执行进一步操作比如允许其通过或丢弃。

1.1ACL配置选项

acl <aclname> <criterion> [flags] [operator] [<value>]
acl 名称 匹配规范 匹配模式 具体操作符 操作对象类型

ACL-Name
#ACL名称,可以使用大写字母、小写字母、数字、冒号、点、中横线和下划线,并且严格区分大小写

ACL-criterion
#定义ACL匹配规范

ACL-flags
#-i 不区分大小写

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

[root@centos7 ~]# vim /etc/haproxy/conf.d/test.cfg

frontend WEB_PORT_80
bind 10.0.0.7:80
balance roundrobin
###################### acl setting ###############################
acl acl_static path_end -i .jpg .jpeg .png .gif .css .js .html
acl acl_php path_end -i .php
###################### acl hosts #################################
use_backend static_hosts if acl_static
use_backend php_hosts if acl_php
default_backend static_hosts
###################### backend hosts #############################
backend static_hosts
server rs1 10.0.0.18:80 check inter 3000 fall 2 rise 5

backend php_hosts
server rs2 10.0.0.28:80 check inter 3000 fall 2 rise 5
[root@centos7 ~]# systemctl reload haproxy


#测试
[root@client ~]#curl 10.0.0.7/index.html
10.0.0.18
[root@client ~]#curl 10.0.0.7/test.php
10.0.0.28

posted on 2022-04-29 21:53  康乐_SH  阅读(43)  评论(0编辑  收藏  举报