Open In App

How To Set Up A Multi-Region Load Balancer On GCP?

Load balancing is necessary in case of any production-ready application. The load balancer is used for load balancing. The main goal here is to distribute the incoming traffic across multiple regions to ensure the high availability and high reliability of an application. Google Cloud Platform provides us with services to configure load balancers for managing traffic on the hosted application. In this article, we will be learning about global load balancers and how we can configure a multi-regional or Global load balancer using Google Cloud Platform.

What Is A Multi-region Load Balancer?

A multi-region load balancer in Google Cloud Platform is a load balancer used for distributing incoming network traffic across multiple regions. This type of load balancer creates globally backed services with a backend in multiple regions, which helps ensure efficient scaling and fault tolerance in different geographical locations. Multi-regional load balancers ensure high availability and reliability. Using a multi-regional load balancer ensures traffic is directed to the closest backend, which reduces latency and improves the overall performance of an application for the users.



Setting Up multi-region Load balancer In GCP: A Step-By-Step Guide

Here we will be creating a cross-region internal application load balancer in a VPC Network with one backend service. Here our objective is to build two backends in two different regions, say us-east1 and us-west1 and then we will be creating forwarding rule so that our multi-region load balancer can serve the requests.

Step 1: Create VPC Network



Name: lb-network-crs-reg
 Name: lbsubnet-uswest1
Region: us-west1
IP address range: 10.1.2.0/24

Name: lbsubnet-useast1
Region: us-east1
IP address range: 10.1.3.0/24

Step 2: Configure Proxy-Only Subnet

    gcloud beta compute networks subnets create proxy-only-subnet1 \
--purpose=GLOBAL_MANAGED_PROXY \
--role=ACTIVE \
--region=us-west1 \
--network=lb-network-crs-reg \
--range=10.129.0.0/23

Output:

    gcloud beta compute networks subnets create proxy-only-subnet2 \
--purpose=GLOBAL_MANAGED_PROXY \
--role=ACTIVE \
--region=us-east1 \
--network=lb-network-crs-reg \
--range=10.130.0.0/23

Output:

Step 3: Create Firewall Rules

A) Firewall rule to allow incoming ssh:

On your VPC Network click on add firewall rule to allow incoming ssh connections and follow the configuration
Name: fw_ilb_to_backends
Network: lb-network-crs-reg
Direction of traffic: Ingress
Action on match: Allow
Targets: Specified target tags
Target tags: allow-ssh
Source filter: IPv4 ranges
Source IPv4 ranges: 0.0.0.0/0

B) Firewall rule to allow google health checks:

Again Click on Add Firewall rule to allow google cloud health checks,

C) Firewall rule to allow connection to backend:

Name: fw_backends
Network: lb-network-crs-reg
Direction of traffic: Ingress
Action on match: Allow
Targets: Specified target tags
Target tags: load-balanced-backend
Source filter: IPv4 ranges
Source IPv4 ranges: 10.129.0.0/23 and 10.130.0.0/23
Protocols and ports: Select the TCP checkbox, and then enter 80, 443, 8080 for the port numbers.

Step 4: Create managed instance group

A) Create Instance Template

Name: gil7-backendeast1-template

#!/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
Name: gil7-backendwest1-template

Boot Disk: Debian GNU/Linux 10 (buster).

#! /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

B) Create Instance Groups

Name: gl7-ilb-migb.
Location: Single zone.
Region: us-west1.
Zone: us-west1-a.
Instance template: gil7-backendwest1-template.
Autoscaling mode: Off:do not autoscale.
Maximum number of instances: 2.
Name: l7-ilb-backend-east
Location: Single zone.
Region: us-east1.
Zone: us-east1-b.
Instance template: gil7-backendeast1-template.
Autoscaling mode: Off:do not autoscale.
Maximum number of instances: 2.

Step 5: Configure Load Balancer

A) Create Health Check

gcloud compute health-checks create http global-http-health-check \
--use-serving-port \
--global

Output:

B) Create backend service And Add Backend

gcloud compute backend-services create gl7-gilb-backend-service \
--load-balancing-scheme=INTERNAL_MANAGED \
--protocol=HTTP \
--enable-logging \
--logging-sample-rate=1.0 \
--health-checks=global-http-health-check \
--global-health-checks \
--global

Output:

gcloud compute backend-services add-backend gl7-gilb-backend-service \
--balancing-mode=UTILIZATION \
--instance-group=gl7-ilb-migb \
--instance-group-zone=us-west1-a \
--global

Output:

gcloud compute backend-services add-backend gl7-gilb-backend-service \
--balancing-mode=UTILIZATION \
--instance-group=gl7-ilb-migb \
--instance-group-zone=us-east1-b \
--global

C) Create URL Map

gcloud compute url-maps create gl7-gilb-url-map \
--default-service=gl7-gilb-backend-service \
--global

Output:

D) Create Forwarding Rules

gcloud compute forwarding-rules create gil7forwarding-rule-a \
--load-balancing-scheme=INTERNAL_MANAGED \
--network=lb-network-crs-reg \
--subnet=lbsubnet-uswest1 \
--subnet-region=us-west1 \
--address=10.1.2.99 \
--ports=80 \
--target-http-proxy=gil7-http-proxy \
--global

Output:

gcloud compute forwarding-rules create gil7forwarding-rule-b \
--load-balancing-scheme=INTERNAL_MANAGED \
--network=lb-network-crs-reg \
--subnet=lbsubnet-useast1 \
--subnet-region=us-east1 \
--address=10.1.3.99 \
--ports=80 \
--target-http-proxy=gil7-http-proxy \
--global

Output:

Step 6: Verify The Multi-Region Load Balancer

Conclusion

A multi-region load balancer on Google Cloud Platform is a powerful tool to ensure high availability, scalability and performance of application using servers in different region. By efficiently distributing the load or the incoming traffic across multiple regions, these load balancers minimizes latency and optimize resource utilization. This features are still new to the Google cloud platform so there is a gap in GUI configuration, still can be configured using cloud shell commands.

Multi-region load balancer on GCP – FAQ’s

What Is A Multi-Region Load Balancer?

A multi region load balancer is a load balancer that distributes the incoming network traffic across multiple regions to reduce latency and optimize resource utilization for the application. This helps in ensuring high availability and reliability of the application.

What Is Health Check And Why Is It Necessary?

A health check is a regular test to ensure that instances are healthy and will be able to handle incoming traffic. That’s why it is a good practice to perform health check up before load balancer distributes traffic.

How To Create A Global Load Balancer?

To create a global load balancer, in your GCP Console, go to network services, then into Load balancing. Click on create load balancer and select HTTPS Load balancing or TCP/UDP Load balancing and then do the configurations with frontend and backends.

What Is Timeout In Load Balancer?

If the backend service doesn’t return a successful response, the load balancer closes the connection after waiting for 30 sec(generally) . This is known as Load balancer time-out. The timeout depends on the configurations made with the backend.

How Many Rules Can A Load Balancer Have?

The number of rules a load balancer can have depends on the load balancing service and it’s configuration. In GCP the maximum number of rules for a URL Map in the HTTPS Load balancing service is 1000.


Article Tags :