Project 1: Highly Available Three-Tier Web App With Global Load Balancer, Managed Instance Group and Cloud SQL
What You Will Build
text
Users ─► Global external Application LB (one anycast IP, health checks)
└─► Regional MIG (3 VMs across zones, autoscaling, no public IPs)
└─► Cloud SQL PostgreSQL (HA, private IP)
Private VMs reach the internet through Cloud NAT. Admins use IAP, not public SSH.
This project costs money Cloud SQL HA, load balancer and Cloud NAT charge by the hour. Finish in one sitting and run the clean-up section.
Step 1: Project and Network
bash
export PROJECT=my-lab-482 REGION=asia-south1
gcloud config set project $PROJECT
gcloud services enable compute.googleapis.com sqladmin.googleapis.com servicenetworking.googleapis.com secretmanager.googleapis.com
gcloud compute networks create lab-vpc --subnet-mode=custom
gcloud compute networks subnets create app-subnet --network=lab-vpc --region=$REGION --range=10.30.0.0/24 --enable-private-ip-google-access
gcloud compute routers create lab-router --network=lab-vpc --region=$REGION
gcloud compute routers nats create lab-nat --router=lab-router --region=$REGION --auto-allocate-nat-external-ips --nat-all-subnet-ip-ranges
gcloud compute firewall-rules create lab-allow-iap --network=lab-vpc --allow=tcp:22 --source-ranges=35.235.240.0/20
gcloud compute firewall-rules create lab-allow-lb --network=lab-vpc --allow=tcp:8080 --source-ranges=130.211.0.0/22,35.191.0.0/16 --target-tags=web
Step 2: Private Cloud SQL
bash
gcloud compute addresses create google-managed-range --global --purpose=VPC_PEERING --prefix-length=20 --network=lab-vpc
gcloud services vpc-peerings connect --service=servicenetworking.googleapis.com --ranges=google-managed-range --network=lab-vpc
gcloud sql instances create lab-db --database-version=POSTGRES_16 --edition=ENTERPRISE --tier=db-custom-1-3840 --region=$REGION \
--availability-type=REGIONAL --network=lab-vpc --no-assign-ip --root-password=CHANGE-ME-NOW
gcloud sql databases create appdb --instance=lab-db
gcloud sql instances describe lab-db --format="get(ipAddresses[0].ipAddress)"
printf 'CHANGE-ME-NOW' | gcloud secrets create db-password --data-file=-
Step 3: Service Account, Template and MIG
bash
gcloud iam service-accounts create web-sa
gcloud secrets add-iam-policy-binding db-password --member="serviceAccount:web-sa@$PROJECT.iam.gserviceaccount.com" --role=roles/secretmanager.secretAccessor
gcloud projects add-iam-policy-binding $PROJECT --member="serviceAccount:web-sa@$PROJECT.iam.gserviceaccount.com" --role=roles/logging.logWriter
gcloud projects add-iam-policy-binding $PROJECT --member="serviceAccount:web-sa@$PROJECT.iam.gserviceaccount.com" --role=roles/monitoring.metricWriter
cat > startup.sh <<'EOF'
#!/bin/bash
apt-get update && apt-get install -y python3-flask python3-psycopg2
DB_IP="REPLACE_WITH_DB_IP"
cat > /opt/app.py <<'PY'
import os, socket, flask
app = flask.Flask(__name__)
@app.route("/")
def home():
return "Served by %s (zone via metadata)\n" % socket.gethostname()
@app.route("/health")
def health():
return "ok"
app.run(host="0.0.0.0", port=8080)
PY
nohup python3 /opt/app.py > /var/log/app.log 2>&1 &
EOF
gcloud compute instance-templates create lab-tpl --machine-type=e2-small --image-family=debian-12 --image-project=debian-cloud \
--subnet=app-subnet --region=$REGION --no-address --tags=web --service-account=web-sa@$PROJECT.iam.gserviceaccount.com --scopes=cloud-platform \
--metadata-from-file=startup-script=startup.sh
gcloud compute health-checks create http lab-hc --port=8080 --request-path=/health
gcloud compute instance-groups managed create lab-mig --region=$REGION --template=lab-tpl --size=3 --health-check=lab-hc --initial-delay=180
gcloud compute instance-groups managed set-named-ports lab-mig --region=$REGION --named-ports=http:8080
gcloud compute instance-groups managed set-autoscaling lab-mig --region=$REGION --min-num-replicas=3 --max-num-replicas=6 --target-cpu-utilization=0.6
The startup script is simplified It shows the web tier and a health check. Add the database connection code with the Cloud SQL Python connector to complete the three tiers.
Step 4: Global Load Balancer
bash
gcloud compute backend-services create lab-backend --global --protocol=HTTP --port-name=http --health-checks=lab-hc
gcloud compute backend-services add-backend lab-backend --global --instance-group=lab-mig --instance-group-region=$REGION
gcloud compute url-maps create lab-map --default-service=lab-backend
gcloud compute target-http-proxies create lab-proxy --url-map=lab-map
gcloud compute addresses create lab-ip --global
gcloud compute forwarding-rules create lab-fr --global --address=lab-ip --target-http-proxy=lab-proxy --ports=80
gcloud compute addresses describe lab-ip --global --format="get(address)"
Step 5: Test Like an Engineer
| Test | Expected result |
|---|---|
| Open the load balancer IP (wait a few minutes) and refresh | Different host names appear |
| Delete one VM in the group | Site stays up. MIG recreates the VM |
Stop the app on one VM (pkill -f app.py via IAP SSH) | Health check fails, VM is recreated |
Run gcloud sql instances failover lab-db | Short interruption, database comes back |
Generate CPU load with stress on a VM | Autoscaler adds VMs |
| Try to SSH from the internet | Fails: no public IP. Use --tunnel-through-iap |
Step 6: Monitoring
- Create an uptime check for the load balancer IP
- Alert on backend 5xx rate and Cloud SQL CPU
- Build one dashboard with LB requests, MIG size and database CPU
Clean Up
bash
gcloud compute forwarding-rules delete lab-fr --global --quiet
gcloud compute target-http-proxies delete lab-proxy --quiet
gcloud compute url-maps delete lab-map --quiet
gcloud compute backend-services delete lab-backend --global --quiet
gcloud compute instance-groups managed delete lab-mig --region=$REGION --quiet
gcloud compute instance-templates delete lab-tpl --quiet
gcloud sql instances delete lab-db --quiet
gcloud compute routers nats delete lab-nat --router=lab-router --region=$REGION --quiet
gcloud compute addresses delete lab-ip --global --quiet
# or simply delete the whole lab project
gcloud projects delete $PROJECT
Review Against the Pillars
| Pillar | What you did |
|---|---|
| Security | No public IPs, IAP access, private database, secret in Secret Manager, least-privilege service account |
| Reliability | Regional MIG, autohealing, global LB, HA database |
| Performance | Load balancer, can add CDN and cache |
| Cost | Small machines, autoscaling, clean-up |
| Operations | Health checks, alerts, can codify in Terraform |
Conclusion
You built a highly available three-tier design: global load balancer, regional MIG without public IPs and a private HA Cloud SQL database. Rebuild it with Terraform as the next step.
Quick Summary: plan; network; database; MIG; load balancer; tests; monitoring; cleanup; review.

