haproxy的调度算法
1、静态调度
-
静态算法:按照事先定义好的规则轮询公平调度,不关心后端服务器的当前负载、链接数和相应速度等,且无法实时修改权重,只能重启后生效
• static-rr:基于权重的轮询调度,不支持权重的运行时调整及后端服务器慢启动,其后端主机数量没有限制
• first:根据服务器在列表中的位置,自上而下进行调度,但是其只会当第一台服务器的连接数达到上限,新请求才会分配给下一台服务,因此会忽略服务器的权重设置 -
动态算法:基于后端服务器 状态进行调度适当调整,比如优先调度至当前负载较低的服务器,且权重可以在haproxy运行时动态调整无需重启
• roundrobin:基于权重的轮询动态调度算法,支持权重的运行时调整,不等于lvs 的rr,支持慢启动即新加的服务器会逐渐增加转发数,每个后端backend中最多支持4095个server,此为默认调度算法, server 权重设置 weight
• leastconn: 加权的最少连接的动态,支持权重的运行时调整和慢启动,即当前后端服务器连接最少的优先调度,比较适合长连接的场景使用,比如MySQL等场景。 -
source:源地址hash,基于用户源地址hash并将请求转发到后端服务器,默认为静态即取模方式,但是可以通过hash-type支持的选项更改,后续同一个源地址请求将被转发至同一个后端web服务器,比较适用于session保持/缓存业务等场景。
consistent:一致性哈希,该hash是动态的,支持在线调整权重,支持慢启动,优点在于当服务器的总权重发生变化时,对调度结果影响是局部的,不会引起大的变动
map-based:取模法,基于服务器权重的hash数组取模,该hash是静态的即不支持在线调整权重,不支持慢启动,其对后端服务器调度均衡,缺点是当服务器的总权重发生变化时,即有服务器上线或下线,都会因权重发生变化而导致调度结果整体改变, hash(o) mod n
-
uri
基于对用户请求的uri做hash并将请求转发到后端指定服务器
-
url_param
对用户请求的url中的<params>部分中的参数name作hash计算,并由服务器总权重相除以后派发至某挑出的服务器;通常用于追踪用户,以确保来自同一个用户的请求始终发往同一个Backend Server
-
hdr
hdr(<name>):针对用户每个http头部(header)请求中的指定信息做hash, 此处由<name>指定的http首部将会被取出并做hash计算, 然后由服务器总权重相除以后派发至某挑出的服务器, 假如无有效的值,则会被轮询调度
-
rdp-cookie
rdp-cookie对远程桌面的负载,使用cookie保持会话,用得不多
安装高版本haproxy
#二选一,国内非常慢,还是推荐源码编译
1、安装cenots提供的sclo_rh库
yum install https://cbs.centos.org/kojifiles/packages/centos-release-scl-rh/2/3.el7.centos/noarch/centos-release-scl-rh-2-3.el7.centos.noarch.rpm -y
yum clean all
yum install rh-haproxy18-haproxy -y
ln -s /opt/rh/rh-haproxy18/root/sbin/haproxy /usr/sbin
haproxy -v
HA-Proxy version 1.8.17 2019/01/08
Copyright 2000-2019 Willy Tarreau <willy@haproxy.org>
2、配置第三方yum仓库
vim /etc/yum.repos.d/ius.repo
[ius]
name=ius
baseurl=https://dl.iuscommunity.org/pub/ius/stable/CentOS/$releasever/$basearch/
gpgcheck=0
enabled=1
yum clean all
yum install haproxy18u -y
haproxy -v
HA-Proxy version 1.8.20 2019/04/25
Copyright 2000-2019 Willy Tarreau <willy@haproxy.org>
#自定义配置文件路径
vim /usr/lib/systemd/system/haproxy.service
[Unit]
Description=HAProxy Load Balancer
After=network.target
[Service]
Environment="CONFIG=/etc/haproxy/haproxy.cfg" "PIDFILE=/run/haproxy.pid"
EnvironmentFile=-/etc/sysconfig/haproxy
ExecStartPre=/usr/sbin/haproxy -f $CONFIG -f /etc/haproxy/conf.d -c -q
ExecStart=/usr/sbin/haproxy -Ws -f $CONFIG -f /etc/haproxy/conf.d -p $PIDFILE $OPTIONS
ExecReload=/usr/sbin/haproxy -f $CONFIG -c -q
ExecReload=/bin/kill -USR2 $MAINPID
KillMode=mixed
Type=notify
[Install]
WantedBy=multi-user.target
mkdir /etc/haproxy/conf.d
systemctl start haproxy
源码编译
1、安装依赖
yum install gcc gcc-c++ glibc glibc-devel pcre pcre-devel openssl openssl-devel systemd-devel net-tools vim iotop bc zip unzip zlib-devel lrzsz tree screen lsof tcpdump wget ntpdate
2、编译安装
tar xvf haproxy-1.8.20.tar.gz && cd haproxy-1.8.20
make ARCH=x86_64 TARGET=linux2628 USE_PCRE=1 USE_OPENSSL=1 USE_ZLIB=1 USE_SYSTEMD=1 USE_CPU_AFFINITY=1 PREFIX=/usr/local/haproxy
make install PREFIX=/usr/local/haproxy
cp haproxy /usr/sbin/
3、创建启动脚本
cat /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 /run/haproxy.pid
ExecReload=/bin/kill -USR2 $MAINPID
[Install]
WantedBy=multi-user.target
4、创建目录和用户
mkdir /etc/haproxy
useradd haproxy -s /sbin/nologin
mkdir /var/lib/haproxy
chown haproxy.haproxy /var/lib/haproxy/ -R
systemctl restart haproxy
#配置文件
cat /etc/haproxy/haproxy.cfg
global
maxconn 100000
chroot /usr/local/haproxy
#stats socket /var/lib/haproxy/haproxy.sock mode 600 level admin
uid 99
gid 99
daemon
nbproc 4
cpu-map 1 0
cpu-map 2 1
cpu-map 3 2
cpu-map 4 3
pidfile /run/haproxy.pid
log 127.0.0.1 local3 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:q1w2e3r4ys
listen web_port
bind 0.0.0.0:80
mode http
log global
server web1 127.0.0.1:8080 check inter 3000 fall 2 rise 5
#haproxy.cfg文件中定义了chroot、 pidfile、 user、 group等参数,如果系统没有相应的资源会导致haproxy无法启动,具体参考日志文件/var/log/messages
5、启动HAProxy
systemctl start haproxy
实现http模式下的客户端IP透传/后端服务器的状态监测机制等配置选项
#ha
vim /etc/haproxy/haproxy.cfg
global
log 127.0.0.1 local2
chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
maxconn 4000
user haproxy
group haproxy
daemon
#开启多线程
stats socket /var/lib/haproxy/haproxy.sock1 mode 600 level admin process 1
stats socket /var/lib/haproxy/haproxy.sock2 mode 600 level admin process 1
nbproc 2 #cpu核心数
cpu-map 1 0
cpu-map 2 1
defaults
mode http
log global
option httplog
option dontlognull
option http-server-close
option forwardfor except 127.0.0.0/8
option redispatch
option http-keep-alive
option abortonclose
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
vim /etc/haproxy/conf.d/test.cfg
listen webs
bind 0.0.0.0:80
balance roundrobin
server web1 192.168.37.27:80 check inter 3000 fall 3 rise 5
server web2 192.168.37.37:80 check inter 3000 fall 3 rise 5
server web3 127.0.0.1:8080 check inter 3000 fall 3 rise 5 backup #sorry服务器
systemctl start haproxy
yum install httpd -y
echo sorry > /var/www/html/index.html
#rs1
yum install httpd -y
echo rs1-17 > /var/www/html/index.html
systemctl start httpd
#rs2
yum install httpd -y
echo rs2-27 > /var/www/html/index.html
systemctl start httpd
#client
while :;do curl 192.168.37.7;sleep 0.5;done
基于cookie实现客户端会话保持
#ha
vim /etc/haproxy/conf.d/test.cfg
listen webs
balance roundrobin
bind 0.0.0.0:80
cookie SERVER-COOKIE insert indirect nocache
server web1 192.168.37.27:80 cookie test1 check inter 3000 fall 3 rise 5
server web2 192.168.37.37:80 cookie test2 check inter 3000 fall 3 rise 5
server web3 127.0.0.1:8080 check inter 3000 fall 3 rise 5 backup
systemctl restart haproxy
#client
curl --cookie "SERVER-COOKIE=test1" 192.168.37.7
四层负载和七层代理的区别
四层负载设备中,把client发送的报文,根据均衡设备设置的选择web服务器的规则选择对应的web服务器IP地址,这样client就可以直接跟此服务器建立TCP连接并发送数据。
七层负载均衡服务器起了一个代理服务器的作用,服务器建立一次TCP连接要三次握手,而client要访问webserver要先与七层负载设备进行三次握手后建立TCP连接,把要访问的报文信息发送给七层负载均衡;然后七层负载均衡再根据设置的均衡规则选择特定的webserver,然后通过三次握手与此台webserver建立TCP连接,然后webserver把需要的数据发送给七层负载均衡设备,负载均衡设备再把数据发送给client;所以,七层负载均衡设备起到了代理服务器的作用。
基于haproxy ACL实现域名匹配,把不同的域名请求调度到不同的后端服务器
#ha
vim /etc/haproxy/haproxy.cfg
default
errorfile 503 /var/www/html/503.html
vim /etc/haproxy/conf.d/test.cfg
listen webs
balance roundrobin
bind 0.0.0.0:80
acl web_net hdr_dom(host) -i www.magedu.net
acl web_org hdr_dom(host) -i www.magedu.org
use_backend net if web_net
use_backend org if web_org
default_backend def
backend net
server web1 192.168.37.27:80 check inter 3000 fall 3 rise 5
backend org
server web1 192.168.37.37:80 check inter 3000 fall 3 rise 5
backend def
server web1 127.0.0.1:8080 check inter 3000 fall 3 rise 5 backup
echo "It down" > /var/www/html/503.html
systemctl restart haproxy
#client
echo "192.168.37.7 www.magedu.net www.magedu.org www.magedu.xyz" >> /etc/hosts
curl www.magedu.net
curl www.magedu.org
curl www.magedu.xyz
基于HAProxy实现匹配请求URI文件后缀做动静分离
#ha
vim /etc/haproxy/conf.d/test.cfg
listen webs
balance roundrobin
bind :80
acl php_server path_end -i .php
acl image_server path_end -i .jpg .png .gif
use_backend php if php_server
use_backend image if image_server
default_backend def
backend php
server web1 192.168.37.27:80 check inter 3000 fall 3 rise 5
backend image
server web1 192.168.37.37:80 check inter 3000 fall 3 rise 5
backend def
server web1 127.0.0.1:8080 check inter 3000 fall 3 rise 5 backup
systemctl restart haproxy
#rs1
yum install php-fpm -y
vim /etc/httpd/conf.d/fcgi.conf
ProxyRequests Off
ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/var/www/html/$1
vim /var/www/html/info.php
<?php
phpinfo();
?>
systemctl restart httpd php-fpm
#rs2
cp /usr/share/pixmaps/faces/sky.jpg /var/www/html
#client
curl -I www.magedu.net/info.php
curl -I www.magedu.net/sky.jpg
实现https
cd /etc/pki/tls/certs/
vim Makefile
#/usr/bin/openssl genrsa -aes128 $(KEYLEN) > $@
/usr/bin/openssl genrsa $(KEYLEN) > $@
make magedu.net.crt
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:beijing
Locality Name (eg, city) [Default City]:beijing
Organization Name (eg, company) [Default Company Ltd]:magedu.net
Organizational Unit Name (eg, section) []:opt
Common Name (eg, your name or your server's hostname) []:www.magedu.net
Email Address []:
cat magedu.net.key magedu.net.crt > magedu.net.pem
cp magedu.net.pem /var/lib/haproxy/
vim /etc/haproxy/conf.d/test.cfg
listen web_80
balance roundrobin
bind 192.168.37.7:80
redirect scheme https if !{ ssl_fc }
server web1 192.168.37.27:80 check inter 2000 fall 3 rise 5
listen web_443
balance roundrobin
bind 192.168.37.7:443 ssl crt /var/lib/haproxy/magedu.net.pem
http-request set-header X-Forwarded-Port %[dst_port]
http-request add-header X-Forwared-Proto https if { ssl_fc }
server web1 192.168.37.27:80 check inter 2000 fall 3 rise 5
server web2 192.168.37.37:80 check inter 2000 fall 3 rise 5
systemctl restart haproxy
#client
curl -I www.magedu.net
curl -kL www.magedu.net
HAProxy基于socker文件多进程情况下服务器自动上下线
vim /etc/haproxy/conf.d/test.cfg
listen web_80
balance roundrobin
bind 192.168.37.7:80
server web1 192.168.37.27:80 check inter 2000 fall 3 rise 5
server web2 192.168.37.37:80 check inter 2000 fall 3 rise 5
listen web_443
balance roundrobin
bind 192.168.37.7:443 ssl crt /var/lib/haproxy/magedu.net.pem
http-request set-header X-Forwarded-Port %[dst_port]
http-request add-header X-Forwared-Proto https if { ssl_fc }
server web1 192.168.37.27:80 check inter 2000 fall 3 rise 5
server web2 192.168.37.37:80 check inter 2000 fall 3 rise 5
listen stats
bind :9999
balance roundrobin
stats enable #开启状态页
stats uri /status
stats auth haadmin:123456 #状态页用户密码
yum install socat -y
vim /etc/haproxy/haproxy.cfg
#开启多线程模式
stats socket /var/lib/haproxy/haproxy.sock1 mode 600 level admin process 1
nbproc 1
cpu-map 1 0
systemctl restart haproxy
echo "show info" | socat stdio /var/lib/haproxy/haproxy.sock1
echo "get weight web_80/web1" | socat stdio /var/lib/haproxy/haproxy.sock1 #查看权重
echo "disable server web_80/web1" | socat stdio /var/lib/haproxy/haproxy.sock1 #下线服务器
echo "enable server web_80/web1" | socat stdio /var/lib/haproxy/haproxy.sock1 #上线服务器
http://192.168.37.7:9999/status #状态页查看变化
网友评论