Setting Up Network and HTTP Load Balancers [ACE] on GCP via Google Cloud Shell

Mehul Patel
Apr 28, 2020 · 6 min read

In this blog, we will see the differences between a network load balancer and a HTTP load balancer, and how to set them up for your applications working on Google Compute Engine virtual machines.

There are several ways you can load balance in Google Cloud Platform. This article takes you through the setup of the following load balancers:

Activate Google Cloud Shell

Google Cloud Shell is a virtual machine that is loaded with development tools. It allows a persistent 5GB home directory and works on the Google Cloud. Google Cloud Shell gives a command-line way to your GCP resources.

Note: gcloud is the command-line tool for Google Cloud Platform. It comes pre-installed on Cloud Shell and supports tab-completion.

Set the default region and zone for all resources

In Cloud Shell, set the default zone:

gcloud config set compute/zone asia-south1-a

Set the default region:

gcloud config set compute/region asia-south1

Learn more about choosing zones and regions here: Regions & Zones documentation.

Create multiple web server instances

To create the Nginx web server clusters, create the following:

  • A startup script to be used by every virtual machine instance to setup Nginx server upon startup
  • An instance template to use the startup script
  • A target pool
  • A managed instance group using the instance template

Jump to Cloud Shell and create a startup script “ startup.sh” to be used by every virtual machine instance. This script sets up the Nginx server upon startup:

https://gist.github.com/rowdymehul/572240a7bdff4055628931b155a8d356

Create an instance template, which uses the startup script:

gcloud compute instance-templates create nginx-template \          --metadata-from-file startup-script=startup.sh

(Output)

Create a target pool. A target pool enables a single access point to all the instances in a group and is essential for load balancing in the future steps.

gcloud compute target-pools create nginx-pool

(Output)

Create a managed instance group using the instance template:

gcloud compute instance-groups managed create nginx-group \                    --base-instance-name nginx \  
--size 3\
--template nginx-template \
--target-pool nginx-pool

(Output)

This creates 3 virtual machine instances with names that are prefixed with nginx-. This may take a couple of minutes.

List the compute engine instances, and you should see all of the instances created:

gcloud compute instances list

(Output)

Verify the instances: On the Navigation menu, click Compute Engine > VM instances.

Now configure a firewall so that you can connect to the machines on port 80 via the EXTERNAL_IP addresses:

gcloud compute firewall-rules create www-firewall --allow tcp:80

(Output)

Create a Network Load Balancer

Network load balancing lets you balance a load of your systems based on incoming IP protocol data, such as an address, port, and protocol type. You also notice some options that are not available, with HTTP(S) load balancing. For example, you can load balance additional TCP/UDP-based protocols such as SMTP traffic. And if your application is interested in TCP-connection-related characteristics, network load balancing passes your app to inspect the packets, where HTTP(S) load balancing does not.

For more information, see Setting Up Network Load Balancing.

Create an L3 network load balancer targeting your instance group:

gcloud compute forwarding-rules create nginx-lb \   
--region us-central1 \
--ports=80 \
--target-pool nginx-pool

(Output)

List all Google Compute Engine forwarding rules in your project.

gcloud compute forwarding-rules list

(Output)

You can then visit the load balancer from the browser http://IP_ADDRESS/ where IP_ADDRESS is the address shown as the result of running the previous command.

Create a HTTP(s) Load Balancer

HTTP(S) load balancing gives global load balancing for HTTP(S) requests reserved for your instances. You can configure URL rules that route some URLs to one set of instances and route other URLs to other instances. Requests perpetually routed to the instance group that is closest to the user, provided that the group has ample potential and is appropriate for the request. If the most intimate group does not have enough capacity, the request has sent to the closest group that does have the ability.

Learn more about the HTTP(s) Load Balancer in the documentation.

First, create a health check. Health checks verify that the instance is responding to HTTP or HTTPS traffic:

gcloud compute http-health-checks create http-basic-check

(Output)

Define an HTTP service and map a port name to the relevant port for the instance group. Now the load balancing service can forward traffic to the named port:

gcloud compute instance-groups managed \ 
set-named-ports nginx-group \
--named-ports http:80

(Output)

Updated [https://www.googleapis.com/compute/v1/projects/...].

Create a backend service:

gcloud compute backend-services create nginx-backend \        --protocol HTTP --http-health-checks http-basic-check --global

(Output)

Add the instance group into the backend service:

gcloud compute backend-services add-backend nginx-backend \                       --instance-group nginx-group \  
--instance-group-zone us-central1-a \
--global

(Output)

Create a default URL map that directs all incoming requests to all your instances:

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

(Output)

Create a target HTTP proxy to route requests to your URL map:

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

(Output)

Create a global forwarding rule to manage and route incoming requests. A forwarding rule sends traffic to a specific target HTTP or HTTPS proxy, depending on the IP address, IP protocol, and port defined. The global forwarding rule does not recommend multiple ports.

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

(Output)

Please note that, after creating the global forwarding rule, it can take several minutes for your configuration to propagate.

gcloud compute forwarding-rules list

(Output)

Take note of the http-content-rule IP_ADDRESS for the forwarding rule.

From the browser, you should be able to connect to http://IP_ADDRESS/. It may take three to five minutes. If you do not connect, wait a minute then reload the browser.

Please wait for three to five minutes and then check.

Thank you for reading. :)

TechnoPanti

Absorb the future tech!

TechnoPanti

TechnoPanti is an online platform focusing on the tech industry and posts the useful out-of-the-box articles for aspirants like you and me.

Mehul Patel

Written by

Tech Speaker | Linux Engineer | DevOps | OpenSource Enthusiast | Independent Researcher | Technical Writer | Explorer

TechnoPanti

TechnoPanti is an online platform focusing on the tech industry and posts the useful out-of-the-box articles for aspirants like you and me.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store