HTTP Load Balancer with Google Cloud Platform

A load balancer improves the distribution of work load by redirecting the request to multiple nodes. Load balancing allows to maximize throughput, minimize response time, and increase reliability and availability of the resources. Let us configure a HTTP Loadbalancer in Google Cloud Platform (GCP) for a single Region.

The Experiment

We are going to configure an HTTP Loadbalancer with 2 Centos Linux nodes. Both nodes are configured with index.html file which has respective node’s hostname as content. So each time you refresh the web browser you should get different content with loadbalancer. Some of the default configuration parameters are not explained here to make this document short. Also, you may use your own configuration parameters.

Cloud Platform : GCP 
Virtual Machine OS : Centos 6
Number of Nodes (VM) : 2
Region: Mumbai
Role : Webserver
Machine type : f1.micro

Steps Involved are,

  1. Create a Health check

2) Create instance template

3) Create instance group for multiple Virtual Machines (VM)

4) Create HTTP Load Balancer

4-a) Configure Load Balancer Backend

4-b) Configure Load Balancer Frontend

  1. Create a Health check: Create the health check in order to monitor the web nodes are available for service. From GCP Navigation menu icon Choose Compute Engine and select Health checks. Click Create a Health check. Enter a name for the health check. I have used a name gir-healthcheck-1. You can enter /index.html as the Request path. Click Create. It looks like below.

2) Create instance template: Instance template is a saved configuration of VM instance which can be referred by a name. We will use this later in the Instance group configuration to create multiple VMs with identical configuration. In the Compute Engine console select Instance templates and CREATE INSTANCE TEMPLATE in the right window pane. I have assigned gir-template-1 as its name, Machine type f1.micro for Centos 6. Scroll down select Management and enter a startup script with below contents.

#!/bin/bash
yum install -y httpd
setenforce 0 (Refer any SELinux document to make this permanent)
echo `hostname` > /var/www/html/index.html
service httpd start
chkconfig httpd on

Now you select Networking in the Above Menu and enter you firewall rule tag name as Network tags. How to create firewall rule refer this link. Also note that once you create instance template you cannot edit. Click Create.

3) Create Instance Group: Instance group creates can multiple VMs with identical configuration. Once index.html is created it contains the respective node’s hostname due to above startup script. In the Compute Engine Menu click Instance groups and click Create instance group. I have given the name Instance-group-1. Enter Instance template name, set autoscaling off , Number of instances to 2 and assign the health check you had created. Click Create.

4) Create HTTP Load balancer: Click The Navigation Menu and then Load balancing from Networking service.

Click Create a Loadbalancer. Under HTTP(S) Load Balancing click Start configuration and enter a name for it.

4-a) Configure Load Balancer Backend : Click Backend configuration-> backend services -> Create a backend service.

Assign a name and select the instance group you had created. Click done. Select the health check and click Create .

4-b) Configure Load Balancer Frontend: Click Frontend Configuration, enter a name for it and click done with other default parameters intact. In the left pane click Create.This completes the configuration of Load balancer.

Testing The configuration

Wait for a few minutes. Note down public IP address under the Frond end and copy to the web browser. you can see hostname of one of the nodes and by refreshing the browser you cloud see second node’s hostname. netstat returns public IP not of your client’s.

Disclaimer: This experiment is accomplished in test environment. You are requested to verify your setup thoroughly before you implement in a production environment. Some of the screenshots may not reflect exactly as the original GCP web console due to mangled copying and joining of multiple screenshots to form a single one. You may have to refer the original GCP console.

Publication Link: