Table of contents
1.
Introduction
2.
Overview and Setup
2.1.
How to launch your lab and log in to the Google Cloud Console
3.
Activate Cloud Shell
4.
Step 1. Set the default region and zone for all resources
5.
Step 2. Create multiple web server instances
6.
Step 3. Configure the load balancing service
7.
Step 4. Sending traffic to your instances
8.
Step 5. Create an HTTP load balancer
9.
Step 6. Testing traffic sent to your instances
10.
Frequently Asked Questions
10.1.
What is the process of HTTP load balancing?
10.2.
What distinguishes an HTTP load balancer from a network load balancer?
10.3.
What is load balancer most appropriate for HTTPS?
10.4.
What three types of load balancers are there?
10.5.
Do load balancers perform HTTP to HTTPS redirects?
11.
Conclusion
Last Updated: Mar 27, 2024

Set Up Network and HTTP Load Balancers in GCP

Author Aditi
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

For networked, collaborative, hypermedia information systems, the Hypertext Transfer Protocol(HTTP) is an application layer protocol in the Internet protocol suite model. With the help of the proxy-based Layer 7 load balancer known as External HTTP Load Balancing, you can execute and scale your services behind a single external IP address.

A group of computers sharing resources that are available on or offered by network nodes is known as a computer network. Over digital links, the computers communicate with one another using standard communication protocols. Computers, servers, mainframes, network devices, peripherals, and other linked devices form a network that enables data sharing. The Internet, which connects millions of people worldwide, is an example of a network.

Let's dive into the article to learn how to set up network and HTTP load balancers in GCP.

intro img

Overview and Setup

Learn how to configure a network and an HTTP load balancer for your apps running on Compute Engine virtual machines in this article (VMs). 

On Google Cloud, load balancing may be done in several different ways. The following steps will be covered for load balancer setup  in this article:

  • HTTP Load Balancer
  • Network Load Balancer

How to launch your lab and log in to the Google Cloud Console

  • Choose the Start Lab option. If you need to pay for the lab, a pop-up appears where you may choose your mode of payment. In the Lab Details panel, which is on the left, are the following:
    • The button to access Google Console
    • Time still left
    • The temporary login information required for this lab
    • If more details are needed to complete this lab, please provide them.
  • To open Google Console, click. The lab starts spinning up resources before opening a new tab on the Sign in page.
  • In the Sign in dialogue, copy the Username from the Lab Details panel if necessary. Choose Next.
  • Copy the password from the Lab Details panel and paste it into the Welcome dialogue. Choose Next.
  • Go through the following pages by clicking:
    • The terms and conditions are accepted.
    • Do not include two-factor authentication or recovery alternatives (because this is a temporary account).
    • Avoid registering for free trials.

The Cloud Console opens on this tab after a little delay.

Activate Cloud Shell

A virtual machine called Cloud Shell is full of programming tools. It uses the Google Cloud and provides a persistent 5GB home directory. Your Google Cloud resources can be accessed via the command line using Cloud Shell.

  • To activate the cloud shell, click. At the top of the Google Cloud console, click the icon to Activate Cloud Shell.
  • Then click Continue.

Provisioning and connecting to the environment take some time. The project is already set to your PROJECT ID when you log in, and you have already been authenticated. A line in the output states the PROJECT ID for this session:

Your Cloud Platform project in this session is set to YOUR_PROJECT_ID

The command-line tool for Google Cloud is called gcloud. It supports tab completion and is already installed on Cloud Shell.

  • (Optional) With this command, you can list the name of the active account:
gcloud auth list

Output:

ACTIVE: *
ACCOUNT: student-01-xxxxxxxxxxxx@qwiklabs.net
To set the active account, run:
    $ gcloud config set account `ACCOUNT`
  • (Optional) This command will list the project ID:
gcloud config list project

Output:

[core]
project = <project_ID>

Step 1. Set the default region and zone for all resources

  • In Cloud Shell, set the default zone:
gcloud config set compute/zone 
  • Set the default region:
gcloud config set compute/region

Step 2. Create multiple web server instances

Create three Compute Engine VM instances, install Apache on them, and then add a firewall rule allowing HTTP traffic to reach the instances for this load balancing scenario.

The code <filled in at lab start> establishes the zone. You can reference all of these occurrences simultaneously by setting the tags field, for example, in a firewall rule. Additionally, each instance gets its home page, and Apache is installed using these commands.

  • In your default zone, create a virtual machine called www1.
  gcloud compute instances create www1 \
    --zone= \
    --tags=network-lb-tag \
    --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
      service apache2 restart
      echo "
<h3>Web Server: www1</h3>" | tee /var/www/html/index.html'
  • Establish www2 as a virtual computer in the default zone.
  gcloud compute instances create www2 \
    --zone= \
    --tags=network-lb-tag \
    --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
      service apache2 restart
      echo "
<h3>Web Server: www2</h3>" | tee /var/www/html/index.html'
  • In your default zone, create a virtual machine called www3.
  gcloud compute instances create www3 \
    --zone= \
    --tags=network-lb-tag \
    --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
      service apache2 restart
      echo "
<h3>Web Server: www3</h3>" | tee /var/www/html/index.html'
  • Make a firewall rule that permits outside traffic to reach the VM instances:
gcloud compute firewall-rules create www-firewall-network-lb \
    --target-tags network-lb-tag --allow tcp:80
  • You must now obtain your instances' external IP addresses and confirm that they are running.
  • To list your instances, run the following command. Their IP addresses are listed in the EXTERNAL IP column:
gcloud compute instances list
  • Use curl to check if each instance is active, replacing [IP ADDRESS] with the IP address of each of your virtual machines:
curl http://[IP_ADDRESS]
  • Click You may confirm that you've constructed a collection of web servers by looking at my progress below.

Step 3. Configure the load balancing service

Your virtual machine instances will receive packets intended for the static external IP address you define once the load balancing service is configured. A Compute Engine image automatically configures instances created with it to handle this IP address.

  • Create a static external IP address for your load balancer:
   gcloud compute addresses create network-lb-ip-1 \
    --region  
  • Add a resource for the traditional HTTP health check:
gcloud compute http-health-checks create basic-check
  • In the same area as your instances, add a target pool. Run the following commands to use the health check and construct the target pool, both of which are necessary for the service to work:
  gcloud compute target-pools create www-pool \
    --region  --http-health-check basic-check
  • The instances are added to the pool:
gcloud compute target-pools add-instances www-pool \
    --instances www1,www2,www3
  • Add a rule for forwarding:
gcloud compute forwarding-rules create www-rule \
    --region   \
    --ports 80 \
    --address network-lb-ip-1 \
    --target-pool www-pool

Step 4. Sending traffic to your instances

Once the load balancing service has been set up, you can send traffic to the forwarding rule and monitor it as it is split among various instances.

  • To view the external IP address of the load balancer's www-rule forwarding rule, enter the following command:
gcloud compute forwarding-rules describe www-rule --region 
  • Obtain the outside IP address
IPADDRESS=$(gcloud compute forwarding-rules describe www-rule --region  --format="json" | jq -r .IPAddress)
  • Display the public IP address
echo $IPADDRESS
  • Replace IP ADDRESS with the external IP address from the preceding command when using the curl command to visit the external IP address:
while true; do curl -m1 $IPADDRESS; done

Between the three instances, the curl command's response changes at random. If your answer is initially rejected, give the configuration 30 seconds to load fully and your instances designated as healthy before attempting again.

  • To stop the command from running, use Ctrl + c.

Step 5. Create an HTTP load balancer

Google Front End uses HTTP(S) Load Balancing (GFE). Using Google's global network and control plane, GFEs are dispersed worldwide and work together. You can set up URL rules to send some URLs to a specific group of instances while sending other URLs to different instances.

If an instance group has enough capacity and is the right one for the request, requests are always forwarded to it if it is close to the user. The request is directed to the nearest group with a capacity if the most immediate group does not have sufficient capacity.

Your VMs must be in an instance group if you want to configure a load balancer with a Compute Engine backend. The backend servers of an external HTTP load balancer are run by virtual machines that are part of the managed instance group. Backends serve their hostnames for this lab.

  • Make the load balancer template first:
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'
  • Based on the template, create a managed instance group:
gcloud compute instance-groups managed to create lb-backend-group \
   --template=lb-backend-template --size=2 --zone= 
  • Make the firewall rule 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
  • Set up a global static external IP address that your customers can use to access your load balancer now that the instances are up and running:
gcloud compute addresses create lb-ipv4-1 \
  --ip-version=IPV4 \
  --global
  • Create a load balancer health check:
gcloud compute health-checks create http http-basic-check \
  --port 80
  • Establish a backend service:
gcloud compute backend-services create web-backend-service \
  --protocol=HTTP \
  --port-name=http \
  --health-checks=http-basic-check \
  --global
  • Your instance group should be added to the backend service as the backend:
gcloud compute backend-services add-backend web-backend-service \
  --instance-group=lb-backend-group \
  --instance-group-zone= \
  --global
  • Make a URL map to direct incoming requests to the default backend.
gcloud compute url-maps create web-map-http \
    --default-service web-backend-service

Step 6. Testing traffic sent to your instances

  • Go to Network services > Load balancing in the Cloud Console by selecting it from the Navigation menu.
  • Select the newly constructed load balancer by clicking it (web-map-http).
  • Verify that the VMs are Healthy by clicking on the backend's name in the Backend section. Wait a few seconds and try reloading the page if they are not functioning correctly.
  • Test the load balancer using a web browser by visiting http://IP_ADDRESS/ and replacing IP_ADDRESS with the load balancer's IP address when the VMs are in good health.

It can take three to five minutes to complete. Wait a minute, and then restart the browser if you cannot connect. Your browser should display a page with information about the zone and instance that provided the page.

man thinking

Frequently Asked Questions

What is the process of HTTP load balancing?

A load balancer serves as the "traffic cop" in front of your servers, distributing client requests across all servers equipped to handle them in a way that maximizes speed and capacity utilization and ensures that no server is overworked, which can result in performance degradation.

What distinguishes an HTTP load balancer from a network load balancer?

While the global HTTPS load balancer is developed using Google Front Engines at the edge of Google's network and is for Layer-7 traffic. The regional network load balancer uses Maglev technology and is designed for Layer-4 traffic.

What is load balancer most appropriate for HTTPS?

Application Load Balancer offers advanced request routing focused on delivering current application architectures, including microservices and containers, and is ideally suited for load balancing HTTP and HTTPS traffic.

What three types of load balancers are there?

Application load balancers, network load balancers, and classic load balancers are all supported by elastic load balancing.

Do load balancers perform HTTP to HTTPS redirects?

By default, traditional load balancers cannot direct HTTP traffic to HTTPS. Instead, set up your rewrite rules for the web server instances behind the Classic Load Balancer. Rewrite rules must be set up to only redirect HTTP clients and use the X-Forwarded-Proto header.

Conclusion

This article extensively discussed how to set up a network and HTTP load balancers in GCP. We have also explained how to launch the lab and login into the google cloud console and the steps to load balancers in GCP in detail. 

We hope this blog has helped you enhance your Ansible Copy module knowledge. If you would like to learn more, check out our articles on Ansible Interview Question Part-1Part-2, and Part-3. Practice makes a man perfect. To practice and improve yourself in the interview, you can check out Top 100 SQL problemsInterview experienceCoding interview questions, and the Ultimate guide path for interviews.

Do upvote our blog to help other ninjas grow. Happy Coding!

thank you img
Live masterclass