被问爆N遍的haproxy基础配置

目录

一.实验环境

二.webserver1的配置

三.webserver2配置

四.haproxy-global全局配置

五.haproxy-proxies配置

六.socat工具:动态修改

 七.haproxy静态算法

七.一.static-rr: 基于权重的轮询调度

七.二.first

八.haproxy动态算法

八.一.roundrobin

八.二.leastconn

九.haproxy其他算法

九.一.source:map-base取模法

九.二.一致性hash

九.三.uri

九.四.uri一致性hash

九.五.url_param一致性hash

九.六.hdr

十.haproxy状态页监控


一.实验环境

功能ip
haproxyeth0:172.25.254.100
webserver1172.25.254.10
webserver2172.25.254.20
客户端Windows

二.webserver1的配置

dnf install nginx -y       #下载软件包
vmset.sh eth0 172.25.254.10 webserver1.rhel9.org      #利用脚本快速配置网卡
echo webserver1 - 172.25.254.10 > /usr/share/nginx/html/index.html   #写入网页内容
systemctl enable --now nginx                 #现在启动,开机自启
echo 172.25.254.10 - index1.html > /usr/share/nginx/html/index1.html
echo 172.25.254.10 - index2.html > /usr/share/nginx/html/index2.html
echo 172.25.254.10 - index3.html > /usr/share/nginx/html/index3.html

三.webserver2配置

dnf install nginx -y
vmset.sh eth0 172.25.254.20 webserver2.rhel9.org
echo webserver2 - 172.25.254.20 > /usr/share/nginx/html/index.html
systemctl enable --now nginx
echo 172.25.254.20 - index1.html > /usr/share/nginx/html/index1.html
echo 172.25.254.20 - index2.html > /usr/share/nginx/html/index2.html
echo 172.25.254.20 - index3.html > /usr/share/nginx/html/index3.html

四.haproxy-global全局配置

vi  /etc/haproxy/haproxy.cfg 
log     127.0.0.1  local2 指定将日志发送到本地回环地址127.0.0.1,并使用 local2 日志设施
chroot   /var/lib/haproxy   设置运行时的根目录为 /var/lib/haproxy,增加安全性。   
pidfile   /var/run/haproxy.pid  指定 HAProxy 进程的 PID(进程标识符)文件的位置为 /var/run/haproxy.pid
maxconn   100000          设置最大并发连接数为 100000  
user      haproxy  指定运行 HAProxy 进程的用户为 haproxy   
group     haproxy  指定运行 HAProxy 进程的用户组为 haproxy
daemon      以守护进程的方式在后台运行 HAProxy  
###################多进程和socket文件配置#####################
	#utilize system-wide crypto-policies
	ssl-default-bind-ciphers PROFILE=SYSTEM
	ssl-default-server-ciphers PROFILE=SYSTEM
	nbproc 2     #启用多进程
	cpu-map 1 0   #进程和cpu核心绑定防止cpu抖动从而减少系统资源消耗
	cpu-map 2 1   #2 表示第二个进程,1 表示第二个cpu核心
###################多线程和socket文件配置#####################
	#utilize system-wide crypto-policies
	ssl-default-bind-ciphers PROFILE=SYSTEM
	ssl-default-server-ciphers PROFILE=SYSTEM
	nbthread 2            #启用多线程
重启:systemctl restart haproxy.service
验证:pstree -p | grep haproxy
tips:多线程与多进程互斥,不能同时启动。

五.haproxy-proxies配置

dnf install haproxy -y
rpm -qc haproxy    查看相关文件路径
vi  /etc/haproxy/haproxy.cfg 
------------------------------frontend前端和backend后端------------------------------
.......
#main frontend which proxys to the backends
#-----------------------------------------------------------
frontend webcluster          #定义了一个名为 webcluster 的前端部分。
bind *:80                    #绑定所有接口的 80 端口来接收请求
mode http                    #指明工作模式为 HTTP 协议
use_backend webcluster-host   #将接收到的请求转发到名为 webcluster-host 的后端。
backend webcluster-host    #定义了名为 webcluster-host 的后端部分。
balance     roundrobin               #使用轮询的负载均衡算法
server      web1      172.25.254.10:80   
server      web2      172.25.254.20:80   #分别定义了两台后端服务器 web1 和 web2 及其对应的 IP 地址和端口。

------------------------------listen同时拥有前后端------------------------------
vi  /etc/haproxy/haproxy.cfg 
......
#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   #写域名的话,需要本地解析。
重启:systemctl restart haproxy.service
验证:curl 172.25.254.100;分别可以stop其中的nginx看看前端和后端是否已起服务。都成功的话会出现第一个请求可能会被发送到 web1 ,第二个请求发送到 web2 ,第三个请求又回到 web1 ,以此类推,实现负载均衡。
------------------------------srever配置------------------------------
dnf install httpd -y
vi /etc/httpd/conf/httpd.conf
	#listen 12.34.56.78:80
	listen 8080  #改监听
systemctl enable --now httpd
echo 刘大帅随时server with you > /var/www/html/index.html   #在浏览器访问172.25.254.100:8080,有内容就对。
vi  /etc/haproxy/haproxy.cfg 
......
#main frontend which proxys to the backends
#-------------------------------------------------------
listen webcluster
bind *:80
mode http
balance     roundrobin
server      web1      172.25.254.10:80 check inter 2 fall 3 rise 5 weight 2      #在后面加上disabled该web下线
server      web2      172.25.254.20:80 check inter 2 fall 3 rise 5 weight 1      #安全检查的间隔为 2 秒,连续失败 3 次认为服务器不可用,连续成功 5 次认为服务器恢复可用,权重为 1 
server web_server 172.25.254.100:8080 backup   #backup不会参与调度,只有前两台都出现问题才会。
重启:systemctl restart haproxy
测试:将分别stop两台其中的nginx,curl 172.25.254.100.有内容就行。
-------------------------------重定向到网站---------------------------------
listen webcluster
bind *:80
mode http
balance     roundrobin
redirect prefix http://www.baidu.com/
重启:systemctl restart haproxy
测试:浏览器访问172.25.254.100,会跳到百度网站。

六.socat工具:动态修改

1.单进程热处理:
vi  /etc/haproxy/haproxy.cfg 
........
# turn on stats unix socket
stats socket /var/lib/haproxy/stats mode 600 level admin   #加权限
........
重启:systemctl restart haproxy
dnf install socat -y
ll /var/lib/haproxy/   #看有几个文件,跟配置文件里面对应的
[root@haproxy ~]#echo "show info" | socat stdio /var/lib/haproxy/stats
 [root@haproxy ~]#echo "show servers state" | socat stdio /var/lib/haproxy/stats
 [root@haproxy ~]#echo get weight webcluster/web1 | socat stdio /var/lib/haproxy/stats   #看权重
 [root@haproxy ~]#echo "set weight webcluster/web1 1" | socat stdio /var/lib/haproxy/stats   #改权重
 [root@haproxy ~]#echo "disable server webcluster/web1" | socat stdio /var/lib/haproxy/haproxy.sock   #下线
 [root@haproxy ~]#echo "enable server webcluster/web1" |  socat stdio /var/lib/haproxy/haproxy.sock   #上线
 2.多进程热处理:
 vi  /etc/haproxy/haproxy.cfg 
........
# turn on stats unix socket
stats socket /var/lib/haproxy/stats1 mode 600 level admin process 1 
stats socket /var/lib/haproxy/stats2 mode 600 level admin process 2
#utilize system-wide crypto-policies
	ssl-default-bind-ciphers PROFILE=SYSTEM
	ssl-default-server-ciphers PROFILE=SYSTEM
	nbproc 2     
	cpu-map 1 0   
	cpu-map 2 1  
........
重启:systemctl restart haproxy
ll /var/lib/haproxy/   #看有几个文件,跟配置文件里面对应的
[root@haproxy ~]#echo "show info" | socat stdio /var/lib/haproxy/stats1
[root@haproxy ~]#echo "show info" | socat stdio /var/lib/haproxy/stats2

 七.haproxy静态算法

七.一.static-rr: 基于权重的轮询调度
vi  /etc/haproxy/haproxy.cfg 
......
#main frontend which proxys to the backends
#-------------------------------------------------------
listen webcluster
bind *:80
mode http
balance   static-rr
server    web1  172.25.254.10:80 check inter 2 fall 3 rise 5 weight 2     
server    web2  172.25.254.20:80 check inter 2 fall 3 rise 5 weight 1        
#为了效果,关闭多进程。
#为了效果,关闭多进程。
#为了效果,关闭多进程。
重启:systemctl restart haproxy
echo "set weight webcluster/web1 1" | socat stdio /var/lib/haproxy/stats    #看看支不支持热处理,结果是不能修改
七.二.first

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

vi  /etc/haproxy/haproxy.cfg 
......
#main frontend which proxys to the backends
#-------------------------------------------------------
listen webcluster
bind *:80
mode http
balance   first
server    web1  172.25.254.10:80 maxconn 1 check inter 2 fall 3 rise 5 weight 2  
server    web2  172.25.254.20:80 check inter 2 fall 3 rise 5 weight 1        
#为了效果,关闭多进程。
#为了效果,加一个链接最大数maxconn 1,后面需要改回来。
#为了效果,加一个链接最大数maxconn 1,后面需要改回来。
重启:systemctl restart haproxy
测试:多台主机执行循环while true ;do curl 172.25.254.100;sleep 0.1;done

八.haproxy动态算法

八.一.roundrobin

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

vi  /etc/haproxy/haproxy.cfg 
......
#main frontend which proxys to the backends
#-------------------------------------------------------
listen webcluster
bind *:80
mode http
balance   roundrobin    #还是以负载小的主
server    web1  172.25.254.10:80 check inter 2 fall 3 rise 5 weight 2  
server    web2  172.25.254.20:80 check inter 2 fall 3 rise 5 weight 1        
重启:systemctl restart haproxy
echo "set weight webcluster/web1 1" | socat stdio /var/lib/haproxy/stats    #支持热处理
八.二.leastconn

leastconn加权的最少连接的动态;支持权重的运行时调整和慢启动,即:根据当前连接最少的后端服务器而非权重进行优先调度(新客户端连接);比较适合长连接的场景使用,比如:MySQL等场景。

vi  /etc/haproxy/haproxy.cfg 
......
#main frontend which proxys to the backends
#-------------------------------------------------------
listen webcluster
bind *:80
mode http
balance   leastconn
server    web1  172.25.254.10:80 check inter 2 fall 3 rise 5 weight 2  
server    web2  172.25.254.20:80 check inter 2 fall 3 rise 5 weight 1        
重启:systemctl restart haproxy
echo "set weight webcluster/web1 1" | socat stdio /var/lib/haproxy/stats    #支持热处理

九.haproxy其他算法

九.一.source:map-base取模法

源地址hash,基于用户源地址hash并将请求转发到后端服务器,后续同一个源地址请求将被转发至同-个后端web服务器。

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

所谓取模运算,就是计算两个数相除之后的余数,10%7=3,7%4=3

map-based算法:基于权重取模,hash(source_ip)%所有后端服务器相加的总权重。

vi  /etc/haproxy/haproxy.cfg 
......
#main frontend which proxys to the backends
#-------------------------------------------------------
listen webcluster
bind *:80
mode http
balance   source
server    web1  172.25.254.10:80 check inter 2 fall 3 rise 5 weight 2  
server    web2  172.25.254.20:80 check inter 2 fall 3 rise 5 weight 1        
重启:systemctl restart haproxy
测试:源地址hash,基于用户源地址hash并将请求转发到后端服务器,后续同一个源地址请求将被转发至同-个后端web服务器。所以可以直接for i in {1..6}; do curl 172.25.254.100; done
echo "set weight webcluster/web1 1" | socat stdio /var/lib/haproxy/stats  #否支持热处理
echo "set weight webcluster/web1 0" | socat stdio /var/lib/haproxy/stats   
echo "get weight webcluster/web1" | socat stdio /var/lib/haproxy/stats  #只能动态上线和下线
九.二.一致性hash
vi  /etc/haproxy/haproxy.cfg 
......
#main frontend which proxys to the backends
#-------------------------------------------------------
listen webcluster
bind *:80
mode http
balance   source
hash-type consistent   #不加就不支持在线调整
server    web1  172.25.254.10:80 check inter 2 fall 3 rise 5 weight 2  
server    web2  172.25.254.20:80 check inter 2 fall 3 rise 5 weight 1        
重启:systemctl restart haproxy
效果是一样的;变了权重,也是一样。
九.三.uri

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

vi  /etc/haproxy/haproxy.cfg 
......
#main frontend which proxys to the backends
#-------------------------------------------------------
listen webcluster
bind *:80
mode http
balance   uri
server    web1  172.25.254.10:80 check inter 2 fall 3 rise 5 weight 2  
server    web2  172.25.254.20:80 check inter 2 fall 3 rise 5 weight 1        
重启:systemctl restart haproxy
测试:在web1;2中写入网页内容,在测试端
curl 172.25.254.100/index1.html
172.25.254.100/index2.html
172.25.254.100/index3.html
九.四.uri一致性hash
vi  /etc/haproxy/haproxy.cfg 
......
#main frontend which proxys to the backends
#-------------------------------------------------------
listen webcluster
bind *:80
mode http
balance   uri
hash-type consistent
server    web1  172.25.254.10:80 check inter 2 fall 3 rise 5 weight 2  
server    web2  172.25.254.20:80 check inter 2 fall 3 rise 5 weight 1        
重启:systemctl restart haproxy
测试:是否支持热处理
九.五.url_param一致性hash

url_param对用户请求的url中的 params 部分中的一个参数key对应的value值作hash计算,并由服务器总权重相除以后派发至某挑出的服务器,后端搜索同一个数据会被调度到同一个服务器,多用与电商通常用于追踪用户,以确保来自同一个用户的请求始终发往同一个realserver。如果无没key,将按roundrobin算法。

vi  /etc/haproxy/haproxy.cfg 
......
#main frontend which proxys to the backends
#-------------------------------------------------------
listen webcluster
bind *:80
mode http
balance   url_param name,userid
hash-type consistent
server    web1  172.25.254.10:80 check inter 2 fall 3 rise 5 weight 1  #看效果,修改权重。
server    web2  172.25.254.20:80 check inter 2 fall 3 rise 5 weight 1        
重启:systemctl restart haproxy
测试:在测试端curl 172.25.254.100/index1.html?name=lee
172.25.254.100/index1.html?name=lee
172.25.254.100/index1.html?name=test
172.25.254.100/index1.html?name=test
九.六.hdr

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

vi  /etc/haproxy/haproxy.cfg 
......
#main frontend which proxys to the backends
#-------------------------------------------------------
listen webcluster
bind *:80
mode http
balance   hdr(User-Agent)   #访问的浏览器
hash-type consistent
server    web1  172.25.254.10:80 check inter 2 fall 3 rise 5 weight 1  
server    web2  172.25.254.20:80 check inter 2 fall 3 rise 5 weight 1        
重启:systemctl restart haproxy
测试:在测试端curl -vA "sogo" 172.25.254.100/index1.html
curl -vA "google" 172.25.254.100/index1.html
curl -vA "firefox" 172.25.254.100/index1.html
curl -vA "360" 172.25.254.100/index1.html
用两个不同的浏览器输入172.25.254.100

十.haproxy状态页监控

vi  /etc/haproxy/haproxy.cfg 
......
server    web2  172.25.254.20:80 check inter 2 fall 3 rise 5 weight 1  
listen stats
	bind *:9999
	mode http
	stats enable
	stats refresh 10   #好久刷新
	stats uri /status自定义stats    #自定义stats page uri
	stats auth lee:lee     #认证,可以出现多次
.....
重启:systemctl restart haproxy
测试:在本地浏览器里输入:172.25.254.100:9999/status
  • 27
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值