七层负载(Application Gateway)+四层负载(LB)

 

上次有个电商客户需要搭建如架构。

192.168.1.100/url1(请求url)——>Node1:10.0.0.4、10.0.0.5(服务器IP)

192.168.1.100/url2(请求url)——>Node2:10.0.0.6、10.0.0.7(服务器IP)

一个客户端根据请求Url进行流量分配,/url1流量走到Node1,然后Node1这个节点再进行一次流量负载。那么这种应用场景可以使用七层负载均衡(Application Gateway)+四层负载均衡(LB),架构图如下:

此种架构在Azure上实现起来是非常方便的,因为Azure直接就提供7层和4层负载均衡的Pass服务。

1、创建虚拟机(应用服务器)

按照上图架构中所示,这里我需要创建四台虚拟机,创建虚机的具体步骤这里就不赘述了。

按照上图所示,我已经创建好了四台虚机:imagevm01、imagevm02、videovm01、videovm02

然后,我分别给这四台虚拟机安装IIS,部署了一个网站,每个虚机部署的网站都带有自己的唯一标识。

 

2、创建四层负载均衡器(Load Banlancer)

 

按照如下图所示创建两个Load Banlancer

按照上图我已经创建好了两个Load Banlancer:imagelb、videolb

然后我们需要分别将imagevm01、imagevm02加入imagelb的后端池

videovm01、videovm02加入videolb的后端池

3、创建七层负载均衡(Application Gateway)

登录到 Azure

Login-AzureRmAccount -EnvironmentName AzureChinaCloud

 

 

 

创建资源组或获取资源组

因为我们创建虚拟机和四层负载均衡已经创建好了资源组,将Application Gateway和这些资源放在一个资源组即可

Get-AzureRmResourceGroup -Name "{resource group name}" -Location ""

 

 

或者也可以新建一个资源组。

New-AzureRmResourceGroup -Name AppgwRG -Location "China East"

 

 

获取虚拟网络

$vnet = Get-AzureRmVirtualNetwork -ResourceGroupName "{resource group name}" -Name "{virual network name}"

 

 

分配子网变量,便于完成后面的创建应用程序网关的步骤。

$subnet=$vnet.Subnets[0]

 

 

创建前端配置的公共 IP 地址

创建前端的公共IP,这个IP就是我们最终访问的Application Gateway的IP地址

$publicip = New-AzureRmPublicIpAddress -ResourceGroupName AppgwRG -name publicIP01 -location "China North" -AllocationMethod Dynamic

 

 

创建应用程序网关配置

创建名为“gatewayIP01”的应用程序网关 IP 配置。当应用程序网关启动时,它会从配置的子网获取 IP 地址,再将网络流量路由到后端 IP 池中的 IP 地址。请记住,每个实例需要一个 IP 地址。

$gipconfig = New-AzureRmApplicationGatewayIPConfiguration -Name gatewayIP01 -Subnet $subnet

 

 

 

分别配置名为“pool01”和“pool2”的后端 IP 地址池,其中,“pool1”的 IP 地址为imagelb的IP地址“42.159.242.134 (imagelbpublicip)”;“pool2”的 IP 地址为videolb的IP地址“139.219.185.186 (videolbpublicip)”。

 

$pool1 = New-AzureRmApplicationGatewayBackendAddressPool -Name pool01 -BackendIPAddresses 134.170.185.46, 134.170.188.221,134.170.185.50

$pool2 = New-AzureRmApplicationGatewayBackendAddressPool -Name pool02 -BackendIPAddresses 134.170.186.46, 134.170.189.221,134.170.186.50

 

 

 

 

 

为后端池中进行了负载均衡的网络流量配置应用程序网关设置“poolsetting01”和“poolsetting02”

 

$poolSetting01 = New-AzureRmApplicationGatewayBackendHttpSettings -Name "besetting01" -Port 80 -Protocol Http -CookieBasedAffinity Disabled -RequestTimeout 120

$poolSetting02 = New-AzureRmApplicationGatewayBackendHttpSettings -Name "besetting02" -Port 80 -Protocol Http -CookieBasedAffinity Enabled -RequestTimeout 240

 

 

 

 

 

 

使用公共 IP 终结点配置前端 IP

$fipconfig01 = New-AzureRmApplicationGatewayFrontendIPConfig -Name "frontend1" -PublicIPAddress $publicip

 

 

 

配置应用程序网关的前端端口

$fp01 = New-AzureRmApplicationGatewayFrontendPort -Name "fep01" -Port 80

 

 

 

配置侦听器。此步骤针对用于接收传入网络流量的公共 IP 地址和连接端口配置侦听器。

$listener = New-AzureRmApplicationGatewayHttpListener -Name "listener01" -Protocol Http -FrontendIPConfiguration $fipconfig01 -FrontendPort $fp01

 

 

 

配置后端池的 URL 规则路径

$imagePathRule = New-AzureRmApplicationGatewayPathRuleConfig -Name "pathrule1" -Paths "/image/*" -BackendAddressPool $pool1 -BackendHttpSettings $poolSetting01

$videoPathRule = New-AzureRmApplicationGatewayPathRuleConfig -Name "pathrule2" -Paths "/video/*" -BackendAddressPool $pool2 -BackendHttpSettings $poolSetting02

 

 

 

 

 

配置默认的后端池,如果路径不符合任何预定义的路径规则,规则路径映射到默认后端池

$urlPathMap = New-AzureRmApplicationGatewayUrlPathMapConfig -Name "urlpathmap" -PathRules $videoPathRule, $imagePathRule -DefaultBackendAddressPool $pool1 -DefaultBackendHttpSettings $poolSetting02

 

 

 

创建规则设置

$rule01 = New-AzureRmApplicationGatewayRequestRoutingRule -Name "rule1" -RuleType PathBasedRouting -HttpListener $listener -UrlPathMap $urlPathMap

 

 

 

配置实例数目和应用程序网关的大小

$sku = New-AzureRmApplicationGatewaySku -Name "Standard_Small" -Tier Standard -Capacity 2

 

 

 

创建应用程序网关

$appgw = New-AzureRmApplicationGateway -Name appgwtest -ResourceGroupName AppgwRG -Location "China East" -BackendAddressPools $pool1,$pool2 -BackendHttpSettingsCollection $poolSetting01, $poolSetting02 -FrontendIpConfigurations $fipconfig01 -GatewayIpConfigurations $gipconfig -FrontendPorts $fp01 -HttpListeners $listener -UrlPathMaps $urlPathMap -RequestRoutingRules $rule01 -Sku $sku

 

 

 

 

获取应用程序网关 DNS 名称

Get-AzureRmPublicIpAddress -ResourceGroupName Appgw-RG -Name publicIP01

 

 

 

测试访问

 

/image会路由到Image01或者Image02

/video会路由到Video01或者Video02

 

转载于:https://www.cnblogs.com/rampb/p/6542512.html

配置 Spring Cloud Gateway负载均衡可以使用 Spring Cloud 提供的 Ribbon 和 LoadBalancerClient,并结合 Gateway 的 Route 来实现。 首先在 pom.xml 文件中添加以下依赖: ```xml <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-ribbon</artifactId> </dependency> ``` 然后在 application.yml 文件中配置 Ribbon 的服务列表和 Gateway 的 Route: ```yaml spring: cloud: gateway: routes: - id: service1_route uri: lb://service1 predicates: - Path=/service1/** - id: service2_route uri: lb://service2 predicates: - Path=/service2/** ribbon: eureka: enabled: false listOfServers: http://localhost:8081,http://localhost:8082 ``` 上面的配置中,Ribbon 的服务列表使用 `listOfServers` 配置,多个服务使用逗号分隔。如果使用 Eureka 作为服务注册中心,可以将 `eureka.enabled` 设置为 `true`,这样 Ribbon 就会自动获取服务列表。 Gateway 的 Route 配置中,`uri` 使用 `lb://` 开头表示使用 Ribbon 进行负载均衡,`id` 为 Route 的唯一标识符,`predicates` 则是匹配请求的条件,这里使用 `Path` 匹配请求路径。 最后,需要在 Gateway 的启动类上添加 `@EnableDiscoveryClient` 注解,启用服务发现功能。 ```java @SpringBootApplication @EnableDiscoveryClient public class GatewayApplication { public static void main(String[] args) { SpringApplication.run(GatewayApplication.class, args); } } ``` 这样就完成了 Spring Cloud Gateway负载均衡配置。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值