【云原生】听说大家跟着学haproxy,都成大佬了(实验篇)

news/2024/9/17 19:03:26/ 标签: 服务器, 运维

PS:想了解haproxy理论知识,请移步haproxy理论篇

一、实验环境

主机名角色IP地址
haproxy172.25.254.100
web1RS1172.25.254.10
web2RS2172.25.254.20
client客户机172.25.254.254

二、haproxy的基本部署

1、安装nginx服务(web1、web2)

#安装nginx服务
dnf install -y nginx#开机自启
systemctl enable --now nginx

 2、网页文件(web1、web2)

#在web1中
echo web1 > /usr/share/nginx/html/index.html#在web2中
echo web2 > /usr/share/nginx/html/index.html

3、安装haproxy服务(haproxy)

#下载haproxy
dnf install -y haproxy#开机自启
systemctl enable --now haproxy

三、haproxy的全局配置

1、编辑配置文件(haproxy)

第一种:

vim /etc/haproxy/haproxy.cfgfrontend webclusterbind *:80mode httpuse_backend webcluster-hostbackend webcluster-hostbalance roundrobinserver web1 172.25.254.10:80server web2 172.25.254.20:80

 第二种:(就是将第一种整合起来)

vim /etc/haproxy/haproxy.cfglisten webclusterbind *:80mode httpbalance roundrobinserver web1 172.25.254.10:80server web2 172.25.254.20:80

2、重启服务 (haproxy)

systemctl restart haproxy

 3、测试(client)

curl 172.25.254.100

四、haproxy代理参数

1、关闭 RS1 和 RS2 的 nginx ,网页页面跳转 haproxy 的页面

1.1 apache服务(haproxy)

#安装
dnf install -y httpd#修改端口号
vim /etc/httpd/conf/httpd.conf
listen 8080#开机自启
systemctl enable --now httpd#网页内容
echo fail > /var/www/html/index.html

 1.2 配置文件(haproxy)

#编辑配置文件
vim /etc/haproxy/haproxy.cfglisten webclusterbind *:80mode httpbalance roundrobin#inter:健康状态次数 fall:失效次数 rise:有效次数 weight:权重        server web1 172.25.254.10:80 check inter 2 fall 3 rise 5 weight 1server web2 172.25.254.20:80 check inter 2 fall 3 rise 5 weight 1server web_sorry 172.25.254.100:8080 backup#重启服务
systemctl restart haproxy

1.3 停止nginx(web1、web2)

systemctl stop nginx

1.4 测试(client)

curl 172.25.254.100

结果:显示haproxy的页面 fail

2、关闭 RS1 

2.1 配置文件(haproxy)

#编辑配置文件
vim /etc/haproxy/haproxy.cfglisten webclusterbind *:80mode httpbalance roundrobin#inter:健康状态次数 fall:失效次数 rise:有效次数 weight:权重        server web1 172.25.254.10:80 check inter 2 fall 3 rise 5 weight 1 disabledserver web2 172.25.254.20:80 check inter 2 fall 3 rise 5 weight 1server web_sorry 172.25.254.100:8080 backup#重启服务
systemctl restart haproxy

2.2 测试 

curl 172.25.254.100

结果 :只显示web2的页面

3、网页重定向(百度为例)

3.1 配置文件(haproxy)

#编辑配置文件
vim /etc/haproxy/haproxy.cfglisten webclusterbind *:80mode httpbalance roundrobinredirect prefix http://www.baidu.com#重启服务
systemctl restart haproxy

3.2 测试

curl 172.25.254.100

 结果:看到百度页面

五、haproxy热处理

1、单线程

1.1 配置文件(haproxy)

#编辑配置文件
vim /etc/haproxy/haproxy.cfg#加上admin变为超级用户
stats socket /var/lib/haproxy/stats mode 600 level adminlisten webclusterbind *:80mode httpbalance roundrobin#inter:健康状态次数 fall:失效次数 rise:有效次数 weight:权重        server web1 172.25.254.10:80 check inter 2 fall 3 rise 5 weight 1server web2 172.25.254.20:80 check inter 2 fall 3 rise 5 weight 1server web_sorry 172.25.254.100:8080 backup#重启服务
systemctl restart haproxy

1.2 安装socat工具(haproxy)

dnf install socat -y

1.3 热处理(haproxy)

echo get weight server webcluster/web1 | socat stdio /var/lib/haproxy/statsecho "set weight server webcluster/web1 2" | socat stdio /var/lib/haproxy/statsecho "set weight server webcluster/web1 1" | socat stdio /var/lib/haproxy/statsecho 'get server stats' | socat stdio /var/lib/haproxy/statsecho 'show server stats' | socat stdio /var/lib/haproxy/statsecho "disenable server webcluster/web1" | socat stdio /var/lib/haproxy/statsecho "enable server webcluster/web1" | socat stdio /var/lib/haproxy/stats

 2、多线程

2.1 配置文件(haproxy)

#编辑配置文件
vim /etc/haproxy/haproxy.cfgstats socket /var/lib/haproxy/haproxy.sock1 mode 600 level admin process 1
stats socket /var/lib/haproxy/haproxy.sock2 mode 600 level admin process 2nbproc 2
cpu-map 10
cpu-map 2 1

2.2 查看

ll /var/lib/haproxy

六、haproxy的算法

1、静态算法

1.1 static-rr

listen webclusterbind *:80mode httpbalance static-rrserver web1 172.25.254.10:80 check inter 2 fall 3 rise 5 weight 1server web2 172.25.254.20:80 check inter 2 fall 3 rise 5 weight 1

1.2 first

listen webclusterbind *:80mode httpbalance firstserver web1 172.25.254.10:80 maxconn 3 check inter 3s fal1 3 rise 5server web2 172.25.254.20:80 check inter 3s fal1 3 rise 5

2、动态算法

2.1 roundrobin

listen webclusterbind *:80mode httpbalance roundrobinserver web1 172.25.254.10:80 check inter 2 fall 3 rise 5 weight 1server web2 172.25.254.20:80 check inter 2 fall 3 rise 5 weight 1

 2.2 leastconn

listen webclusterbind *:80mode httpbalance leastconnserver web1 172.25.254.10:80 check inter 2 fall 3 rise 5 weight 1server web2 172.25.254.20:80 check inter 2 fall 3 rise 5 weight 1

 3、其他算法

3.1 source

listen webclusterbind *:80mode httpbalance sourceserver web1 172.25.254.10:80 weight 1 check inter 3s fal1 3 rise 5server web2 172.25.254.20:80 weight 1 check inter 3s fall 3 rise 5
 3.1.1 map-base取模法
listen webclusterbind *:80mode httpbalance sourceserver web1 172.25.254.10:80 weight 1 check inter 3s fall 3 rise 5server web2 172.25.254.20:80 weight 1 check inter 3s fall 3 rise 5
#不支持动态调整权重值
echo "set weight webcluster/web1 2" socat stdio/var/lib/haproxy/haproxy.sock
Backend is using a static LB algorithm and only accepts weights '0%' and '100%'#只能动态上线和下线
echo "set weight webcluster/web1 0" socat stdio /var/lib/haproxy/stats
echo "get weight webcluster/web1" socat stdio /var/lib/haproxy/stats
0(initial 1)
 3.1.2 一致性hash
listen webclusterbind *:80mode httpbalance sourcehash-type consistentserver web1 172.25.254.10:80 weight 1 check inter 3s fal1 3 rise 5server web2 172.25.254.20:80 weight 1 check inter 3s fall 3 rise 5

 3.2 uri

3.2.1 uri 取模法配置示例
listen webclusterbind *:80mode httpbalance uriserver web1 172.25.254.10:80 weight 1 check inter 3s fal1 3 rise 5server web2 172.25.254.20:80 weight 1 check inter 3s fall 3 rise 5
 3.2.2 uri -致性hash配置示例
listen webclustebind *:80mode httpbalance urihash-type consistentserver web1 172.25.254.10:80 weight 1 check inter 3s fall 3 rise 5server web2 172.25.254.20:80 weight 1 check inter 3s fall 3 rise 5
3.2.3 访问测试
#在web1中
echo web1 11 > /usr/share/nginx/html/index1.html
echo web1 22 > /usr/share/nginx/html/index2.html
echo web1 33 > /usr/share/nginx/html/index3.html#在客户机中
curl 172.25.254.100/index1.html
curl 172.25.254.100/index2.html
curl 172.25.254.100/index3.html

3.3 ur]_param

3.3.1 url_param取模法配置示例
listen webclusterbind *:80mode httpbalance urlparam name,userid #支持对多个ur]_param hashserver web1 172.25.254.10:80 weight 1 check inter 3s fall 3 rise 5server web2 172.25.254.20:80 weight 1 check inter 3s fall 3 rise 5
3.3.2 url_param一致性hash配置示例
listen webclusterbind *:80mode httpbalance urlparam name,userid #支持对多个ur]_param hashhash-type consistentserver web1 172.25.254.10:80 weight 1 check inter 3s fall 3 rise 5server web2 172.25.254.20:80 weight 1 check inter 3s fall 3 rise 5
 3.3.3 访问测试
#在客户机中
curl 172.25.254.100/index1.html?userid=111
curl 172.25.254.100/index2.html?userid=111
curl 172.25.254.100/index3.html?userid=111

3.4 hdr

3.4.1 hdr取模法配置示例
listen webclusterbind *:80mode httpbalance hdr(user-Agent)server web1 172.25.254.10:80 weight 1 check inter 3s fall 3 rise 5server web2 172.25.254.20:80 weight 1 check inter 3s fall 3 rise 5
3.4.2 hdr一致性hash配置示例
listen webclusterbind *:80mode httpbalance hdr(user-Agent)hash-type consistentserver web1 172.25.254.10:80 weight 1 check inter 3s fall 3 rise 5server web2 172.25.254.20:80 weight 1 check inter 3s fall 3 rise 5
3.4.3 访问测试
curl -v 172.25.254.100
curl -v "baidu" 172.25.254.100

七、基于cookie的会话保持

1、配置文件(haproxy)

#编辑配置文件
vim /etc/haproxy/haproxy.cfglisten webclusterbind *:80mode httpbalance roundrobincookie WEBCOOKIE insert nocache indirectserver web1 172.25.254.10:80 cookie moon1 check inter 2 fall 3 rise 5 weight 1server web2 172.25.254.20:80 cookie moon2 check inter 2 fall 3 rise 5 weight 1#重启服务
systemctl restart haproxy.service

2、测试

curl -b WEBCOOKIE=moon1 172.25.254.100
curl -b WEBCOOKIE=moon2 172.25.254.100

八、ip透传

#web1
#卸载nginx
rpm -e nginx#下载apache
dnf install -y httpd#开机自启
systemctl enable --now httpd

1、四层

listen webclusterbind *:80mode tcpbalance roundrobinserver web1 172.25.254.20:80 check inter 2 fall 3 rise 5 weight 1

2、七层

listen webclusteroption forwardforbind *:80mode tcpbalance roundrobinserver web1 172.25.254.10:80 check inter 2 fall 3 rise 5 weight 1server web1 172.25.254.10:80 send-proxy check inter 2 fall 3 rise 5 weight 1

九、自定义错误页面

vim /etc/haproxy/haproxy.cfg
errorfie 503 haproxy/errorpages/503page.httpmkdir /haproxy/errorpages/ -pcp usr/share/haproxy/503.http/haproxy/errorpages/503page.httpvim /haproxy/errorpages/503page.http
HTTP/1.0 503 Service Unavailable
Cache-Control:no-cache
Connection:close
Content-Type:text/html;charset=UTF-8
<htm]><body><h1>什么动物生气最安静</h1>大猩猩!!
</body></htm1>

十、四层负载示例

vim /etc/haproxy/haproxy.cfg
frontend mysql_portbind :3306mode tcpuse_backend mysql_rslisten mysql_portbind :3306mode tcpbalance leastconnserver mysql1 172.25.254.10:3306 checkserver mysql2 172.25.254.20:3306 check#RS1和RS2下载数据库
dnf install mariadb-server -y
dnf install mariadb-server -y#RS1
vim /etc/my.cnf
server-id=1
mysql -e "grant all on *.* to lee'%' identified by 'lee';"#RS2
vim /etc/my.cnf
server-id=2mysql -e "grant all on *.* to lee'%' identified by 'lee';"

十一、haproxy的https 

#证书制作
mkdir /etc/haproxy/certs/
opensslreg -newkey rsa:2048 -nodes -sha256 -keyout /etc/haproxy/certs/timinglee.org.key -x509 -days 365 out /etc/haproxy/certs/timinglee.org.crtvim /etc/haproxy/haproxy.cfg
frontend webserverbind *:80redirect scheme https if !{ ssl_fc }mode httpuse backend webcluster
frontend webserver-httpsbind *:443 ssl crt /etc/haproxy/timinglee.org.pemmode httpuse backend webcluster
backend webclustermode httpbalance roundrobinserver webl 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/news/1507312.html

相关文章

PHP MySQL 读取数据

PHP MySQL 读取数据 PHP和MySQL是Web开发中的经典组合,广泛用于创建动态网站和应用程序。在PHP中读取MySQL数据库中的数据是一项基本技能,涉及到连接数据库、执行查询以及处理结果集。本文将详细介绍如何使用PHP从MySQL数据库中读取数据。 1. 环境准备 在开始之前,请确保…

虚拟机centos9搭建wordpress

目录 安装环境和搭建简介 1. 更换yum源更新系统软件包&#xff1a; 1.1备份yum源 1.1.1创建备份目录&#xff1a; 1.1.2移动现有仓库配置文件到备份目录&#xff1a; 1.1.3验证备份&#xff1a; 1.2更换yum源 1.2.1添加yum源 1.2.2删除和建立yum缓存 1.3更新系统软件…

《深入浅出WPF》学习笔记七.使用Prism实现点单系统

《深入浅出WPF》学习笔记七.使用Prism实现Mvvm点单系统 背景 深入浅出Wpf系列视频的最后一个demo,使用Prism、Mvvm实现点单系统。demo并不复杂&#xff0c;但是涉及的面广&#xff0c;方便更好的理解wpf。代码在下面自取。后续会把git地址补充上来。 代码 项目层级 command …

Multisim 用LM358 运放模拟线性稳压器 - 运放输出饱和 - 前馈电容

就是拿运放搭一个可调的LDO 稳压器&#xff0c;类似下面这个功能框图里的感觉。本来应该非常简单&#xff0c;没什么好说的&#xff0c;没想到遇到了两个问题。 原理 - 理想运放 我用PNP 三极管Q2 作为输出&#xff0c;运放输出电压升高时&#xff0c;流过PNP 三极管BE 的电流变…

云服务器部署Java+Vue前后端分离项目

1、申请一个云服务器 选择云服务器&#xff1a;阿里云、腾讯云、百度云、京东云、华为云等等&#xff0c;我使用的是阿里云服务器。 2、远程链接服务器 使用FinalShell工具或者其他远程工具&#xff0c;使用SSH链接&#xff0c;主机地址要填写阿里云服务的公网ip&#xff0c;如…

Redis的String类型常用命令总结

1. set 设置一个键的值。 set key value示例&#xff1a; set username "alice"2. get 获取一个键的值。 get key示例&#xff1a; get username3. getset 设置键的值&#xff0c;并返回键的旧值。 getset key value示例&#xff1a; getset username "…

for(char c:s),std::vector<int> numbers 和std::int numbers[],.size()和.sizeof()区别

在C中当需要对某个容器或数组进行遍历时我们可以使用以下语句&#xff0c;c将会被赋值为s中的元素 for(char c:s)://s可以是任何满足条件的容器或数组for(int c:s):for(double c:s):for(float c:s):在C中我们来区分std::vector numbers {1, 2, 3, 4, 5};和std::int numbers[] …

常见8种数据结构

常见的数据结构包括数组、链表、队列、栈、树、堆、哈希表和图&#xff0c;每种数据结构都有其特点&#xff0c;如下&#xff1a; 常见数据结构 1.数组2.链表3.队列4.栈5.树6.图7.哈希表8.堆 1.数组 特点&#xff1a; 固定大小的线性数据结构支持快速随机访问插入和删除效率…

徐州BGP机房与普通机房的区别有哪些?

BGP也被称为是边界网关协议&#xff0c;是运行在TCP上的一种自治系统的路由协议&#xff0c;能够用来处理因特网大小的网络协议&#xff0c;同时也是能够处理好不相关路由域之间的多路连接的协议&#xff0c;今天小编主要来聊一聊徐州BGP机房与普通机房之间的区别有哪些&#x…

5分钟上手亚马逊云科技AWS核心云开发/云架构知识 - 维护EC2服务器

简介&#xff1a; 小李哥从今天开始将开启全新亚马逊云科技AWS云计算知识学习系列&#xff0c;适用于任何无云计算或者亚马逊云科技技术背景的开发者&#xff0c;让大家0基础5分钟通过这篇文章就能完全学会亚马逊云科技一个经典的服务开发架构。 我将每天介绍一个基于亚马逊云…

【xilinx】Vitis 2021.1 安装在 Ubuntu 20.04 上挂起

描述 受影响的设备和配置&#xff1a; 所有 I/Q/M/E 温度等级的 Versal GTM。当前或未来的 38 Gb/s 及以上的 PAM4 配置。所有线路速率低于 38 Gb/s 或 NRZ 的 PAM4 配置不受影响。 当 GTM 收发器当前未使用但已配置时&#xff0c;在某些操作条件下&#xff08;参见表 1&#x…

C++之类与对象(完结撒花篇)

目录 前言 1.再探构造函数 2.类型转换 3.static成员 4. 友元 5.内部类 6.匿名对象 7.对象拷贝时的编译器优化 结束语 前言 在前面的博客中&#xff0c;我们对类的默认成员函数都有了一定了解&#xff0c;同时实现了一个日期类对所学的没内容进行扩展延伸&#xff0c;本…

【C++ 面试 - 基础题】每日 3 题(十一)

✍个人博客&#xff1a;Pandaconda-CSDN博客 &#x1f4e3;专栏地址&#xff1a;http://t.csdnimg.cn/fYaBd &#x1f4da;专栏简介&#xff1a;在这个专栏中&#xff0c;我将会分享 C 面试中常见的面试题给大家~ ❤️如果有收获的话&#xff0c;欢迎点赞&#x1f44d;收藏&…

计算机网络TCP/UDP知识点

这是一些在学习过程中关于计算机网络八股文的一些知识点记录&#xff1a; TCP/UDP TCP怎么保证可靠性 1.序列号&#xff0c;确认应答&#xff0c;超时重传 数据到达接收方&#xff0c;接收方需要发出一个确认应答&#xff0c;表示已经收到该数据段&#xff0c;并且确认序号…

【HarmonyOS NEXT星河版开发学习】小型测试案例06-小红书卡片

个人主页→VON 收录专栏→鸿蒙开发小型案例总结​​​​​ 基础语法部分会发布于github 和 gitee上面&#xff08;暂未发布&#xff09; 前言 在鸿蒙&#xff08;HarmonyOS&#xff09;开发中&#xff0c;自适应伸缩是指应用程序能够根据不同设备的屏幕尺寸、分辨率和形态&…

Leetcode每日刷题之 11. 盛最多水的容器(C++)

1. 题目解析 根据题目我们知道本题我们需要由给出的数组找出所有容器中盛水最多的一个&#xff0c;即核心就是先求出所有容器后遍历找出最大的即可 2. 算法原理 本题使用到的算法是双指针&#xff0c;在使用暴力解法遍历所有容器的时候会出现超时的问题&#xff0c;而是用双指针…

【机器学习数据预处理】特征工程

【作者主页】Francek Chen 【专栏介绍】 ⌈ ⌈ ⌈Python机器学习 ⌋ ⌋ ⌋ 机器学习是一门人工智能的分支学科&#xff0c;通过算法和模型让计算机从数据中学习&#xff0c;进行模型训练和优化&#xff0c;做出预测、分类和决策支持。Python成为机器学习的首选语言&#xff0c;…

mysql本地3306通过nginx映射到外网

mysql本地3306通过nginx映射到外网 安装nginx, 版本大于1.19x yum install nginx 安装stream-module模块 查看yum源&#xff0c;查找有没有 nginx-mod-stream.x86_64 yum list | grep nginx 安装stream, 安装后就可以使用stream这个功能 yum install -y nginx-mod-stream.x8…

LVS详解

一、概念简述 1.1LVS概念简述 1.1.1 LVS LVS&#xff08;Linux Virtual Server&#xff09;即Linux虚拟服务器&#xff0c;是由章文嵩博士主导的开源负载均衡项目&#xff0c;目前LVS已经被集成到Linux内核模块中。LVS基于内核网络层工作&#xff0c;有着超强的并发处理能力&…

av.codec.codec.UnknownCodecError: libx264

遇到 av.codec.codec.UnknownCodecError: libx264 这个错误通常意味着 PyAV 库尝试使用 libx264 编码器来编码或解码视频&#xff0c;但该编码器在你的系统中不可用。 libx264 是一个广泛使用的 H.264 视频编码库。如果你正在使用 PyAV 来处理视频&#xff0c;特别是当你尝试读…