Overview and What You Will Learn
Google Cloud does not have one load balancer, it has a family. To choose, answer three questions: who calls the service (the public internet or only your VPC), what protocol it speaks (HTTP(S) or raw TCP/UDP), and whether traffic should be served globally or inside one region. The wrong answer is a security problem as well as a performance one, because it can expose an internal service to the internet.
In this lab you will:
- Run a small web tier and put a global external Application Load Balancer in front of it
- Put an internal passthrough Network Load Balancer in front of a second copy of the tier
- Prove from a client VM and from your laptop that one is public and one is private
- Use a decision table to map any workload to the right type
Why This Matters in Production
A team builds an internal microservice that only other services should call, but puts it behind an external load balancer because that is the tutorial they followed. It sits on the public internet until a security review finds it months later. A platform team at a company like Shopify or Grab makes the load balancer type part of the design review, because the scheme (external or internal) is what decides who can reach the service.
Core Principles
GCP load balancers differ on two axes: the layer (Application load balancers work at HTTP(S), Network load balancers at TCP/UDP) and the reach (external to the internet, or internal to a VPC). Reach can be global or regional for external types, and is regional for internal types.
the service?} -->|Internet| E{Protocol?} Q -->|Only the VPC| I{Protocol?} E -->|HTTP or HTTPS| EA([External Application LB
global or regional]) E -->|TCP or UDP| EN([External Network LB]) I -->|HTTP or HTTPS| IA([Internal Application LB]) I -->|TCP or UDP| IN([Internal passthrough
Network LB]) classDef q fill:#DBEAFE,color:#1E293B classDef pub fill:#F97316,color:#FFFFFF classDef priv fill:#10B981,color:#FFFFFF class Q,E,I q class EA,EN pub class IA,IN priv
| Type | Best for |
|---|---|
| Global external Application LB | Public web apps, one anycast IP, users routed to the nearest healthy region |
| Regional external Application LB | Public web apps that must stay in one region for compliance or cost |
| External passthrough Network LB | Public TCP or UDP services that need the original client IP |
| Internal Application LB | Private HTTP(S) between microservices in a VPC |
| Internal passthrough Network LB | Private TCP or UDP services in a VPC |
Google has renamed these products over time. Older material calls them HTTP(S), TCP Proxy and Network load balancers, so match on the scheme (external or internal) and layer, not just the name. Global reach uses Premium Network Tier, and regional external load balancers can use Standard Tier.
Detailed Step-by-Step Practical Lab
Milestone 1: Create the Project and a Web Tier
You will build two identical managed instance groups from one template: one for the public load balancer and one for the internal one. Using separate groups avoids sharing a backend between incompatible load balancer types.
export PROJECT_ID=lb-lab-$(date +%s | tail -c 6)export REGION=asia-south1export ZONE=asia-south1-a gcloud projects create $PROJECT_ID --name="Load Balancer Lab"gcloud config set project $PROJECT_IDgcloud billing projects link $PROJECT_ID --billing-account=0A1B2C-3D4E5F-6A7B8Cgcloud services enable compute.googleapis.com gcloud compute instance-templates create web-template \ --machine-type=e2-small \ --image-family=debian-12 \ --image-project=debian-cloud \ --tags=web \ --metadata=startup-script='#! /bin/bashapt-get updateapt-get install -y nginxecho "served by $(hostname)" > /var/www/html/index.html' gcloud compute instance-groups managed create web-mig-public \ --template=web-template --size=2 --zone=$ZONE gcloud compute instance-groups managed create web-mig-internal \ --template=web-template --size=2 --zone=$ZONE gcloud compute instance-groups set-named-ports web-mig-public \ --named-ports=http:80 --zone=$ZONEThe VMs install nginx at boot, so they need outbound internet access. Wait about two minutes for the startup script to finish.
Milestone 2: Allow Health Check Traffic
Load balancer health checkers come from Google ranges, and the default firewall blocks them. Without this rule the backends are reported unhealthy and the load balancer returns errors.
gcloud compute firewall-rules create allow-health-checks \ --network=default \ --direction=INGRESS \ --allow=tcp:80 \ --source-ranges=130.211.0.0/22,35.191.0.0/16 \ --target-tags=webMilestone 3: Build a Global External Application Load Balancer
This public load balancer has a single global IP address, a health check, a backend service, a URL map, a proxy and a forwarding rule.
gcloud compute addresses create lb-ip-public --ip-version=IPV4 --global gcloud compute health-checks create http hc-web --port=80 --request-path=/ gcloud compute backend-services create be-web-public \ --load-balancing-scheme=EXTERNAL_MANAGED \ --protocol=HTTP \ --port-name=http \ --health-checks=hc-web \ --global gcloud compute backend-services add-backend be-web-public \ --instance-group=web-mig-public \ --instance-group-zone=$ZONE \ --global gcloud compute url-maps create lb-map-public --default-service=be-web-public gcloud compute target-http-proxies create lb-proxy-public --url-map=lb-map-public gcloud compute forwarding-rules create fr-public \ --load-balancing-scheme=EXTERNAL_MANAGED \ --network-tier=PREMIUM \ --address=lb-ip-public \ --global \ --target-http-proxy=lb-proxy-public \ --ports=80# Test from anywhere. Allow a few minutes for the load balancer to provisionexport LB_IP=$(gcloud compute addresses describe lb-ip-public --global --format='value(address)')curl -s http://$LB_IP# served by web-mig-public-abcdTipIf you get a 502 or "no healthy upstream", check the health check firewall rule and the backend health with
gcloud compute backend-services get-health be-web-public --global.
Milestone 4: Build an Internal Passthrough Network Load Balancer
An internal passthrough load balancer has no proxy and no public address. It gets a private IP from a subnet, and is reachable only from the VPC and networks connected to it.
gcloud compute health-checks create tcp hc-internal --port=80 gcloud compute backend-services create be-internal \ --load-balancing-scheme=INTERNAL \ --protocol=TCP \ --health-checks=hc-internal \ --region=$REGION gcloud compute backend-services add-backend be-internal \ --instance-group=web-mig-internal \ --instance-group-zone=$ZONE \ --region=$REGION gcloud compute forwarding-rules create fr-internal \ --load-balancing-scheme=INTERNAL \ --network=default \ --subnet=default \ --region=$REGION \ --ip-protocol=TCP \ --ports=80 \ --backend-service=be-internal \ --backend-service-region=$REGIONThe key difference is --load-balancing-scheme=INTERNAL together with a subnet. Nothing about this setup has a public address.
Milestone 5: Prove the Difference in Reachability
Compare what each load balancer exposes, then call the internal one from inside and outside the VPC.
gcloud compute forwarding-rules list \ --format="table(name, IPAddress, loadBalancingScheme, region.basename())" export ILB_IP=$(gcloud compute forwarding-rules describe fr-internal \ --region=$REGION --format='value(IPAddress)') # From your laptop: this should time out, because the address is privatecurl -s --max-time 5 http://$ILB_IP || echo "not reachable from the internet" # From a client VM inside the VPC: this worksgcloud compute instances create client-vm \ --zone=$ZONE --machine-type=e2-micro \ --image-family=debian-12 --image-project=debian-cloud gcloud compute ssh client-vm --zone=$ZONE \ --command="curl -s http://$ILB_IP"# served by web-mig-internal-wxyzMilestone 6: Clean Up
Forwarding rules and addresses cost money while they exist. Deleting the project removes everything created in this lab.
gcloud projects delete $PROJECT_ID --quietProduction Best Practices & Common Pitfalls
- Decide reach before anything else. If a service must never be public, only use internal load balancer schemes for it.
- Use the global external Application LB for public web apps. One anycast IP, TLS termination and routing to the closest healthy backend.
- Use regional types when data must stay in a region. They trade global reach for locality.
- Use passthrough Network LBs when you need original client IPs or non-HTTP protocols.
- Always allow health check ranges.
130.211.0.0/22and35.191.0.0/16must be allowed to reach backends on the health check port. - Match health checks to the backend. Use HTTP checks on a path for web apps and TCP checks to confirm a port is open.
- Protect public endpoints. Put Cloud Armor and TLS in front of external load balancers.
Common MistakePutting an internal-only service behind an external load balancer. It becomes reachable from the internet. For anything that should stay private, confirm the forwarding rule uses the INTERNAL scheme and has no external address.
TipName resources with their scope, such as
fr-internal-mumbaiorfr-public-global, so a review ofgcloud compute forwarding-rules listshows exposure at a glance.
RememberInternal means reachable from the VPC and anything connected to it by peering, VPN or Interconnect, not only from the same subnet.
Quick Reference & Troubleshooting Commands
| Command | Description |
|---|---|
gcloud compute addresses create --global |
Reserve a global external IP |
gcloud compute backend-services create --load-balancing-scheme=INTERNAL |
Create an internal backend service |
gcloud compute health-checks create http |
Create an HTTP health check |
gcloud compute forwarding-rules list |
Show every load balancer entry point |
gcloud compute backend-services get-health NAME |
Show backend health |
| Symptom | Likely cause and fix |
|---|---|
| 502 from the external LB | Backends unhealthy, allow health check ranges and check the app |
| Backends show UNHEALTHY | Wrong port or path in the health check, or the firewall is blocking |
| Internal LB times out from another region | Global access is off, enable it on the forwarding rule |
| Named port error on add-backend | Instance group missing the named port, run set-named-ports |