负载均衡
负载均衡: Load Balance ,简称 LB ,是一种服务或基于硬件设备等实现的高可用反向代理技术,负载均衡将特定的业务(web 服务、网络流量等 ) 分担给指定的一个或多个后端特定的服务器或设备,从而提高了 公司业务的并发处理能力、保证了业务的高可用性、方便了业务后期的水平动态扩展。
负载均衡的作用
Web服务器的动态水平扩展-->对用户无感知
增加业务并发访问及处理能力-->解决单服务器瓶颈问题
节约公网IP地址-->降低IT支出成本 隐藏内部服务器IP-->提高内部服务器安全性
配置简单-->固定格式的配置文件 功能丰富-->支持四层和七层,支持动态下线主机
性能较强-->并发数万甚至数十万
四层负载与七层负载的区别
分层位置:四层负载均衡在传输层及以下,七层负载均衡在应用层及以下
性能 :四层负载均衡架构无需解析报文消息内容,在网络吞吐量与处理能力上较高:七层可支持解析应用层报文消息内容,识别URL、Cookie、HTTP header等信息。
原理:四层负载均衡是基于ip+端口;七层是基于虚拟的网址或主机IP等。
功能类比:四层负载均衡类似于路由器;七层类似于代理服务器。
安全性:四层负载均衡无法识别DDoS攻击;七层可防御SYN Cookie/Flood攻击
haproxy的安装
yum install -y haproxy
配置文件:/etc/haproxy/haproxy.cfg
global:全局配置段
进程及安全配置相关的参数
性能调整相关参数
Debug参数
proxies:代理配置段
defaults:为frontend,backend,listen提供默认配置
frontend:前端,相当于nginx中的server0}
backend:后端,相当于nginx中的upstream
listen:同时拥有前端和后端配置,配置简单,生产推荐使用
global配置
log 127.0.0.1 local2
#运行目录
chroot /var/lib/haproxy
#pid文件路径
pidfile /var/run/haproxy.pid
maxconn 4000
user haproxy
group haproxy
daemon
#开启多线程
nbthread 数字
#开启多进程
nbproc 数字
cpu-map 1 0 #为进程指定cpu
cpu-map 2 1
# 套接字文件
stats socket /var/lib/haproxy/stats1 mode 600 level admin process 1
#查看进程数
pstree -p | grep haproxy
#查看线程数
cat /proc/子进程id/status
defaults配置
针对以下的frontend、backed、listen生效
defaults
mode http #默认工作类型
log global
option httplog
option dontlognull
option http-server-close
option forwardfor except 127.0.0.0/8
option redispatch
retries 3
timeout http-request 10s
timeout queue 1m
timeout connect 10s #最长连接等待时间
timeout client 1m #与客户端最长非活动时间
timeout server 1m #请求处理时长
timeout http-keep-alive 10s #会话保持超时时间
timeout check 10s #默认检测时间
maxconn 3000
frontend配置
前端servername、类似于虚拟主机和lvs的集群
frontend main
bind *:5000 #监听地址及其端口号
use_backend static if url_static
default_backend app
backed配置
后端服务器组,类似于RS服务器
mode httpltcp #指定负载协议类型,和对应的frontend必须一致
option #配置选项
server #定义后端realserver,必须指定IP和端口
listen配置
将frontend和backed合并在一起配置,更加简洁
listen webserver_80
bind 172.25.254.100:80
mode http
option forwardfor
server webserver1 192.168.0.101:80 check inter 3s fa11 3 rise 5
#每隔三秒检查一次,连续失败三次则转为线下,失败之后重连5次则转为线上
server webserver2 192.168.0.102:80 check inter 3s fa11 3 rise 5
haproxy算法
静态算法
static-rr基于权重的轮询调度
不支持运行时利用socat进行权重的动态调整(只支持0和1)
不支持端服务器慢启动(服务刚启动时缓慢的给流量)
其后端主机数量没有限制,相当于lvs中的wrr
[root@node1 ~]# vim /etc/haproxy/haproxy.cfg
listen web_80
bind 10.10.100.101:80
mode http
balance static-rr
server web01 10.10.100.102:80 weight 1 check inter 3000 fall 3 rise 5
server web02 10.10.100.103:80 weight 1 check inter 3000 fall 3 rise 5
#测试
[root@node1 ~]# while true; do curl -L http://10.10.100.101; sleep 1s; done
web02 10.10.100.103
web01 10.10.100.102
web01 10.10.100.102
web02 10.10.100.103
first
根据服务器在列表的位置,从上往下调度,只有第一个满了才调度到下一台
会忽略服务器的权重设置
不支持socat进行动态修改权重
[root@node1 ~]# vim /etc/haproxy/haproxy.cfg
listen web_80
bind 10.10.100.101:80
mode http
balance first
server web01 10.10.100.102:80 maxconn 2 weight 1 check inter 3000 fall 3 rise 5
server web02 10.10.100.103:80 weight 1 check inter 3000 fall 3 rise 5
#测试
[root@node1 ~]# while true; do curl http://10.10.100.101; sleep 0.1;done
动态算法
roundrobin
基于权重的轮询动态调度算法
支持权重的运行时调整,不同于lvs中的rr轮询的一点是支持慢启动
后端支持4095个real server
支持对real server权重动态调整,将流量打到权重高且负载小的主机,优先选择负载小
listen web_80
bind 10.10.100.101:80
mode http
balance roundrobin
server web01 10.10.100.102:80 weight 1 check inter 3000 fall 3 rise 5
server web02 10.10.100.103:80 weight 1 check inter 3000 fall 3 rise 5
#测试
[root@node1 ~]# while true; do curl http://10.10.100.101; sleep 1;done
web01 10.10.100.102
web02 10.10.100.103
web01 10.10.100.102
web02 10.10.100.103
#动态调整权重
[root@node1 ~]# echo "get weight web_80/web01" |socat stdio /var/lib/haproxy/haproxy.sock1
1 (initial 1)
[root@node1 ~]# echo "set weight web_80/web01 3" |socat stdio /var/lib/haproxy/haproxy.sock1
[root@node1 ~]# echo "get weight web_80/web01" |socat stdio /var/lib/haproxy/haproxy.sock1
3 (initial 1)
leastconn
加权最少链接的动态
支持权重的运行时调整和慢启动,根据当前连接最少的后端服务器而不是权重进行优先调度
比较适合长连接的场景使用,比如MySQL
listen web_80
bind 10.10.100.101:80
mode http
balance leastconn
server web01 10.10.100.102:80 weight 1 check inter 3000 fall 3 rise 5
server web02 10.10.100.103:80 weight 1 check inter 3000 fall 3 rise 5
其他算法
source
源地址hash,基于用户源地址hash并将请求转发到后端服务器,后续同一个源地址请求将被转发至同一个web服务器中。默认为静态方式,可以通过hash-type支持选项更改,一般是在不插入Cookie的TCP模式下使用,也可以拒绝会话cookie的客户提供最好的会话粘性,有两种转发客户端请求到后端服务器选取计算方法,分别为取模法和hashi一致法
#map-base取模法
listen web_80
bind 10.10.100.101:80
mode http
balance source
server web01 10.10.100.102:80 weight 1 check inter 3000 fall 3 rise 5
server web02 10.10.100.103:80 weight 1 check inter 3000 fall 3 rise 5
#缺点:服务器总权重发生变化时,如有服务器上线或下线,都会导致调度结果整体改变,导致会话丢失
#一致性hash算法
listen web_80
bind 10.10.100.101:80
mode http
balance source
#指定使用一致性hash
hash-type consistent
server web01 10.10.100.102:80 weight 1 check inter 3000 fall 3 rise 5
server web02 10.10.100.103:80 weight 1 check inter 3000 fall 3 rise 5
uri
资源在互联网中的唯一标识符
基于对用户请求的uri的左半部分或整个uri做hash,再将hash结果对总权重进行取模后根据最终结果将请求转发到后端指定服务器
url_param
对用户请求的url中的params部分中的一个参数key对应的value值进行hash计算,常用于追踪用户,确保来自同一个用户的请求始终发往同一个real server,如果没key按rr算法执行
hdr
针对用户每个http头部(header)请求中的指定信息做hash,由name指定的http头部将会被取出并做hash,然后由服务器总权重取模之后派发至某跳出的服务器,如果无有效值则会使用默认的轮询调度,适用于不同的浏览器访问不同的主机
IP透传
IP 透传指的是在网络通信中,允许一个数据包在经过中间设备或网络节点时,保留其原始的源 IP 地址和目的 IP 地址,而不被中间设备修改或替换。
四层IP透传
#1、#nginx 配置:在访问日志中通过变量$proxy_protocol_addr 记录透传过来的客户端IP
[root@web1 ~]# cat /etc/nginx/nginx.conf
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
' "$proxy_protocol_addr"' #记录透传过来的客户IP
'"$http_user_agent" "$http_x_forwarded_for"';
server {
listen 80 proxy_protocol; #无法直接访问,只能走四层
listen [::]:80;
server_name _;
root /usr/share/nginx/html;
#2、haproxy设置
[root@haproxy conf.d]# cat ip.cfg
listen ip
bind *:80
mode tcp #四层透传
balance roundrobin
server web1 192.168.84.141:80 send-proxy check inter 3 fall 3 rise 5 weight 1
server web2 192.168.84.142:80 check inter 3 fall 3 rise 5 weight 1
七层IP透传
#1、#nginx 配置:在访问日志中通过变量"$proxy_add_x_forwarded_for"
[root@web1 ~]# cat /etc/nginx/nginx.conf
http {
log_format main '"$proxy_add_x_forwarded_for" - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
server {
listen 80;
listen [::]:80;
server_name _;
root /usr/share/nginx/html;
#2、haproxy设置
[root@haproxy conf.d]# cat ip.cfg
listen ip
bind *:80
mode http #七层透传
balance roundrobin
server web1 192.168.84.141:80 send-proxy check inter 3 fall 3 rise 5 weight 1
server web2 192.168.84.142:80 check inter 3 fall 3 rise 5 weight 1