Step-by-Step Guide to Implement Load Balancing on Compute Engine


1. Create VM Instances
First, create multiple VM instances that will serve as the backend for your load balancer.
Command:
gcloud compute instances create instance-1 instance-2 \
--zone=us-central1-a \
--machine-type=e2-micro \
--tags=http-server
Expected Output:
Created [https://www.googleapis.com/compute/v1/projects/your-project/zones/us-central1-a/instances/instance-1].
Created [https://www.googleapis.com/compute/v1/projects/your-project/zones/us-central1-a/instances/instance-2].
2. Install and Configure Nginx
Create a startup script to install and configure Nginx on your instances.
Startup Script:
cat << EOF > startup.sh
#! /bin/bash
apt-get update
apt-get install -y nginx
service nginx start
EOF
3. Create an Instance Template
Use the startup script to create an instance template.
Command:
gcloud compute instance-templates create web-server-template \
--metadata-from-file startup-script=startup.sh \
--machine-type=e2-micro
Expected Output:
Created [https://www.googleapis.com/compute/v1/projects/your-project/global/instanceTemplates/web-server-template].
4. Create a Managed Instance Group
Create a managed instance group using the instance template.
Command:
gcloud compute instance-groups managed create web-server-group \
--base-instance-name web-server \
--template=web-server-template \
--size=2 \
--zone=us-central1-a
Expected Output:
Created [https://www.googleapis.com/compute/v1/projects/your-project/zones/us-central1-a/instanceGroupManagers/web-server-group].
5. Create a Firewall Rule
Allow traffic to your instances by creating a firewall rule.
Command:
gcloud compute firewall-rules create allow-http \
--allow tcp:80 \
--target-tags=http-server
Expected Output:
Creating firewall...done.
6. Create a Health Check
Create a health check to monitor the health of your instances.
Command:
gcloud compute health-checks create http http-basic-check \
--port 80
Expected Output:
Created [https://www.googleapis.com/compute/v1/projects/your-project/global/healthChecks/http-basic-check].
7. Create a Backend Service
Create a backend service and attach the managed instance group.
Command:
gcloud compute backend-services create web-backend-service \
--protocol HTTP \
--port-name http \
--health-checks http-basic-check \
--global
Expected Output:
Created [https://www.googleapis.com/compute/v1/projects/your-project/global/backendServices/web-backend-service].
8. Add Backend to Backend Service
Add the instance group to the backend service.
Command:
gcloud compute backend-services add-backend web-backend-service \
--instance-group=web-server-group \
--instance-group-zone=us-central1-a \
--global
Expected Output:
Updated [https://www.googleapis.com/compute/v1/projects/your-project/global/backendServices/web-backend-service].
9. Create a URL Map
Create a URL map to route incoming requests to the backend service.
Command:
gcloud compute url-maps create web-map-http \
--default-service web-backend-service
Expected Output:
Created [https://www.googleapis.com/compute/v1/projects/your-project/global/urlMaps/web-map-http].
10. Create a Target HTTP Proxy
Create a target HTTP proxy to route requests to the URL map.
Command:
gcloud compute target-http-proxies create http-lb-proxy \
--url-map web-map-http
Expected Output:
Created [https://www.googleapis.com/compute/v1/projects/your-project/global/targetHttpProxies/http-lb-proxy].
11. Create a Global Forwarding Rule
Create a global forwarding rule to route incoming requests to the target HTTP proxy.
Command:
gcloud compute forwarding-rules create http-content-rule \
--global \
--target-http-proxy=http-lb-proxy \
--ports=80
Expected Output:
Created [https://www.googleapis.com/compute/v1/projects/your-project/global/forwardingRules/http-content-rule].
Conclusion
By following these steps, you have successfully set up load balancing on Google Cloud's Compute Engine. This setup ensures that your application can handle increased traffic and provides high availability.
If you have any questions or need further assistance, feel free to ask!
Subscribe to my newsletter
Read articles from Vikas Surve directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Vikas Surve
Vikas Surve
I am an ๐ ๐ฆ ๐๐ฒ๐ฟ๐๐ถ๐ณ๐ถ๐ฒ๐ฑ ๐๐ฒ๐๐ข๐ฝ๐ ๐๐ป๐ด๐ถ๐ป๐ฒ๐ฒ๐ฟ ๐๐ ๐ฝ๐ฒ๐ฟ๐ and ๐๐๐๐ฟ๐ฒ ๐๐ฑ๐บ๐ถ๐ป๐ถ๐๐๐ฟ๐ฎ๐๐ผ๐ฟ ๐๐๐๐ผ๐ฐ๐ถ๐ฎ๐๐ฒ with over ๐ญ๐ฌ ๐๐ฒ๐ฎ๐ฟ๐ ๐ผ๐ณ ๐ฒ๐ ๐ฝ๐ฒ๐ฟ๐ถ๐ฒ๐ป๐ฐ๐ฒ in designing, implementing, and optimizing DevOps solutions. My expertise includes ๐๐/๐๐ ๐ฎ๐๐๐ผ๐บ๐ฎ๐๐ถ๐ผ๐ป ๐๐๐ถ๐ป๐ด ๐๐ถ๐๐๐ฎ๐ฏ, ๐๐ฒ๐ป๐ธ๐ถ๐ป๐, ๐ฎ๐ป๐ฑ ๐๐๐๐ฟ๐ฒ ๐๐ฒ๐๐ข๐ฝ๐, as well as ๐๐ผ๐ป๐๐ฎ๐ถ๐ป๐ฒ๐ฟ ๐ผ๐ฟ๐ฐ๐ต๐ฒ๐๐๐ฟ๐ฎ๐๐ถ๐ผ๐ป ๐๐ถ๐๐ต ๐๐ผ๐ฐ๐ธ๐ฒ๐ฟ ๐ฎ๐ป๐ฑ ๐๐๐ฏ๐ฒ๐ฟ๐ป๐ฒ๐๐ฒ๐. ๐น ๐๐ ๐ฝ๐ฒ๐ฟ๐ ๐ถ๐ป ๐ฑ๐ฒ๐๐ถ๐ด๐ป๐ถ๐ป๐ด ๐ฎ๐ป๐ฑ ๐บ๐ฎ๐ป๐ฎ๐ด๐ถ๐ป๐ด ๐ฒ๐ป๐ฑ-๐๐ผ-๐ฒ๐ป๐ฑ ๐๐/๐๐ ๐ฝ๐ถ๐ฝ๐ฒ๐น๐ถ๐ป๐ฒ๐ ๐น ๐๐ฎ๐ป๐ฑ๐-๐ผ๐ป ๐ฒ๐ ๐ฝ๐ฒ๐ฟ๐ถ๐ฒ๐ป๐ฐ๐ฒ ๐๐ถ๐๐ต ๐๐๐๐ฟ๐ฒ, ๐๐๐ฏ๐ฒ๐ฟ๐ป๐ฒ๐๐ฒ๐ (๐๐๐ฆ), ๐ฎ๐ป๐ฑ ๐ง๐ฒ๐ฟ๐ฟ๐ฎ๐ณ๐ผ๐ฟ๐บ ๐ณ๐ผ๐ฟ ๐๐ฐ๐ฎ๐น๐ฎ๐ฏ๐น๐ฒ ๐ฑ๐ฒ๐ฝ๐น๐ผ๐๐บ๐ฒ๐ป๐๐ ๐น ๐ฃ๐ฎ๐๐๐ถ๐ผ๐ป๐ฎ๐๐ฒ ๐ฎ๐ฏ๐ผ๐๐ ๐ฎ๐๐๐ผ๐บ๐ฎ๐๐ถ๐ผ๐ป, ๐๐ฒ๐ฐ๐๐ฟ๐ถ๐๐, ๐ฎ๐ป๐ฑ ๐ฐ๐น๐ผ๐๐ฑ-๐ป๐ฎ๐๐ถ๐๐ฒ ๐๐ฒ๐ฐ๐ต๐ป๐ผ๐น๐ผ๐ด๐ถ๐ฒ๐ ๐ ๐ฆ๐ธ๐ถ๐น๐น๐ & ๐ง๐ผ๐ผ๐น๐ โ ๐๐ฒ๐๐ข๐ฝ๐ & ๐๐/๐๐: Azure DevOps, GitLab, Jenkins โ ๐๐น๐ผ๐๐ฑ & ๐๐ป๐ณ๐ฟ๐ฎ๐๐๐ฟ๐๐ฐ๐๐๐ฟ๐ฒ: Azure, AWS โ ๐๐ป๐ณ๐ฟ๐ฎ๐๐๐ฟ๐๐ฐ๐๐๐ฟ๐ฒ ๐ฎ๐ ๐๐ผ๐ฑ๐ฒ (๐๐ฎ๐): Terraform, Bicep โ ๐๐ผ๐ป๐๐ฎ๐ถ๐ป๐ฒ๐ฟ๐ & ๐ข๐ฟ๐ฐ๐ต๐ฒ๐๐๐ฟ๐ฎ๐๐ถ๐ผ๐ป: Docker, Kubernetes (AKS) โ ๐๐ผ๐ป๐ณ๐ถ๐ด ๐ ๐ฎ๐ป๐ฎ๐ด๐ฒ๐บ๐ฒ๐ป๐: PowerShell, Shell Scripting โ ๐ ๐ผ๐ป๐ถ๐๐ผ๐ฟ๐ถ๐ป๐ด & ๐ข๐ฏ๐๐ฒ๐ฟ๐๐ฎ๐ฏ๐ถ๐น๐ถ๐๐: Grafana, Prometheus, Azure Monitor โ ๐ฆ๐ฒ๐ฐ๐๐ฟ๐ถ๐๐ & ๐ก๐ฒ๐๐๐ผ๐ฟ๐ธ๐ถ๐ป๐ด: Load Balancers, Firewalls, ClusterIP โ ๐ข๐ฝ๐ฒ๐ฟ๐ฎ๐๐ถ๐ป๐ด ๐ฆ๐๐๐๐ฒ๐บ๐: Linux, Mac ๐ก ๐๐ฒ๐ ๐ฆ๐๐ฟ๐ฒ๐ป๐ด๐๐ต๐ โ ๐๐น๐ผ๐๐ฑ ๐๐ฟ๐ฐ๐ต๐ถ๐๐ฒ๐ฐ๐๐๐ฟ๐ฒ & ๐๐๐๐ผ๐บ๐ฎ๐๐ถ๐ผ๐ป โ Designing and managing scalable cloud solutions โ ๐๐/๐๐ & ๐๐ฒ๐๐ข๐ฝ๐ ๐๐ฒ๐ฎ๐ฑ๐ฒ๐ฟ๐๐ต๐ถ๐ฝ โ Implementing robust and automated software delivery pipelines โ ๐ง๐ฒ๐ฎ๐บ ๐๐ฒ๐ฎ๐ฑ๐ฒ๐ฟ๐๐ต๐ถ๐ฝ & ๐ ๐ฒ๐ป๐๐ผ๐ฟ๐๐ต๐ถ๐ฝ โ Leading a 5-member team, fostering collaboration and growth โ ๐ฆ๐ฒ๐ฐ๐๐ฟ๐ถ๐๐ & ๐๐ผ๐บ๐ฝ๐น๐ถ๐ฎ๐ป๐ฐ๐ฒ โ Ensuring cloud security, compliance, and best practices โ ๐ฃ๐ฟ๐ผ๐ฏ๐น๐ฒ๐บ ๐ฆ๐ผ๐น๐๐ถ๐ป๐ด & ๐ข๐ฝ๐๐ถ๐บ๐ถ๐๐ฎ๐๐ถ๐ผ๐ป โ Driving efficiency through automation and DevOps practices โ ๐๐ผ๐ป๐๐ถ๐ป๐๐ผ๐๐ ๐๐ฒ๐ฎ๐ฟ๐ป๐ถ๐ป๐ด & ๐๐ป๐ป๐ผ๐๐ฎ๐๐ถ๐ผ๐ป โ Exploring emerging technologies and best practices