DEV Community

Jay Sheth
Jay Sheth

Posted on

🌍 How to Set Up a Global HTTP Load Balancer on Google Cloud (Step-by-Step with Screenshots)

Load balancing is an essential part of modern cloud architectures --- it helps distribute traffic across multiple backend instances, ensuring reliability, scalability, and performance.

In this tutorial, we'll set up a Global HTTP Load Balancer on Google Cloud Platform (GCP) using both the Cloud Shell (gcloud) and the Google Cloud Console (GUI).


🧱 Prerequisites

Before starting, make sure you have:

  • A GCP project (like Qwiklabs or your own)

  • Billing enabled

  • Cloud Shell or gcloud CLI access


πŸ–₯️ Step 1: Create Initial Compute Engine Instances

We'll start by creating 3 individual virtual machines. For a full tutorial, it's helpful to see how basic VMs are set up before moving to managed groups.

You can create them using the following gcloud commands, each setting up a simple Apache web server that displays its own name.

Bash

gcloud compute instances create www1\
  --zone=us-west1-c\
  --tags=network-lb-tag\
  --machine-type=e2-small\
  --image-family=debian-11\
  --image-project=debian-cloud\
  --metadata=startup-script='#!/bin/bash
    apt-get update
    apt-get install apache2 -y
    service apache2 restart
    echo "<h3>Web Server: www1</h3>" | tee /var/www/html/index.html'

gcloud compute instances create www2\
  --zone=us-west1-c\
  --tags=network-lb-tag\
  --machine-type=e2-small\
  --image-family=debian-11\
  --image-project=debian-cloud\
  --metadata=startup-script='#!/bin/bash
    apt-get update
    apt-get install apache2 -y
    service apache2 restart
    echo "<h3>Web Server: www2</h3>" | tee /var/www/html/index.html'

gcloud compute instances create www3\
  --zone=us-west1-c\
  --tags=network-lb-tag\
  --machine-type=e2-small\
  --image-family=debian-11\
  --image-project=debian-cloud\
  --metadata=startup-script='#!/bin/bash
    apt-get update
    apt-get install apache2 -y
    service apache2 restart
    echo "<h3>Web Server: www3</h3>" | tee /var/www/html/index.html'

Enter fullscreen mode Exit fullscreen mode

The VM Instances appear in the Console like this:


🧩 Step 2: Create an Instance Template

Instance templates define the configuration for VM instances that will be part of the managed instance group, ensuring they are identical and run the startup script.

Bash

gcloud compute instance-templates create lb-backend-template\
  --region=us-west1\
  --network=default\
  --subnet=default\
  --tags=network-lb-tag\
  --image-family=debian-11\
  --image-project=debian-cloud\
  --metadata=startup-script='#! /bin/bash
    apt-get update
    apt-get install -y apache2
    echo "<h1>Hello from $(hostname)</h1>" | tee /var/www/html/index.html
    systemctl restart apache2'

Enter fullscreen mode Exit fullscreen mode

The Instance Group Template appear in the Console like this:


βš™οΈ Step 3: Create a Managed Instance Group (MIG)

This group will manage the identical backend instances using the template we just created. We'll start with a size of 2.

Bash

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

Enter fullscreen mode Exit fullscreen mode

Bash

gcloud compute instance-groups managed set-named-ports lb-backend-group\
    --named-ports http:80\
    --zone=us-west1-c

Enter fullscreen mode Exit fullscreen mode

The Instance Group appear in the Console like this:


🌐 Step 4: Reserve a Global Static IP Address

This IP address will be the public, permanent address for your load balancer.

Bash

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

Enter fullscreen mode Exit fullscreen mode

The Static IP appear in the Console like this:


πŸ”₯ Step 5: Configure Firewall Rules

We need a firewall rule to allow HTTP traffic (port 80) and a separate rule to allow the Google Cloud Health Check probes to reach your backend instances.

Bash

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=network-lb-tag\
  --rules=tcp:80

Enter fullscreen mode Exit fullscreen mode

The Firewall appear in the Console like this:


❀️ Step 6: Create a Health Check

This checks your instances to ensure they are healthy before sending user traffic to them.

Bash

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

Enter fullscreen mode Exit fullscreen mode

The Health Check appear in the Console like this:


🧠 Step 7: Create a Backend Service

The Backend Service links your health check to your instance group and defines the protocol for traffic to the backends.

Bash

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

Enter fullscreen mode Exit fullscreen mode

The Backend service appear in the Console like this:


🧩 Step 8: Add the Instance Group to the Backend Service

Now, connect your Managed Instance Group to the Backend Service.

Bash

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

Enter fullscreen mode Exit fullscreen mode

The Instance Group added to the Backend Service appear in the Console like this:


🌍 Step 9: Create a URL Map

The URL map defines which backend service handles incoming requests. For this simple setup, all requests go to our single backend service.

Bash

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

Enter fullscreen mode Exit fullscreen mode

The URL Map appear in the Console like this:


🧭 Step 10: Create a Target HTTP Proxy

The Target HTTP Proxy receives the request from the forwarding rule and consults the URL map to determine where to send the traffic.

Bash

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

Enter fullscreen mode Exit fullscreen mode

πŸš€ Step 11: Create a Global Forwarding Rule

This is the final step, linking your static external IP to the proxy and listening on port 80 (HTTP).

Bash

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

Enter fullscreen mode Exit fullscreen mode

The Proxy appear in the Console like this:


βœ… Step 12: Test Your Load Balancer

It may take a few minutes for the Load Balancer to provision and for the health checks to pass.

  1. Go to Load Balancing > Frontends and copy the IP address (in this example: 34.54.232.204).

  2. Now open your browser and visit: http://34.54.232.204

You should see output similar to this:

Each time you refresh the page, the VM name (e.g., lb-backend-group-c151) may change, confirming that the load balancer is successfully distributing traffic between the healthy instances in your Managed Instance Group.

The Final Result:


πŸ’‘ Summary

Component Description
VM Instances Backend servers running Apache.
Instance Template & Group Automates VM creation and management (MIG).
Firewall Rules Allows HTTP and health check traffic.
Health Check Monitors backend VM health.
Backend Service Connects instances to the LB, uses the named port.
URL Map Routes incoming traffic to the correct backend service.
Frontend Rule Assigns the static IP and listens on port 80.

You've successfully built a global HTTP load balancer in GCP, distributing traffic between healthy backends using gcloud commands and GUI verification.

If you're looking for more details on the setup and configuration of different load balancer types, this video is a helpful resource: How to set up a Global HTTP Load Balancer with Compute Engine.

Top comments (0)