关于开发部署中(4层7层C端S端)软负载均衡笔记
7月3日 囍孤女投稿 写在前面负载这一块的知识有些乱,听人家常说软负载、硬负载,四层负载,七层负载、客户端负载,服务端负载之类的,所以梳理一下。负载均衡在系统架构中是一个非常重要,通过负载均衡可以提高系统的高可用,缓解网络、硬件资源的限制。本文主要涉及一些项目中常用的负载方式,很浅,不涉及负载算法啥的,做负载离不开代理,所以文中提到代理服务器即负载服务器。这里梳理的方式从Dev和Ops的概念出发。这里简单分类一下:
软负载
客户端负载
SpringCloudRibbon
服务端负载Ngixn
(47)层负载Hyproxy
(47)层负载kubeproxy
(47)层负载
LVS(4)层负载
硬负载
F5
关于LVS和kubeproxy、F5我们这里之后在和小伙伴分享,F5没有接触过,LVS的demo容器的方式一直没有成功,kubeproxy这一块我还没学到,只是简单的了解。
如果能深刻理解苦难,苦难就会给人带来崇高感。路遥一、软负载
处理传输层到应用层的数据,为了能通一个URL或者IPPORT将前端的访问分发到后台的多个服务器上1、客户端负载
Dev即开发角度的负载均衡。开发中的负载均衡一般是在微服务中涉及。服务提供方一般以多实例的形式提供服务,负载均衡功能能够让服务调用方连接到合适的服务节点。并且,服务节点选择的过程对服务调用方来说是透明的。
所以这里理解为是客户端的负载均衡,是相对服务端负载均衡而言。
客户端负载均衡来讲,就是调用的客户端本身是知道所有服务信息,当需要调用服务上的接口的时候,客户端从自身所维护的服务列表中,根据提前配置好的负载均衡策略,自己挑选一个服务来调用,此时,客户端知道它所调用的是哪一个服务。
在SpringCloud中使用在RestTemplate进行服务调用,要想使用负载均衡功能,需要使用SpringCloudRibbon。
SpringCloudRibbon是一个基于HTTP和TCP的客户端负载均衡工具,它基于NettlixRibbon实现。通过SpringCloud的封装,可以让我们轻松地将面向服务的REST模板请求自动转换成客户端负载均衡的服务调用。
使用时需要给RestTemplate实例上添加一个LoadBalanced注解即可,此时,RestTemplate就会自动具备负载均衡功能,这个负载均衡就是客户端负载均衡。packagecom。liruilong。importorg。springframework。boot。SpringAimportorg。springframework。boot。autoconfigure。SpringBootAimportorg。springframework。cloud。client。discovery。EnableDiscoveryCimportorg。springframework。context。annotation。Bimportorg。springframework。web。client。RestTSpringBootApplicationEnableDiscoveryClientpublicclassConsulConApplication{publicstaticvoidmain(String〔〕args){SpringApplication。run(ConsulConApplication。class,args);}BeanLoadBalancedRestTemplaterestTemplate(){returnnewRestTemplate();}}二、服务端负载
Ops即运维角度的负载均衡,这里的负载我们也称为服务端负载
所谓服务端负载均衡,比如传统的Nginx的方式,调用的客户端并不知道具体是哪个服务提供的服务,它也不关心,反正请求发送给Nginx,或者hyproxy作为代理的服务器,然后Ngixn在请求负载任意服务,客户端只需要记着Nginx的地址即可。1、Nginx负载七层(应用层)负载
Nginx7层负载是最常见的一种负载,所谓7层负载,即应用层负载,即基于应用层协议(TELNET,SSH,HTTP,SMTP,POP)做的代理,7层负载需要解析数据包的具体内容,需要消耗额外的cpu,然后根据具体内容(url,参数,cookie,请求头)匹配相应的路径,然后转发到相应的服务器。转发的过程是:建立和目标机器的连接,然后转发请求,收到响应数据再转发给请求客户端。
使用docker构建一个内部网络〔rootliruilongs。github。io〕〔〕dockernetworkcreatesubnet10。1。1。124loadbalancing0e0cdf9c70b038f9bcd44fd282ddc3e5bff77403ca28ce5b9006c20793ae2f8d
内网里运行两个httpd服务
10。1。1。22〔rootliruilongs。github。io〕〔loadbalancing〕dockerrunitdnameweb1netloadbalancingp80hweb1ip10。1。1。22httpdccaa091f295d40c61e50f103e9d84b86caddf9f98d6e5075de3690d93ab48f70〔rootliruilongs。github。io〕〔loadbalancing〕echo10。1。1。22index。catindex。html10。1。1。22〔rootliruilongs。github。io〕〔loadbalancing〕dockercp。index。htmlweb1:usrlocalapache2htdocs〔rootliruilongs。github。io〕〔loadbalancing〕dockerpsCONTAINERIDIMAGECOMMANDCREATEDSTATUSPORTSNAMESccaa091f295dhttpdhttpdforeground29secondsagoUp28seconds0。0。0。0:4915380tcp,:::4915380tcpweb1〔rootliruilongs。github。io〕〔loadbalancing〕curl127。0。0。1:4915310。1。1。22〔rootliruilongs。github。io〕〔loadbalancing〕
10。1。1。33〔rootliruilongs。github。io〕〔loadbalancing〕dockerrunitdnameweb2netloadbalancingp80hweb2ip10。1。1。33httpd5b08d54cf4983f6f6ce69cc0cee4b2eab2684cfde8deee89796196760924e434〔rootliruilongs。github。io〕〔loadbalancing〕echo10。1。1。33index。catindex。html10。1。1。33〔rootliruilongs。github。io〕〔loadbalancing〕dockercp。index。htmlweb2:usrlocalapache2htdocs〔rootliruilongs。github。io〕〔loadbalancing〕dockerpsCONTAINERIDIMAGECOMMANDCREATEDSTATUSPORTSNAMES5b08d54cf498httpdhttpdforeground40secondsagoUp38seconds0。0。0。0:4915480tcp,:::4915480tcpweb2ccaa091f295dhttpdhttpdforeground6minutesagoUp6minutes0。0。0。0:4915380tcp,:::4915380tcpweb1〔rootliruilongs。github。io〕〔loadbalancing〕curl127。0。0。1:4915410。1。1。33〔rootliruilongs。github。io〕〔loadbalancing〕
Ngixn实现到上面两个httpd服务的负载
ng配置文件
〔rootliruilongs。github。io〕〔loadbalancing〕catnginx。errorlogvarlognginxerror。pidvarrunnginx。events{workerconnections1024;}http{includeetcnginxmime。logformatmainupstreamaddrremoteaddrremoteuser〔timelocal〕accesslogvarlognginxnginxaccess。keepalivetimeout65;includeetcnginxconf。d。server{listen8099;indexindex。htmlindex。accesslogvarlognginxdefaultaccess。errorlogvarlognginxdefaulterror。location{proxypasshttp:}location。{proxypasshttp:proxysetheaderHproxysetheaderXRealIPproxysetheaderXForwardedF}}upstreambackend{serverweb2:80;serverweb1:80;}}
运行Nginx容器〔rootliruilongs。github。io〕〔loadbalancing〕dockerrunitdp8099:8099namenginxnetworkloadbalancingvPWDnginx。conf:etcnginxnginx。confnginx0af20ed5c390e81398037a498fc7d385cac96cd2f403a8b08f6f4e09d7a20ee0
测试一下〔rootliruilongs。github。io〕〔loadbalancing〕dockerpsCONTAINERIDIMAGECOMMANDCREATEDSTATUSPORTSNAMES0af20ed5c390nginxdockerentrypoint。4secondsagoUp3seconds80tcp,0。0。0。0:80998099tcp,:::80998099tcpnginxb16bcb89e0a8httpdhttpdforeground32minutesagoUp32minutes0。0。0。0:4915580tcp,:::4915580tcpweb15b08d54cf498httpdhttpdforeground39minutesagoUp39minutes0。0。0。0:4915480tcp,:::4915480tcpweb2〔rootliruilongs。github。io〕〔loadbalancing〕curl127。0。0。1:809910。1。1。33〔rootliruilongs。github。io〕〔loadbalancing〕curl127。0。0。1:809910。1。1。22〔rootliruilongs。github。io〕〔loadbalancing〕四层(传输层)负载
所谓四层负载,即在传输层协议的基础上来做负载,基于TCP,UDP等协议,传输层的作用是确保数据被可靠的传输送到目标地址,能够让应用程序之间实现通信,所以彼此传递的是数据包,标识的只有IP端口。不涉及具体的url其他结构解析。路径匹配等,不会涉及具体的应用层协议,所以理论上四层负载要比七成负载快。
nginx四层代理是nginx1。9。0开始新增的功能,需要开启withstream模块,可以实现四层协议的转发、代理、负载等功能。
这里的话,我们还是用容器的方式。配置方式和七层主要是配置文件的区别
ng配置文件
catnginx。errorlogvarlognginxerror。pidvarrunnginx。events{workerconnections1024;}四层代理的方式stream{server{listen8088;}upstreambackend{serverweb1:80;serverweb2:80;}}http{这个是协议级别includemime。keepalivetimeout65;server{这个是服务器级别listen80;location{这个是请求级别indexindex。htmlindex。}}}
启动4层负载的Nginx〔rootliruilongs。github。io〕〔loadbalancing〕dockerrunitdp8088:8088namenginx4networkloadbalancingvPWDnginx。conf:etcnginxnginx。confnginxnginx7c342f86752c5fe494b5a142983503d82dd11ea54e2968da7172f2201d1c45ea〔rootliruilongs。github。io〕〔loadbalancing〕dockerlogsnginx4dockerentrypoint。sh:dockerentrypoint。disnotempty,willattempttoperformconfigurationdockerentrypoint。sh:Lookingforshellscriptsindockerentrypoint。ddockerentrypoint。sh:Launchingdockerentrypoint。d10listenonipv6bydefault。sh10listenonipv6bydefault。sh:info:Gettingthechecksumofetcnginxconf。ddefault。conf10listenonipv6bydefault。sh:info:EnabledlistenonIPv6inetcnginxconf。ddefault。confdockerentrypoint。sh:Launchingdockerentrypoint。d20envsubstontemplates。shdockerentrypoint。sh:Launchingdockerentrypoint。d30tuneworkerprocesses。shdockerentrypoint。sh:Creadyforstartup2021111616:25:50〔notice〕11:usingtheepolleventmethod2021111616:25:50〔notice〕11:nginx1。21。32021111616:25:50〔notice〕11:builtbygcc8。3。0(Debian8。3。06)2021111616:25:50〔notice〕11:OS:Linux3。10。0693。el7。x86642021111616:25:50〔notice〕11:getrlimit(RLIMITNOFILE):1048576:10485762021111616:25:50〔notice〕11:startworkerprocesses2021111616:25:50〔notice〕11:startworkerprocess312021111616:25:50〔notice〕11:startworkerprocess32〔rootliruilongs。github。io〕〔loadbalancing〕dockerpsCONTAINERIDIMAGECOMMANDCREATEDSTATUSPORTSNAMES7c342f86752cnginxdockerentrypoint。21secondsagoUp19seconds80tcp,0。0。0。0:80888088tcp,:::80888088tcpnginx40af20ed5c390nginxdockerentrypoint。2hoursagoUp2hours80tcp,0。0。0。0:80998099tcp,:::80998099tcpnginxb16bcb89e0a8httpdhttpdforeground2hoursagoUp2hours0。0。0。0:4915580tcp,:::4915580tcpweb15b08d54cf498httpdhttpdforeground2hoursagoUp2hours0。0。0。0:4915480tcp,:::4915480tcpweb2
测试一下〔rootliruilongs。github。io〕〔loadbalancing〕curl127。0。0。1:808810。1。1。22〔rootliruilongs。github。io〕〔loadbalancing〕curl127。0。0。1:808810。1。1。33〔rootliruilongs。github。io〕〔loadbalancing〕curl127。0。0。1:808810。1。1。222、HAProxy负载
HAProxy是一款提供高可用性、负载均衡以及基于TCP(第四层)和HTTP(第七层)应用的代理软件,支持虚拟主机,它是免费、快速并且可靠的一种解决方案。
HAProxy特别适用于那些负载特大的web站点,这些站点通常又需要会话保持或七层处理。HAProxy完全可以支持数以万计的并发连接。7层(应用层)负载
这里我们还用之前的连个httpd服务演示〔rootliruilongs。github。io〕〔loadbalancing〕dockerpullhaproxy
haproxy。cfg配置文件〔rootliruilongs。github。io〕〔loadbalancing〕cathaproxy。cfggloballog127。0。0。1local0log127。0。0。1local1noticemaxconn4096defaultslogglobalmodehttpoptionhttplogoptiondontlognulltimeoutconnect5000mstimeoutclient50000mstimeoutserver50000mslistenadminstatsbind0。0。0。0:8070modehttpstatsenablestatshideversionstatsscope。statsrealmHaproxyStatisticsstatsrefresh30sstatsuristatsauthuser:passfrontendbalancerbind0。0。0。0:8077modehttpdefaultbackendwebbackendsbackendwebbackendsmodehttpoptionforwardforbalanceroundrobinserverweb1web1:80checkserverweb2web2:80checkoptionhttpchkGEThttpcheckexpectstatus200〔rootliruilongs。github。io〕〔loadbalancing〕〔rootliruilongs。github。io〕〔loadbalancing〕dockerrunitdp8070p8077namehaproxynetloadbalancingvPWDhaproxy。cfg:usrlocaletchaproxyhaproxy。cfghaproxya467636f6fdc75a7cf7538c32caab78c5a9e465235e2baec212bc9274a2cd534〔rootliruilongs。github。io〕〔loadbalancing〕dockerpsCONTAINERIDIMAGECOMMANDCREATEDSTATUSPORTSNAMESa467636f6fdchaproxydockerentrypoint。s2secondsagoUp1second0。0。0。0:491638070tcp,:::491638070tcp,0。0。0。0:491628077tcp,:::491628077tcphaproxy7c342f86752cnginxdockerentrypoint。40minutesagoUp40minutes80tcp,0。0。0。0:80888088tcp,:::80888088tcpnginx40af20ed5c390nginxdockerentrypoint。2hoursagoUp2hours80tcp,0。0。0。0:80998099tcp,:::80998099tcpnginxb16bcb89e0a8httpdhttpdforeground3hoursagoUp3hours0。0。0。0:4915580tcp,:::4915580tcpweb15b08d54cf498httpdhttpdforeground3hoursagoUp3hours0。0。0。0:4915480tcp,:::4915480tcp
测试下〔rootliruilongs。github。io〕〔loadbalancing〕curl127。0。0。1:4916210。1。1。22〔rootliruilongs。github。io〕〔loadbalancing〕curl127。0。0。1:4916210。1。1。33〔rootliruilongs。github。io〕〔loadbalancing〕
统计页面
4层(传输层)负载
四层负载和七层负载也是配置文件的区别〔rootliruilongs。github。io〕〔loadbalancing〕cathaproxy。cfggloballog127。0。0。1local0log127。0。0。1local1noticemaxconn4096defaultslogglobalmodehttpoptionhttplogoptiondontlognulltimeoutconnect5000mstimeoutclient50000mstimeoutserver50000mslistenadminstatsbind0。0。0。0:8070modehttpstatsenablestatshideversionstatsscope。statsrealmHaproxyStatisticsstatsrefresh30sstatsuristatsauthuser:passlistenwebbind:3306modetcpbalanceroundrobinserverweb110。1。1。22:80weight1checkinter1srise2fall2serverweb210。1。1。33:80weight1checkinter1srise2fall2〔rootliruilongs。github。io〕〔loadbalancing〕
运行容器并测试〔rootliruilongs。github。io〕〔loadbalancing〕dockerrunitdp8070p3306namehaproxy4netloadbalancingvPWDhaproxy。cfg:usrlocaletchaproxyhaproxy。cfghaproxy602eebc6ad525c420bad6061d9465fa7cdf2036d7db177efaf5450edf8c9db8b〔rootliruilongs。github。io〕〔loadbalancing〕dockerpsCONTAINERIDIMAGECOMMANDCREATEDSTATUSPORTSNAMES602eebc6ad52haproxydockerentrypoint。s8secondsagoUp6seconds0。0。0。0:491653306tcp,:::491653306tcp,0。0。0。0:491648070tcp,:::491648070tcphaproxy4a467636f6fdchaproxydockerentrypoint。s19hoursagoUp19hours0。0。0。0:491638070tcp,:::491638070tcp,0。0。0。0:491628077tcp,:::491628077tcphaproxy7c342f86752cnginxdockerentrypoint。20hoursagoUp20hours80tcp,0。0。0。0:80888088tcp,:::80888088tcpnginx40af20ed5c390nginxdockerentrypoint。21hoursagoUp21hours80tcp,0。0。0。0:80998099tcp,:::80998099tcpnginxb16bcb89e0a8httpdhttpdforeground22hoursagoUp22hours0。0。0。0:4915580tcp,:::4915580tcpweb15b08d54cf498httpdhttpdforeground22hoursagoUp22hours0。0。0。0:4915480tcp,:::4915480tcpweb2〔rootliruilongs。github。io〕〔loadbalancing〕curl127。0。0。1:4916510。1。1。22〔rootliruilongs。github。io〕〔loadbalancing〕curl127。0。0。1:4916510。1。1。33〔rootliruilongs。github。io〕〔loadbalancing〕
投诉 评论