Haproxy

embedded/2024/9/25 15:20:50/

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

#测试


http://www.ppmy.cn/embedded/94247.html

相关文章

c++信号函数

信号处理函数 信号处理函数是处理特定信号&#xff08;如中断信号 SIGINT 或终止信号 SIGTERM&#xff09;的函数。典型的信号处理函数具有以下签名&#xff1a; #include <csignal>void signal_handler(int signal);注册信号处理函数的方式通常如下&#xff1a; #inc…

Linux Vim教程(十五):使用Vimscript进行脚本编写

目录 1. Vimscript简介 2. 基本语法和结构 2.1 变量 2.2 条件语句 2.3 循环语句 2.4 函数 3. 操作缓冲区、窗口和标签页 3.1 缓冲区 3.2 窗口 3.3 标签页 4. 自动化编辑任务 4.1 自动命令 4.2 键映射 5. 编写和调试Vimscript脚本 5.1 编写脚本 5.2 调试脚本 6…

中间件是一种在客户端和服务器之间进行通信和处理的软件组件或服务

中间件是一种在客户端和服务器之间进行通信和处理的软件组件或服务。中间件位于应用程序和操作系统之间&#xff0c;可以提供一些功能&#xff0c;如请求转发、数据转换、安全性和身份验证、日志记录等。 中间件的主要作用是将应用程序与底层基础设施解耦&#xff0c;提供了一…

jenkins 安装以及自动构建maven项目并且运行

在这里找到你对应jdk的版本的jenkins包 War Jenkins Packages 我这里用的使java8,所以下载 https://mirrors.jenkins.io/war-stable/2.60.1/jenkins.war 然后jenkins可以安装到centos系统 在本地windows系统运行命令行 scp C:\Users\98090\Downloads\jenkins.war root@192…

PwnLab: init-文件包含、shell反弹、提权--靶机渗透思路讲解

Vulnhub靶机链接回【PwnLab】 首页有一个登录框 image-20240807124822770 他没有验证码&#xff0c;我们试试暴力破解 image-20240807122743025 开始爆破了&#xff0c;全部失败&#xff0c;哈哈哈 image-20240807122851001 nmap全端口扫描试试 image-20240807131408315 有…

【黑马】MyBatis

目录 MyBatis简介JDBC缺点&#xff1a;MyBatis针对于JDBC进行简化&#xff0c;简化思路&#xff1a; MyBatis快速入门具体构建步骤解决SQL映射文件的警告提示 Mapper代理开发案例&#xff1a;使用Mapper代理方式完成案例具体步骤详解&#xff1a;Mapper代理方式 Mapper核心配置…

Linux5.15.71编译问题处理

目录 1 编译环境及源码版本2 移植Linux 5.15.71遇到问题2.1 imx-sdma 20ec000.dma-controller: Direct firmware load for imx/sdma/sdma-imx6q.bin failed with error -22.2 cfg80211: failed to load regulatory.db 1 编译环境及源码版本 ​ 1. uboot-alientek-v2022.04 ​…

halcon计算解析度

最近因为项目需求&#xff0c;研究了一个小工具&#xff0c;自动计算解析度。 read_image (Image, D:/TT/Desktop/halcon/计算解析度/擷取.PNG) InputTuple:[179, 77, 410, 513,3] CalculateResolution (Image, OutputImage, InputTuple, OutputTuple)新建函数&#xff0c;计算…