Mastering GSP313: Step-by-Step Guide to Implementing Load Balancing on Google Cloud Compute Engine.
Introduction
As 2024 came to an end, I took and passed the Google Cloud Associate Cloud Engineer (ACE) exam. The entire experience brought me into a new light in Cloud Engineering thanks to the GET Certified Program by Google Cloud Skills Boost.
Understanding the infrastructure on which the tools and technologies we use and build as Software Engineers rely goes a long way in contributing to the quality of the work we produce and a better understanding of the design of great software systems.
While going through the GET Certified Program, we were made to get our hands dirty with some required labs that must be passed to complete the program. One of the labs I found quite interesting and slightly challenging was the GSP313- Implement Load Balancing on Compute Engine: Challenge Lab.
In this article, I aim to provide a clear, actionable, and command-driven guide to solving the lab. I hope this can be of help to aspiring Google Cloud Associate Cloud Engineers, beginners, and those preparing for hands-on labs.
Overview of GSP313 Challenge Lab
Topics tested in the lab include:
Create an instance.
Create an HTTP load balancer in front of two web servers.
Prerequisites
Active Qwiklabs subscription or Google Cloud free trial
Credits on Google Cloud Skills Boost
Basic knowledge of Google Cloud CLI and Compute Engine.
Step-by-Step Solution
Task 1: Create a project jumphost instance
To get started, click on the “Activate Cloud Shell“ button towards the top right of the Google Cloud Console, then;
- Check that you’re in the right project:
gcloud config list project
- Or set your project:
gcloud config set project [PROJECT_ID]
- Set REGION and ZONE:
gcloud config set compute/region [REGION]
gcloud config set compute/zone [ZONE]
- Create the instance:
gcloud compute instances create [INSTANCE_NAME] --machine-type e2-micro
This completes Task 1, you can check your progress here.
Task 2: Set up an HTTP Load Balancer
Create an HTTP load balancer with a managed instance group of 2 Nginx web servers
- Copy and paste the script below to the terminal and press enter, this will create the `startup.sh` file and use the code to configure the web servers.
cat << EOF > startup.sh
#! /bin/bash
apt-get update
apt-get install -y nginx
service nginx start
sed -i -- 's/nginx/Google Cloud Platform - '"\$HOSTNAME"'/' /var/www/html/index.nginx-debian.html
EOF
Create an instance template: This command is to create an instance template with the following specifications:
The instances created from this template will use an e2-medium machine type (2 vCPUs and 4 GB of memory) as specified in the lab instructions.
The startup script (from
startup.sh
) will run on each instance at boot to configure it automatically.
gcloud compute instance-templates create [INSTANCE_TEMPLATE_NAME] --machine-type=e2-medium --metadata-from-file startup-script=startup.sh
Create a target pool for the nginx servers: The purpose of this command is to create a target pool that can:
Distribute incoming traffic among its member instances in a regional load balancer setup.
Perform health checks on the instances in the pool to ensure that only healthy instances receive traffic.
gcloud compute target-pools create [TARGET_POOL_NAME]
Create a managed instance group based on the template: This command creates a managed instance group:
It creates 2 VM (Virtual Machine) instances (
--size 2
) as specified in the lab instructions using a specified instance template.The instances in the group are assigned a base name (
—-base-instance-name
) ofnginx
.The group is automatically linked to a target pool for load balancing.
gcloud compute instance-groups managed create [INSTANCE_GROUP_NAME] --base-instance-name nginx --size 2 --template [INSTANCE_TEMPLATE_NAME] --target-pool [TARGET_POOL_NAME]
- Confirm that 2 additional instances have been created based on the instance template specified with this command. You should see 3 instances, the one we created in Task 1, and another 2 nginx instances created as the instance group.
gcloud compute instances list
- Create the firewall rule: Allow traffic on TCP port 80, which is typically used for HTTP traffic to flow into the Google Cloud instances. It's commonly used for configuring web servers or load balancers that need to accept HTTP traffic. Use the firewall rule name provided in the lab instructions (e.g. accept-tcp-rule-723)
gcloud compute firewall-rules create [FIREWALL_RULE_NAME] --allow tcp:80
- Create a forwarding rule: This directs incoming traffic to a specific target pool on port 80. The target pool is a collection of backend instances that will process the traffic.
gcloud compute forwarding-rules create [FORWARDING_RULE_NAME] --region [REGION] --ports=80 --target-pool [TARGET_POOL_NAME]
- Create a health check: The health check will regularly monitor the health of the backend instances (e.g., in an instance group) by making HTTP requests to them. If an instance fails the health check, it will not receive traffic until it passes the check again.
gcloud compute http-health-checks create [HEALTH_CHECK_NAME]
- Set named ports for the managed instance group: The purpose of this command is to configure a named port (
http:80
) for the specified managed instance group. Named ports are useful for identifying ports that the load balancer can use to direct traffic to backend instances in the group.
gcloud compute instance-groups managed \
set-named-ports [INSTANCE_GROUP_NAME] \
--named-ports http:80
- Create a backend service: This command creates a backend service that will be used in the HTTP(S) load balancer. The backend service specifies how traffic is distributed across backend resources (such as instance groups) and also includes a health check to ensure that the backend resources are healthy and able to handle requests.
gcloud compute backend-services create [BACKEND_SERVICE_NAME] --protocol HTTP --http-health-checks [HEALTH_CHECK_NAME] --global
- Add the instance group as the backend to the backend service group with the named port (http:80): This command adds an instance group to a backend service. The backend service then directs traffic to the instance group as part of the load-balancing process.
gcloud compute backend-services add-backend [BACKEND_SERVICE_NAME] --instance-group [INSTANCE_GROUP_NAME] --instance-group-zone [ZONE] --global
- Create a URL map: This defines how incoming HTTP(S) requests/traffic are routed to the default backend service. The URL map allows the HTTP(S) load balancer to inspect the URL of the incoming request and forward it to the appropriate backend service.
gcloud compute url-maps create [URL_MAP_NAME] --default-service [BACKEND_SERVICE_NAME]
- Create a target HTTP proxy to route requests to the URL map: This creates an HTTP target proxy and associates it with the URL map. The HTTP target proxy acts as a bridge between the frontend (the global forwarding rule) and the backend service. It uses the URL map to determine how incoming requests should be routed to the appropriate backend service.
gcloud compute target-http-proxies create [HTTP_TARGET_PROXY_NAME] --url-map [URL_MAP_NAME]
- Create a forwarding rule: This configures a global forwarding rule that directs incoming HTTP traffic on port 80 to the target HTTP proxy we’ve created. Essentially serving as the Load Balancer.
gcloud compute forwarding-rules create [FORWARDING_RULE_NAME] --global --target-http-proxy [HTTP_TARGET_PROXY_NAME] --ports 80
This completes Task 2 and the Lab as a whole. It may take from 5 to 7 minutes after running this command to get the score for this task.
Conclusion
I hope this article has been detailed enough to help you on your journey and further shed some light on some seemingly confusing terms if any.
See you on the next one. Cheers 🥂