6
votes

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.

1

1 Answers

19
votes

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:

HTTP load balancer architecture

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.