1、haproxy调度算法总结
2、haproxy+nginx实现四、七层IP透传

-------------------------------------------------------------------------------------------

1 haproxy调度算法总结

1.1 静态算法

#特点:
不关注real_server负载、链接数等情况 无法动态调整real_server权重 不支持real_server慢启动

1.1.1 static-rr

#基于权重值轮询调度
 real_server数量:无限制
 mode:支持tcp/http
 
#示例
listen test_web
 bind 10.0.0.7:80
 mode tcp
 balance static-rr
 server web1 10.0.0.27:8080 weight 1 check inter 3s fall 3 rise 5
 server web2 10.0.0.100:8080 weight 1 check inter 3s fall 3 rise 5

1.1.2 first

#根据配置文件real_server的顺序,自上而下调度,当连接达到上限时,才调度到下一个real_server
 mode:支持tcp/http
 忽略权重设置:动态更改权重不报错,但不会影响最终结果

#示例
listen test_web
 bind 10.0.0.7:80
 mode tcp
 balance first
 server web1 10.0.0.27:8080 weight 1 check inter 3s fall 3 rise 5
 server web2 10.0.0.100:8080 weight 1 check inter 3s fall 3 rise 5

1.2 动态算法

#特点:
关注real_server运行状态 支持动态调整real_server权重 支持real_server慢启动

1.2.1 roundrobin

#默认算法
 在static-rr的基础上,支持real_server慢启动和动态调整real_server权重
 real_server数量:4095
 mode:支持tcp/http
 
#示例
listen test_web
 bind 10.0.0.7:80
 mode tcp
 balance roundrobin
 server web1 10.0.0.27:8080 weight 1 check inter 3s fall 3 rise 5
 server web2 10.0.0.100:8080 weight 1 check inter 3s fall 3 rise 5

1.2.2 leastconn

#优先调度到连接数最少的real_server,适合长连接使用场景,如real_server为MySQL
 mode:支持tcp/http

#示例
listen test_web
 bind 10.0.0.7:80
 mode tcp
 balance leastconn
 server web1 10.0.0.27:8080 weight 1 check inter 3s fall 3 rise 5
 server web2 10.0.0.100:8080 weight 1 check inter 3s fall 3 rise 5

1.2.3 random

#1.9版本起新增算法
 使用随机数作为一致性hash的key,对大型服务场景或经常上下线real_server非常有用
 mode:支持tcp/http
 
#示例
listen test_web
 bind 10.0.0.7:80
 mode tcp
 balance random
 server web1 10.0.0.27:8080 weight 1 check inter 3s fall 3 rise 5
 server web2 10.0.0.100:8080 weight 1 check inter 3s fall 3 rise 5

1.3 其他算法

#不添加附加选项或添加附加选项hash-type map-based为静态算法
 添加附加选项hash-type consistent为动态算法
 
#map-based
 取模法,根据算法类型选择不同key(如源IP、uri等),对real_server总权重的hash数组取模后调度
 缺点:当real_server上下线导致总权重发生变化,所有取模运算结果都会改变
#consistent
 一致性hash,根据算法类型选择不同key,对real_server总权重进行hash运算后调度
 优点:当real_server上下线导致总权重发生变化,影响部分hash运算结果

1.3.1 source

#使用客户端源IP作为key进行运算后调度,相同源IP的请求调度到同一个real_server
 适用于session保持、缓存业务场景
 mode:支持tcp/http

#示例
#取模法
listen test_web
 bind 10.0.0.7:80
 mode tcp
 balance source
 server web1 10.0.0.27:8080 weight 1 check inter 3s fall 3 rise 5
 server web2 10.0.0.100:8080 weight 1 check inter 3s fall 3 rise 5

#一致性hash
listen test_web
 bind 10.0.0.7:80
 mode tcp
 balance source
 hash-type consistent
 server web1 10.0.0.27:8080 weight 1 check inter 3s fall 3 rise 5
 server web2 10.0.0.100:8080 weight 1 check inter 3s fall 3 rise 5

1.3.2 uri

#使用请求的uri作为key进行运算后调度,相同uri的请求调度到同一个real_server
 适用于缓存代理场景,如CDN缓存源站场景
 mode:只支持http

#示例
#取模法
listen test_web
 bind 10.0.0.7:80
 mode http
 balance uri
 server web1 10.0.0.27:8080 weight 1 check inter 3s fall 3 rise 5
 server web2 10.0.0.100:8080 weight 1 check inter 3s fall 3 rise 5

#一致性hash
listen test_web
 bind 10.0.0.7:80
 mode http
 balance uri
 hash-type consistent
 server web1 10.0.0.27:8080 weight 1 check inter 3s fall 3 rise 5
 server web2 10.0.0.100:8080 weight 1 check inter 3s fall 3 rise 5

1.3.3 url_param

#使用请求的url中的params作为key进行运算后调度,url中相同params的请求调度到同一个real_server
 mode:只支持http

#示例
#取模法
listen test_web
 bind 10.0.0.7:80
 mode http                                                                   
 balance url_param name
 server web1 10.0.0.27:8080 weight 1 check inter 3s fall 3 rise 5
 server web2 10.0.0.100:8080 weight 1 check inter 3s fall 3 rise 5

#一致性hash
listen test_web
 bind 10.0.0.7:80
 mode http    
 balance url_param name
 hash-type consistent
 server web1 10.0.0.27:8080 weight 1 check inter 3s fall 3 rise 5
 server web2 10.0.0.100:8080 weight 1 check inter 3s fall 3 rise 5

1.3.4 hdr

#使用请求中指定的首部字段作为key进行运算后调度,key值无效时,使用默认算法调度
 mode:只支持http

#示例
#取模法
listen test_web
 bind 10.0.0.7:80
 mode http
 balance hdr(User-Agent)
 server web1 10.0.0.27:8080 weight 1 check inter 3s fall 3 rise 5
 server web2 10.0.0.100:8080 weight 1 check inter 3s fall 3 rise 5

#一致性hash
listen test_web
 bind 10.0.0.7:80
 mode http
 balance hdr(User-Agent)
 hash-type consistent
 server web1 10.0.0.27:8080 weight 1 check inter 3s fall 3 rise 5
 server web2 10.0.0.100:8080 weight 1 check inter 3s fall 3 rise 5

1.3.5 rdp-cookie

#对windows远程桌面的负载,使用cookie保持会话
 mode:只支持tcp

#示例
#取模法
listen rdp_9527
 bind 10.0.0.7:9527
 mode tcp
 balance rdp-cookie
 server rdp0 10.0.0.27:9527 weight 1 check inter 3s fall 3 rise 5

#一致性hash
listen rdp_9527
 bind 10.0.0.7:9527
 mode tcp
 balance rdp-cookie
 hash-type consistent
 server rdp0 10.0.0.27:9527 weight 1 check inter 3s fall 3 rise 5

1.4 算法总结及应用场景

#算法           #模式         #类型    #场景
static-rr      tcp/http      静态     做了session共享的web集群
first          tcp/http      静态     较少

roundrobin     tcp/http      动态
leastconn      tcp/http      动态     数据库等长连接应用
random         tcp/http      动态     大型服务场景或经常上下线real_server非常有用

#可静可动,取决于hash_type是否为consistent
source         tcp/http              基于源公网IP的会话保持
uri            http                  缓存代理,如CDN缓存源站场景
url_param      http
hdr            http                  基于请求中的指定首部字段做下一步处理
rdp-cookie     tcp                   远程桌面

2 haproxy+nginx实现四、七层IP透传

#环境
 haproxy    #10.0.0.7
 nginx      #10.0.0.27
 client     #10.0.0.100

2.1 四层IP透传

#haproxy配置

#yum安装haproxy
# yum -y install haproxy

#修改配置文件
# tail -n5 /etc/haproxy/haproxy.cfg
listen test_web
 bind 10.0.0.7:80
 mode tcp
 balance roundrobin
 server web1 10.0.0.27:8080 send-proxy check inter 3s fall 3 rise 5
 
#启动服务
# haproxy -f /etc/haproxy/haproxy.cfg -c && systemctl enable --now haproxy
===========================================================================
#nginx配置

#real_server yum安装nginx
# yum -y install nginx

#修改主配置文件,日志格式添加变量proxy_protocol_addr
# grep -A3 'log_format' /etc/nginx/nginx.conf
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for" "$proxy_protocol_addr"';

#新增配置文件
# cat /etc/nginx/conf.d/www.testou.com.conf
server {
  listen 8080 proxy_protocol;
  server_name www.testou.com;
  location / {
    root /usr/share/nginx/html;
    index www.testou.com.html;
  }
}

#准备测试资源
# hostname -I > /usr/share/nginx/html/www.testou.com.html

#启动服务
# nginx -t && systemctl enable --now nginx
===========================================================================
#客户端测试
# tail -n1 /etc/hosts
10.0.0.7 www.testou.com
# curl www.testou.com
10.0.0.27
===========================================================================
#real_server端日志
# tail -n1 /var/log/nginx/access.log 
10.0.0.7 - - [07/Feb/2023:14:01:50 +0800] "GET / HTTP/1.1" 200 11 "-" "curl/7.58.0" "-" "10.0.0.100"

2.2 七层IP透传

#haproxy配置

#yum安装haproxy
# yum -y install haproxy

#修改配置文件
# tail -n6 /etc/haproxy/haproxy.cfg 
listen test_web
 bind 10.0.0.7:80
 mode http
 balance roundrobin
 option forwardfor
 server web1 10.0.0.27:8080 check inter 3s fall 3 rise 5
 
#启动服务
# haproxy -f /etc/haproxy/haproxy.cfg -c && systemctl enable --now haproxy
===========================================================================
#nginx配置

#real_server yum安装nginx
# yum -y install nginx

#新增配置文件
# cat /etc/nginx/conf.d/www.testou.com.conf
server {
  listen 8080;
  server_name www.testou.com;
  location / {
    root /usr/share/nginx/html;
    index www.testou.com.html;
  }
}

#准备测试资源
# hostname -I > /usr/share/nginx/html/www.testou.com.html

#启动服务
# nginx -t && systemctl enable --now nginx
===========================================================================
#客户端测试
# tail -n1 /etc/hosts
10.0.0.7 www.testou.com
# curl www.testou.com
10.0.0.27
===========================================================================
#real_server端日志
# tail -n1 /var/log/nginx/access.log 
10.0.0.7 - - [07/Feb/2023:11:43:40 +0800] "GET / HTTP/1.1" 200 11 "-" "curl/7.58.0" "10.0.0.100"
posted on 2023-02-07 14:10  不期而至  阅读(27)  评论(0编辑  收藏  举报