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

Vikas SurveVikas Surve
3 min read

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!

0
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