How to set up a Network Load Balancer in GCP

Reading Time: 3 minutes

Hi everyone! Today in this blog we’ll be learning how to set up a Network Load Balancer in GCP. Before starting, let’s quickly brush up on our basic concepts.

What is a Load Balancer?

Simply speaking, a load balancer is something that distributes user traffic across multiple instances of your application. Load balancing reduces the risk that your application faces by spreading the load resulting in performance improvement.

There’s another thing known as Cloud Load Balancing. Let’s see what that is.

Cloud Load Balancing

According to google – It’s a fully distributed, software-defined managed service. It isn’t hardware based so you don’t need to manage a physical load balancing infrastructure. Now, let’s see how to create a network load balancer and how to set it up for your applications running on Compute Engine virtual machines (VMs).

Steps involved –

  • Activating cloud shell
  • Setting up the default region and zone
  • Creating multiple web server instances
  • Configuring the load balancing service
  • Sending traffic to your instances

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 the 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 multiple web server instances

Here, we’ll be creating three Compute Engine VM instances and then we’ll be installing Apache on them. Then we’ll be adding a firewall rule that will allow HTTP traffic to reach the instances.

gcloud compute instances create www1 \
  --image-family debian-9 \
  --image-project debian-cloud \
  --zone us-central1-a \
  --tags network-lb-tag \
  --metadata startup-script="#! /bin/bash
    sudo apt-get update
    sudo apt-get install apache2 -y
    sudo service apache2 restart
    echo '<!doctype html><html><body><h1>www1</h1></body></html>' | tee /var/www/html/index.html"
gcloud compute instances create www2 \
  --image-family debian-9 \
  --image-project debian-cloud \
  --zone us-central1-a \
  --tags network-lb-tag \
  --metadata startup-script="#! /bin/bash
    sudo apt-get update
    sudo apt-get install apache2 -y
    sudo service apache2 restart
    echo '<!doctype html><html><body><h1>www2</h1></body></html>' | tee /var/www/html/index.html"
gcloud compute instances create www3 \
  --image-family debian-9 \
  --image-project debian-cloud \
  --zone us-central1-a \
  --tags network-lb-tag \
  --metadata startup-script="#! /bin/bash
    sudo apt-get update
    sudo apt-get install apache2 -y
    sudo service apache2 restart
    echo '<!doctype html><html><body><h1>www3</h1></body></html>' | tee /var/www/html/index.html"

The above three commands will create three new virtual machines in the default zone we specified earlier. It will give them all the same tag which will let us reference these instances all at once. For example, to apply a firewall rule. It will also install Apache on the instances and give them a unique home page.

Next, we’ll be creating a firewall rule that will allow external traffic to the VM instances by using the following command –

gcloud compute firewall-rules create www-firewall-network-lb \
--target-tags network-lb-tag --allow tcp:80

Now, to verify that these instances are running, we will need their external IP addresses. Use the following command to find the IP addresses in the EXTERNAL_IP column:

gcloud compute instances list

You can also verify that each instance is running using the curl command –

curl http://[IP_ADDRESS]

Replace the [IP_ADDRESS] with the IP address for each of your VMs.

4. Configuring the load balancing service

We first need to create a static external IP address for our load balancer with the following command –

gcloud compute addresses create network-lb-ip-1 \
--region us-central1

Now we’ll be adding an HTTP health check resource –

gcloud compute http-health-checks create basic-check

Then, we’ll be adding a target pool in the same region as our instances. Use the following command to create the target pool and use the health check, which is required for the service to function.

gcloud compute target-pools create www-pool \
--region us-central1 --http-health-check basic-check

Next, add the instances to the pool –

gcloud compute target-pools add-instances www-pool ---instances www1, www2, www3

Finally, we’ll be adding a forwarding rule –

gcloud compute forwarding-rules create www-rule \
--region us-central1 \
--ports 80 \
--address network-lb-ip-1 \
--target-pool www-pool

5. Sending traffic to your instances

We have configured the load balancing service now. We can start sending our traffic to the forwarding rule and watch the traffic be dispersed to different instances. Below is the command to view the external IP address of the www-rule forwarded rule used by the load balancer:

gcloud compute forwarding-rules describe www-rule --region us-central1

Finally, we can use the curl command to access the external IP address. Just replace the IP_ADDRESS with the external IP you got from the previous command.

while true; do curl -m1 IP_ADDRESS; done

Now you can see that the response from the above command will be alternating randomly among the three instances. You can use Ctrl + c to stop running the command.

Okay, that’s it for now. I hope this article 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.