Joel Dorrington

(514) 601-1122

joel.dorrington0@gmail.com

Recent Posts

Google Cloud HTTP Load Balancers can be thought of as 2 components, frontends and backends. Our frontend constists of a simple static IP address and a forwarding rule which points to a backend Service.

The backend service combines multiple targets into a single access point. It requires a regional health check. The targets can be a list of managed instance groups (MIG) or target pools. I’ll focus on MIGs here.

MIGs create and manage the lifecycle of one or more VM instances. It uses a health check for self healing, which can be the same one used for the load balancer. It uses instance templates to create new VMs for autoscaling and rolling update features too.

Creating a Google Cloud HTTP Load Balancer

The overal process to create a MIG and hook it up to a load balancer goes like this:

  1. Create the instance template
  2. Create the health check
  3. Open the firewall for HTTP on port 80
  4. Create the instance group
  5. Create the backend service
  6. Add the instance group to the backend service
  7. Create the frontend

Create the instance template

In this step I used one simple command to combine a disk image with a VM type and added a network tag for the firewall rule to target the resulting machines.

gcloud compute instance-templates create joels-nginx --image=joels-nginx --machine-type e2-micro --tags=http-80

This command created a new instance template from a disk I had created previously for an NGINX web-server.

Create the health check

We need to create a regional http health check for the MIG and the backend service. This is easy with the following command. Providing the region argument ensures the healthcheck will be regional and not global.

gcloud compute health-checks create http http-check-montreal --region=northamerica-northeast1

Open the firewall

We need to open the firewall on port 80 for our nginx instances to allow the health checks to access the instances.

gcloud compute firewall-rules create http --allow=tcp:80 --target-tags=http-80

Create the instance group

Finally we get to actually deploy the service in a managed instance group.

gcloud compute instance-groups managed create joels-nginx --template=joels-nginx --health-check=http-check-montreal --size=1

Now our managed instance group is set up to self heal. Autoscaling can be configured now or later. It’s out of scope for this post.

Create the backend service

We need to create the backend service as a load balancer target with our MIG as the destination. This is a two step process.

gcloud compute backend-services create joels-nginx --health-checks-region=northamerica-northeast1 --health-checks=http-check-montreal --network=default
gcloud compute backend-services add-backend joels-nginx --region=northamerica-northeast1 --instance-group=joels-nginx

Create the frontend

The frontend of our load balancer is also a simple two step process.

gcloud compute addresses create joels-nginx
gcloud compute forwarding-rules create joels-nginx --address=joels-nginx --backend-service=joels-nginx

After that forwarding rule has been created, the load balancer is complete and the dynamically managed NGINX server instances are accessible via the external static IP we created.