在 Google Cloud Platform 中创建 HTTP 负载平衡器 [英] Creating a HTTP load balancer in Google Cloud Platform

查看:24
本文介绍了在 Google Cloud Platform 中创建 HTTP 负载平衡器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

GCP 中的 HTTP 负载平衡器有很多活动部分,例如全局转发规则、目标代理、网址映射、后端服务和实例组.

由于我发现设置 HTTP 负载平衡器很困难,因此我编写了以下答案,汇总了在 GCP 中设置 HTTP 负载平衡器的步骤.

此问题是另一个

正如您从上述架构中看到的,设置 HLB 有很多活动部件.现在,我们要从实例模板、实例组到转发规则,从逆向构建.

1.实例模板: 虽然设置 HLB 不需要实例模板,因为您甚至可以使用非托管实例组并将其附加到 HLB.我更喜欢有一个实例模板和托管实例组,这样也可以为同构实例组添加自动缩放功能.我已在此处编写了创建实例模板的步骤.

让我们假设实例模板是 sample-template.

2.托管实例组:请在此处完成创建托管实例组和自动缩放的步骤一>.自动缩放和负载均衡是相互独立的.两者都提供健康检查.从我的角度来看,为负载平衡器和自动缩放设置健康检查是多余的,我认为单独为负载平衡器进行健康检查会很好.

让我们假设托管实例组是 autoscale-managed-instance 组,它是基于 sample-template 自动缩放和创建的.

3.服务端点: 您需要指定 HLB 将使用的服务端点.在 gcloud 中设置服务端点的命令:

gcloud compute instance-groups managed 设置命名端口 自动缩放管理实例组 --named-ports http:80 --region 亚洲-东北1

上述命令在实例组中创建了一个服务端点,帮助 HLB 与组中的同构实例进行通信.

4.健康检查:这是必不可少的,以确保 HLB 仅将流量路由到健康的实例.创建健康检查的命令:

gcloud compute http-health-checks create sample-health-check

5.后端服务: 此服务将流量路由到所有后端实例,即实例组中的实例.它还关联实例的健康检查,并确保它只将流量路由到健康的实例.创建后端服务的命令:

gcloud计算后端服务创建样本后端服务--http-health-checks 样本健康检查

上述命令创建了一个后端服务,并将自身与上一步创建的健康检查相关联.现在,通过运行以下命令将实例组添加到后端服务:

gcloud compute backend-services add-backend sample-backend-service --instance-group 样本管理实例组 --平衡模式速率--max-rate-per-instance 100 --instance-group-region asia-northeast1

上述命令将实例组附加到后端服务,并使用 balancing-mode 标志根据请求数量分散负载.ma​​x-rate-per-instance 由自动调节器使用,前提是您已根据负载均衡器的利用率设置了自动调节器策略.

6.URL 映射: 创建一个 URL 映射,将 HTTP 请求 URL 映射到您的后端服务.用于创建网址映射的命令:

gcloud compute url-maps create sample-map --default-service 示例后端服务

当您选择基于内容的负载均衡器时,您需要在网址映射中添加许多条目,以便将请求路由到适当的后端服务.

7.目标 HTTP 代理: 此步骤将目标代理与网址映射相关联.创建目标HTTP代理的命令:

gcloud compute target-http-proxies 创建样本目标代理--url-map 示例地图

8.转发规则: 这是向 HLB 提供全局外部 IP 地址的最后一步.

gcloud 计算转发规则 创建样本转发 - 全球的 --端口 80 --target-http-proxy 样本-目标-代理

现在,在浏览器中访问 HLB 的 IP 地址将提供由实例组中的实例提供的网页.这最终建立了一个高度可扩展的 Web 应用程序,该应用程序具有自动扩展和负载平衡功能.

这是三部分系列第 3 部分,内容是关于构建自动缩放的负载-平衡后端.

HTTP load balancer in GCP has lot of moving parts like global forwarding rules, target proxies, URL map, backend services and instance groups.

Since I found difficult in setting up my HTTP load balancer, I have written the below answer that aggregates the steps for setting up HTTP load balancer in GCP.

This question is a part of another question's answer, which is about building an autoscaled and load-balanced backend.

解决方案

At first, GCP offers two types of load balancer, namely the Network and HTTP(s) load balancers. You can find the differences between Network Vs HTTP(s) load balancer here. From this point, I will be calling HTTP load balancer as HLB(coz it is too long).

Here is the architecture of HLB taken from GCP:

As you have seen from the above architecture, there are lot of moving parts for setting up the HLB. Now, We are going to build it from backwards starting from Instance template, Instance group to Forwarding rules.

1. Instance Template: Though instance template is not needed to set up HLB, as you can use even unmanaged instance group and attach it with HLB. I prefer having an instance template and managed instance group, so that autoscaling feature can also be added for the group of homogeneous instances. I have written the steps for creating an instance template here.

Lets assume the instance template to be sample-template.

2. Managed Instance Group: Please go through the steps for creating managed instance group and autoscaling here. Autoscaling and load balancing are independent of each other. Both offers health check. From my perspective, setting up the health check for both load balancer and autoscaling are redundant and I think health check for load balancer alone would do good.

Lets assume the managed instance group to be autoscale-managed-instance group, which is autoscaled and created based on sample-template.

3. service Endpoint: You need to specify the service endpoint which will be used by the HLB. The command for setting up the service endpoint in gcloud:

gcloud compute instance-groups managed 
set-named-ports 
autoscale-managed-instance group 
--named-ports http:80 
--region asia-northeast1

The above command creates a service endpoint in the instance group that helps the HLB to communicate with the homogeneous instances in the group.

4. Health Check: This is essential so as to assure that HLB routes traffic only to healthy instances. The command to create the health check:

gcloud compute http-health-checks create sample-health-check

5. Backend services: This service routes the traffic to all the backend instances i.e instances in the instance group. It also associates the health check of the instances and make sure it routes traffic only to healthy instances. The command for creating the backend service:

gcloud compute backend-services create 
sample-backend-service 
--http-health-checks sample-health-check

The above command creates a backend service and associates itself to the health check created at the previous step. Now, adding the instance group to the backend service by running the following command:

gcloud compute backend-services add-backend sample-backend-service 
--instance-group 
sample-managed-instance-group 
--balancing-mode RATE 
--max-rate-per-instance 100 
--instance-group-region asia-northeast1

The above command attaches the instance group to backend-service and also spread the load based on the number of requests using the balancing-mode flag. max-rate-per-instance is used by the autoscaler, if you had set the autoscaler policy based on load balancer utilisation.

6. URL map: Creating a URL map that maps the HTTP request URL to your backend service. The command for creating the URL map:

gcloud compute url-maps create sample-map 
--default-service sample-backend-service

When you choose Content-based load balancer, you need to add many entries in URL maps, so that it routes the request to appropriate backend service.

7. Target HTTP proxy: This step associates the target proxy to the URL map. The command for creating target HTTP proxy:

gcloud compute target-http-proxies 
create sample-target-proxy 
--url-map sample-map

8. Forwarding rules: This is the final step which gives an global external IP address to the HLB.

gcloud compute forwarding-rules 
create sample-forward 
--global 
--ports 80 
--target-http-proxy sample-target-proxy

Now, accessing the HLB's IP address in the browser gives the web page served by instances in the instance group. This finally sets up an Highly scalable web application which is both autoscaled and load balanced.

This is part 3 of 3-part series about building an autoscaled, load-balanced backend.

这篇关于在 Google Cloud Platform 中创建 HTTP 负载平衡器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆