How to set up an HTTP Load Balancer in GCP

Reading Time: 3 minutes

Introduction

Hi everyone! Today in this blog, we’ll learn how to set up an HTTP Load Balancer in GCP. As a prerequisite, you can check out my previous blog where I demonstrated how you can set up a network load balancer in GCP.

How it works

HTTP(S) Load Balancing is implemented on Google Front End (GFE). GFEs are distributed globally and operate together using Google’s global network and control plane. You can configure URL rules to route some URLs to one set of instances and others to other instances.

Requests are always routed to the instance group closest to the user. If that group has enough capacity and is appropriate for the request. If the closest group does not have enough capacity, the request is sent to the closest group that does have capacity.

To set up a load balancer with a Compute Engine backend, your VMs need to be in an instance group. The managed instance group provides VMs running the backend servers of an external HTTP load balancer. For this lab, backends serve their own hostnames.

Steps involved

Now, let’s get started with the steps involved in setting up the load balancer –

  • Activating cloud shell
  • Setting up default region and zone
  • Creating the load balancer template
  • Creating a managed instance group based on the template
  • Creating the firewall rule
  • Setting up a global static external IP address
  • Creating a health check
  • Creating a backend service
  • Adding instances to the backend service
  • Creating a URL map
  • Creating a target HTTP proxy
  • Creating a global forwarding rule

1. Activating cloud shell

After launching your GCP console, look for the Activate Cloud Shell button in the top right toolbar as shown below –

Load Balancer

2. Setting up default region and zone

To set up the default region in the cloud shell, use the following command –

gcloud config set compute/zone us-central1

To set up the default zone –

gcloud config set compute/zone us-central1-a

3. Creating the load balancer template

gcloud compute instance-templates create lb-backend-template \
   --region= \
   --network=default \
   --subnet=default \
   --tags=allow-health-check \
   --machine-type=e2-medium \
   --image-family=debian-11 \
   --image-project=debian-cloud \
   --metadata=startup-script='#!/bin/bash
     apt-get update
     apt-get install apache2 -y
     a2ensite default-ssl
     a2enmod ssl
     vm_hostname="$(curl -H "Metadata-Flavor:Google" \
     http://169.254.169.254/computeMetadata/v1/instance/name)"
     echo "Page served from: $vm_hostname" | \
     tee /var/www/html/index.html
     systemctl restart apache2'

4. Creating a managed instance group based on the template

Managed instance groups (MIGs) let you operate apps on multiple identical VMs. You can make your workloads scalable and highly available by taking advantage of automated MIG services, including autoscaling, auto-healing, regional (multiple zones) deployment, and automatic updating.

gcloud compute instance-groups managed create lb-backend-group \
   --template=lb-backend-template --size=2 --zone= 

5. Creating the firewall rule

Here, we’re creating a firewall rule named fw-allow-health-check

gcloud compute firewall-rules create fw-allow-health-check \
  --network=default \
  --action=allow \
  --direction=ingress \
  --source-ranges=130.211.0.0/22,35.191.0.0/16 \
  --target-tags=allow-health-check \
  --rules=tcp:80

6. Setting up a global static external IP address

Now that the instances are up and running, set up a global static external IP address that your customers use to reach your load balancer.

gcloud compute addresses create lb-ipv4-1 \
  --ip-version=IPV4 \
  --global

Note the IPv4 address that was reserved: \

gcloud compute addresses describe lb-ipv4-1 \
  --format="get(address)" \
  --global

7. Creating a health check

Google Cloud provides health checking mechanisms that determine whether backend instances respond properly to traffic.

gcloud compute health-checks create http http-basic-check \
  --port 80

8. Creating a backend service

gcloud compute backend-services create web-backend-service \
  --protocol=HTTP \
  --port-name=http \
  --health-checks=http-basic-check \
  --global

9. Adding instances to the backend service

gcloud compute backend-services add-backend web-backend-service \
  --instance-group=lb-backend-group \
  --instance-group-zone= \
  --global

10. Creating a URL map

URL map is a Google Cloud configuration resource used to route requests to backend services or backend buckets. For example, with an external HTTP(S) load balancer, you can use a single URL map to route requests to different destinations based on the rules configured in the URL map:

gcloud compute url-maps create web-map-http \
    --default-service web-backend-service

11. Creating a target HTTP proxy

gcloud compute target-http-proxies create http-lb-proxy \
    --url-map web-map-http

12. Creating a global forwarding rule

A forwarding rule and its corresponding IP address represent the frontend configuration of a Google Cloud load balancer.

gcloud compute forwarding-rules create http-content-rule \
    --address=lb-ipv4-1\
    --global \
    --target-http-proxy=http-lb-proxy \
    --ports=80

Okay, so that’s all for now. I hope this blog was helpful to you. Please feel free to drop any comments, questions, or suggestions.

Written by 

Riya is a DevOps Engineer with a passion for new technologies. She is a programmer by heart trying to learn something about everything. On a personal front, she loves traveling, listening to music, and binge-watching web series.