0
0
GcpHow-ToBeginner · 4 min read

How to Set Up Load Balancer on GCP: Step-by-Step Guide

To set up a load balancer on GCP, create a backend service with your instances, configure a health check, and then create a forwarding rule with a target proxy. Use gcloud compute commands or the GCP Console to connect these components and distribute traffic evenly.
📐

Syntax

Setting up a load balancer on GCP involves these main parts:

  • Backend service: Groups your VM instances or instance groups.
  • Health check: Monitors instance health to route traffic only to healthy ones.
  • URL map and target proxy: Directs incoming requests to the backend service.
  • Forwarding rule: Listens on an IP and port to forward traffic to the target proxy.

Use gcloud compute commands to create and link these resources.

bash
gcloud compute health-checks create http HEALTH_CHECK_NAME --port=PORT

gcloud compute backend-services create BACKEND_SERVICE_NAME --protocol=PROTOCOL --health-checks=HEALTH_CHECK_NAME --global

gcloud compute url-maps create URL_MAP_NAME --default-service=BACKEND_SERVICE_NAME

gcloud compute target-http-proxies create TARGET_PROXY_NAME --url-map=URL_MAP_NAME

gcloud compute forwarding-rules create FORWARDING_RULE_NAME --global --target-http-proxy=TARGET_PROXY_NAME --ports=PORT --address=IP_ADDRESS
💻

Example

This example creates a simple HTTP load balancer that distributes traffic to a managed instance group.

bash
gcloud compute health-checks create http my-health-check --port=80

gcloud compute backend-services create my-backend-service --protocol=HTTP --health-checks=my-health-check --global

gcloud compute backend-services add-backend my-backend-service --instance-group=my-instance-group --instance-group-zone=us-central1-a --global

gcloud compute url-maps create my-url-map --default-service=my-backend-service

gcloud compute target-http-proxies create my-http-proxy --url-map=my-url-map

gcloud compute forwarding-rules create my-forwarding-rule --global --target-http-proxy=my-http-proxy --ports=80
Output
Created [https://www.googleapis.com/compute/v1/projects/PROJECT_ID/global/healthChecks/my-health-check]. Created [https://www.googleapis.com/compute/v1/projects/PROJECT_ID/global/backendServices/my-backend-service]. Added backend to [my-backend-service]. Created [https://www.googleapis.com/compute/v1/projects/PROJECT_ID/global/urlMaps/my-url-map]. Created [https://www.googleapis.com/compute/v1/projects/PROJECT_ID/global/targetHttpProxies/my-http-proxy]. Created [https://www.googleapis.com/compute/v1/projects/PROJECT_ID/global/forwardingRules/my-forwarding-rule].
⚠️

Common Pitfalls

  • Not creating or linking a health check causes the load balancer to send traffic to unhealthy instances.
  • Forgetting to add the instance group to the backend service means no traffic reaches your instances.
  • Using regional resources incorrectly with global load balancers can cause errors.
  • Not opening firewall ports (like 80 or 443) blocks incoming traffic.
bash
## Wrong: Missing health check

gcloud compute backend-services create my-backend-service --protocol=HTTP --global

## Right: Include health check

gcloud compute health-checks create http my-health-check --port=80

gcloud compute backend-services create my-backend-service --protocol=HTTP --health-checks=my-health-check --global
📊

Quick Reference

StepCommand or ActionDescription
1Create health checkMonitors instance health on specified port
2Create backend serviceGroups instances and links health check
3Add backendAttach instance group to backend service
4Create URL mapRoutes requests to backend service
5Create target proxyHandles incoming requests using URL map
6Create forwarding ruleListens on IP and port to forward traffic

Key Takeaways

Always create and link a health check to your backend service to ensure traffic goes to healthy instances.
Add your instance groups to the backend service to enable load balancing.
Use global resources for HTTP(S) load balancers and ensure firewall rules allow traffic on required ports.
Follow the sequence: health check → backend service → URL map → target proxy → forwarding rule.
Test your load balancer after setup to confirm traffic is distributed correctly.