How to Configure Load Balancer on GCP: Step-by-Step Guide
To configure a load balancer on GCP, create a backend service with instance groups, set up a URL map and target proxy, then create a forwarding rule with a public IP. Use
gcloud compute commands or the GCP Console to define these components and distribute traffic evenly.Syntax
Configuring a load balancer on GCP involves these main parts:
- Instance Group: A group of VM instances serving your app.
- Backend Service: Connects instance groups to the load balancer.
- URL Map: Routes incoming requests to backend services.
- Target Proxy: Handles HTTP(S) requests and forwards them.
- Forwarding Rule: Listens on an IP and port to send traffic to the proxy.
Use gcloud compute commands to create each part in order.
bash
gcloud compute instance-groups managed create [INSTANCE_GROUP_NAME] --zone=[ZONE] --template=[INSTANCE_TEMPLATE] --size=[SIZE] gcloud compute health-checks create http [HEALTH_CHECK_NAME] --port 80 gcloud compute backend-services create [BACKEND_SERVICE_NAME] --protocol=HTTP --port-name=http --health-checks=[HEALTH_CHECK_NAME] --global gcloud compute backend-services add-backend [BACKEND_SERVICE_NAME] --instance-group=[INSTANCE_GROUP_NAME] --instance-group-zone=[ZONE] --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=80 --address=[IP_ADDRESS]
Example
This example creates a simple HTTP load balancer that distributes traffic to a managed instance group.
bash
gcloud compute instance-groups managed create my-instance-group --zone=us-central1-a --template=my-instance-template --size=2 gcloud compute health-checks create http my-health-check --port 80 gcloud compute backend-services create my-backend-service --protocol=HTTP --port-name=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 --address=0.0.0.0
Output
Created [https://www.googleapis.com/compute/v1/projects/PROJECT_ID/zones/us-central1-a/instanceGroups/my-instance-group]
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
Common mistakes when configuring GCP load balancers include:
- Not creating a health check or misconfiguring it, causing backends to be marked unhealthy.
- Forgetting to add the instance group to the backend service.
- Using regional resources with global load balancer components incorrectly.
- Not reserving a static IP address before creating the forwarding rule.
Always verify each step and resource scope matches the load balancer type.
bash
## Wrong: Missing health check causes backend to be unhealthy
gcloud compute backend-services create my-backend-service --protocol=HTTP --port-name=http --global
## Right: Create health check and attach it
gcloud compute health-checks create http my-health-check --port 80
gcloud compute backend-services create my-backend-service --protocol=HTTP --port-name=http --health-checks=my-health-check --globalQuick Reference
Tips for configuring GCP load balancers:
- Use
--globalflag for HTTP(S) load balancers. - Reserve a static IP with
gcloud compute addresses createbefore forwarding rule. - Health checks must match backend service ports.
- Instance groups must be in the same region/zone as specified.
Key Takeaways
Create instance groups and health checks before backend services.
Use global resources for HTTP(S) load balancers and regional for others.
Always attach health checks to backend services to ensure traffic only goes to healthy instances.
Reserve static IP addresses before creating forwarding rules for stable endpoints.
Verify all resource zones and regions align with load balancer requirements.