haproxy基础实验:
环境:
haproxy:172.25.254.100
web1: 172.25.254.10 均为nat网络
web2: 172.25.254.20
haproxy端配置:
[root@www ~]# yum install haproxy -y
[root@www ~]# vim /etc/haproxy/haproxy.cfg .........................................................................................
#配置在defauls模块下面
listen webcluster # 定义名为“webcluster”的监听
bind *:80 # 绑定到所有 IP 地址的 80 端口
mode http # 设置工作模式为 HTTP
balance roundrobin # 设置负载均衡算法为轮询(roundrobin)
server web1 172.25.254.10:80 # 定义后端服务器“web1”,其 IP 为 172.25.254.10,端口为 80
server web2 172.25.254.20:80 # 定义后端服务器“web2”,其 IP 为 172.25.254.20,端口为 80
.........................................................................................[root@www ~]# systemctl restart haproxy.service
[root@www ~]# curl 172.25.254.100
web1
[root@www ~]# curl 172.25.254.100
web2
web1端配置:
[root@www ~]# yum install nginx -y
[root@www ~]# echo web1 > /usr/share/nginx/html/index.html
[root@www ~]# systemctl restart nginx
[root@www ~]# curl 172.25.254.10
web1
web2端配置:
[root@www ~]# yum install nginx -y
[root@www ~]# echo web1 > /usr/share/nginx/html/index.html
[root@www ~]# systemctl restart nginx
[root@www ~]# curl 172.25.254.20
web2
haproxy的server详细配置:
.............................................................................
server web1 172.25.254.10:80 check inter 2 fall 3 rise 5 weight 2 disabled
server web2 172.25.254.20:80 check inter 2 fall 3 rise 5 weight 1 disabled
server web_sorry 172.25.254.100:8080 backupcheck:启用对该服务器的健康检查。
inter 2:表示健康检查的间隔时间为 2 秒。
fall 3:如果连续 3 次健康检查失败,就认为该服务器不可用。
rise 5:如果服务器不可用,当连续 5 次健康检查成功时,将其重新标记为可用。
weight 2:为该服务器设置权重为 2,通常在负载均衡算法中用于决定分配请求的比例。
disabled:不可用
backup:备份使用,当所有主服务器挂机后使用
.............................................................................#测试:
#在172.25.254端为backup添加配置
[root@www ~]# yum install httpd -y
[root@www ~]# echo backup > /var/www/html/index.html
[root@www ~]# vim /etc/httpd/conf/httpd.conf
Listen 8080
[root@www ~]# systemctl restart httpd
[root@www ~]# systemctl restart haproxy.service
[root@www ~]# curl 172.25.254.100
backup
haproxy的多进程设置:
global
..........................................
stats socket /var/lib/haproxy/stats
nbproc 2 # 设置工作进程数量为 2
cpu-map 1 0 # 将第一个工作进程绑定到 CPU 0
cpu-map 2 1 # 将第二个工作进程绑定到 CPU 1..........................................#查看添加的多进程
[root@www ~]# systemctl restart haproxy.service
[root@www ~]# pstree -p | grep haproxy|-haproxy(33720)-+-haproxy(33722)| `-haproxy(33723)
haproxy的socat工具的使用
socat工具可以动态地对我们的服务器的状态信息以及权重进行调整,而不用进入配置文件直接修改文件信息,当使用该工具时,第一次按照原来的算法执行,后面按照socat调整的值为准
[root@www ~]# vim /etc/haproxy/haproxy.cfg
stats socket /var/lib/haproxy/stats mode 600 level admin #socket提权#将web1的权重值修改为5
[root@www ~]# echo "set weight webcluster/web1 5 " | socat stdio /var/lib/haproxy/stats #查看web1的权重值
[root@www ~]# echo "get weight webcluster/web1 " | socat stdio /var/lib/haproxy/stats
5 (initial 2)#停掉web1
[root@www ~]# echo "disable server webcluster/web1 " | socat stdio /var/lib/haproxy/stats #启用服务器
[root@www ~]# echo "enable server webcluster/web1 " | socat stdio /var/lib/haproxy/stats #socat对多进程的处理,通过添加stats文件的方式实现
[root@www ~]# vim /etc/haproxy/haproxy.cfg
........................................................................stats socket /var/lib/haproxy/stats1 mode 600 level admin process 1stats socket /var/lib/haproxy/stats2 mode 600 level admin process 2nbproc 2cpu-map 1 0cpu-map 2 1
........................................................................#测试#将web1的第一个进程的weight值设置为2
[root@www ~]# echo "set weight webcluster/web1 2 " | socat stdio /var/lib/haproxy/stats1 [root@www ~]# echo "get weight webcluster/web1 " | socat stdio /var/lib/haproxy/stats1
2 (initial 2)#将web2的第二个进程的weight值设置为5
[root@www ~]# echo "set weight webcluster/web1 5 " | socat stdio /var/lib/haproxy/stats2[root@www ~]# echo "get weight webcluster/web1 " | socat stdio /var/lib/haproxy/stats2
5 (initial 2)
haproxy的算法
静态算法:
static-rr:
基于权重的轮询调度,不支持运行时利用socat进行权重的动态调整(只支持0和1,不支持其它值,只能改为上线或者下线)及后端服务器慢启动,其后端主机数量没有限制,相当于LVS中的 wrr
first:
根据服务器在列表中的位置,自上而下进行调度,但是其只会当第一台服务器的连接数达到上限,新请求才会分配给下一台服务,因此会忽略服务器的权重设置,此方式使用较少不支持用socat进行动态修改权重,可以设置0和1,可以设置其它值但无效
动态算法:
roundrobin:
基于权重的轮询动态调度算法,支持权重的运行时调整,不同于lvs中的rr轮训模式,HAProxy中的roundrobin支持慢启动,其每个后端backend中最多支持4095个real server,支持对real server权重动态调整,roundrobin为默认调度算法,此算法使用广泛 ,静态算法后端real server个数不限。
leastconn:
leastconn加权的最少连接的动态,支持权重的运行时调整和慢启动(相当于lvs的wlc),即:根据当前连接最少的后端服务器而非权重进行优先调度(新客户端连接),比较适合长连接的场景使用,比如:MySQL等场景。
haproxy状态页
[root@www ~]# vim /etc/haproxy/haproxy.cfg ...............................................................................
listen stats # 定义监听模式为 HTTPmode http # 明确为 HTTP 模式bind *:9999 # 绑定到所有接口的 9999 端口stats enable # 启用统计功能stats refresh 3 # 设置统计信息刷新的时间间隔为 3 秒stats uri /status # 设置统计信息的 URI 为 /statusstats auth lee:lee # 设置统计信息的认证用户名为 lee,密码为 lee
................................................................................[root@www ~]# restart haproxy.service
#测试添加的状态页
浏览器输入:172.254.25.100:9999/status
haproxy的cookie的会话保持
[root@www ~]# vim /etc/haproxy/haproxy.cfg .........................................................................................
listen webclusterbind *:80mode httpbalance roundrobincookie WEBCOOKIE insert nocache indirect # 设置名为 WEBCOOKIE 的 Cookie,以插入方 式添加,不缓存,间接模式server web1 172.25.254.10:80 cookie lee1 check inter 2 fall 3 rise 5 weight 2server web2 172.25.254.20:80 cookie lee2 check inter 2 fall 3 rise 5 weight 1
.........................................................................................#测试
[root@www ~]# curl -i 172.25.254.100
HTTP/1.1 200 OK
server: nginx/1.20.1
date: Sun, 11 Aug 2024 12:17:17 GMT
content-type: text/html
content-length: 5
last-modified: Sun, 11 Aug 2024 08:32:48 GMT
etag: "66b87730-5"
accept-ranges: bytes
set-cookie: WEBCOOKIE=lee1; path=/
cache-control: private
web1
haproxy的ACL
#基于域名匹配的acl
frontend webclusterbind *:80mode httpacl test hdr_dom(host) -i www.timinglee.org #基于域名匹配的acluse_backend webcluster-host if testdefault_backend default-hostbackend webcluster-hostmode httpserver web1 172.25.254.10:80 check inter 2 fall 3 rise 5backend default-hostmode httpserver web2 172.25.254.20:80 check inter 2 fall 3 rise 5#测试
[root@www ~]# curl www.timinglee.org
web1
#host
字段以 .org
结尾
frontend webclusterbind *:80mode httpacl test hdr_end(host) -i .orguse_backend webcluster-host if testdefault_backend default-hostbackend webcluster-hostmode httpserver web1 172.25.254.10:80 check inter 2 fall 3 rise 5backend default-hostmode httpserver web2 172.25.254.20:80 check inter 2 fall 3 rise 5#测试
[root@www ~]# curl www.timinglee.org
web1
#域名中包含lee子串的匹配
frontend webclusterbind *:80mode httpacl test base_sub -m sub leeuse_backend webcluster-host if testdefault_backend default-hostbackend webcluster-hostmode httpserver web1 172.25.254.10:80 check inter 2 fall 3 rise 5backend default-hostmode httpserver web2 172.25.254.20:80 check inter 2 fall 3 rise 5#测试
[root@www ~]# curl www.timinglee.lee
web1
#路径部分是否包含子字符串lee
frontend webclusterbind *:80mode httpacl test path_sub -m sub leeuse_backend webcluster-host if testdefault_backend default-hostbackend webcluster-hostmode httpserver web1 172.25.254.10:80 check inter 2 fall 3 rise 5backend default-hostmode httpserver web2 172.25.254.20:80 check inter 2 fall 3 rise 5#测试
[root@www ~]# curl www.timinglee.lee
web2
#匹配源ip是否包含指定的ip
frontend webclusterbind *:80mode httpacl test src 172.25.254.100 192.168.0.0/24use_backend webcluster-host if testdefault_backend default-hostbackend webcluster-hostmode httpserver web1 172.25.254.10:80 check inter 2 fall 3 rise 5backend default-hostmode httpserver web2 172.25.254.20:80 check inter 2 fall 3 rise 5#测试
[root@www ~]# curl 172.25.254.100
web1
#拒绝指定的源ip
frontend webclusterbind *:80mode httpacl test src 172.25.254.100 192.168.0.0/24#use_backend webcluster-host if testhttp-request deny if testdefault_backend default-hostbackend webcluster-hostmode httpserver web1 172.25.254.10:80 check inter 2 fall 3 rise 5backend default-hostmode httpserver web2 172.25.254.20:80 check inter 2 fall 3 rise 5#测试
[root@www ~]# curl 172.25.254.100
<html><body><h1>403 Forbidden</h1>
Request forbidden by administrative rules.
</body></html>
#匹配浏览器类型
frontend webclusterbind *:80mode httpacl badwebrowers hdr_sub(User-Agent) -i curl wget#use_backend webcluster-host if testhttp-request deny if badwebrowersdefault_backend default-hostbackend webcluster-hostmode httpserver web1 172.25.254.10:80 check inter 2 fall 3 rise 5backend default-hostmode httpserver web2 172.25.254.20:80 check inter 2 fall 3 rise 5[root@www ~]# curl 172.25.254.100
<html><body><h1>403 Forbidden</h1>
Request forbidden by administrative rules.
</body></html>
#acl基于文件后缀名分离实现动静分离
frontend webclusterbind *:80mode httpacl static path_end -i .jpg .png .css .js .htmlacl php path_end -i .phpuse_backend webcluster-host if phpdefault_backend default-hostbackend webcluster-hostmode httpserver web1 172.25.254.10:80 check inter 2 fall 3 rise 5backend default-hostmode httpserver web2 172.25.254.20:80 check inter 2 fall 3 rise 5#web1端更改配置
[root@www ~]# systemctl stop nginx.service
[root@www ~]# yum install httpd -y
[root@www ~]# yum install php-fpm -y
[root@www ~]# vim /var/www/html/index.php
<?phpphpinfo();
?>
#测试:
#acl基于文件路径分离实现动静分离
frontend webclusterbind *:80mode httpacl url_static path_end -m sub /static /images /htmlacl url_app path_end -m sub /apiuse_backend webcluster-host if url_appdefault_backend default-hostbackend webcluster-hostmode httpserver web1 172.25.254.10:80 check inter 2 fall 3 rise 5backend default-hostmode httpserver web2 172.25.254.20:80 check inter 2 fall 3 rise 5#更改web1端配置
[root@www ~]# mkdir /var/www/html/api
[root@www ~]# echo api > /var/www/html/api/index.html
[root@www ~]# systemctl restart httpd
#测试
haproxy自定义错误页面
[root@www ~]# mkdir /etc/haproxy/error/ -p #创建存放错误页面的目录
[root@www ~]# cp /usr/share/haproxy/503.http /etc/haproxy/error/
[root@www ~]# vim /etc/haproxy/error/503.http
<html>
<meta http-equiv=content-type content=text/html;charset=utf-8> #加入utf8<body><h1>I am hero <h1>yulang come
</body></html>defaults..............................................................timeout check 10smaxconn 3000errorfile 503 /etc/haproxy/error/503.http #错误页面加入#停掉服务测试
[root@www ~]# systemctl stop nginx.service
[root@www ~]# systemctl stop httpd.service
#测试:
haproxy基于mysql的四层负载
[root@www ~]# vim /etc/haproxy/haproxy.cfg
listen mysqlbind :3306mode tcpbalance leastconnserver db1 172.25.254.10:3306 checkserver db2 172.25.254.20:3306 check#后端服务器配置
[root@www mysql]# yum install mariadb-server -y
[root@www ~]# vim /etc/my.cnf.d/mysql-server.cnf
[mysqld]
server-id=1/2[root@www ~]# systemctl restart mariadb.service
[root@www ~]# mysql -e "grant all on *.* to lee@'%' identified by 'lee';"#测试
[root@www mysql]# mysql -ulee -plee -h 172.25.254.100 -e "select @@server_id"
+-------------+
| @@server_id |
+-------------+
| 1 |
+-------------+
haproxy的https实现
#制作证书
[root@www ~]# mkdir /etc/haproxy/certs
[root@www ~]# openssl req -newkey rsa:2048 -nodes -sha256 -keyout /etc/haproxy/certs/timinglee.org.key -x509 -days -out /etc/haproxy/certs/timinglee.org.crt#更改haproxy配置文件
[root@www ~]# vim /etc/haproxy/haproxy.cfg
frontend webserverbind *:80redirect scheme https if !{ ssl_fc } # 全站加密mode httpuse_backend webclusterfrontend webserver-httpsbind *:443 ssl crt /etc/haproxy/certs/timinglee.org.pemmode httpuse_backend webclusterbackend webclustermode httpbalance roundrobinserver web1 172.25.254.10:80 check inter 3s fall 3 rise 5server web2 172.25.254.20:80 check inter 3s fall 3 rise 5
#测试