Real-world Scenarios: Hands-on Labs
Deploy production-grade apps. Practice multi-tier deployments, scaling, monitoring, and cost optimization.
Lab 1: Deploy Web App with Auto-Scaling
# Create instance template
gcloud compute instance-templates create web-app-template \
--machine-type=n1-standard-1 \
--image-family=debian-11 \
--image-project=debian-cloud \
--metadata=startup-script='#!/bin/bash
apt-get update && apt-get install -y nodejs npm
cat > /app.js << EOF
const http = require("http");
http.createServer((req, res) => {
res.end("Hello from GCP! " + new Date());
}).listen(8080);
EOF
node /app.js &'
# Create managed instance group
gcloud compute instance-groups managed create web-app-ig \
--base-instance-name=web --template=web-app-template \
--size=3 --zone=us-central1-a
# Health check
gcloud compute health-checks create http web-health \
--global --port=8080
# Auto-scaling
gcloud compute instance-groups managed set-autoscaling web-app-ig \
--max-num-replicas=10 --min-num-replicas=3 \
--target-cpu-utilization=0.7 --zone=us-central1-a
# Load balancer
gcloud compute backend-services create web-backend \
--global --protocol=HTTP --health-checks=web-health
gcloud compute backend-services add-backend web-backend --global \
--instance-group=web-app-ig --instance-group-zone=us-central1-a
gcloud compute url-maps create web-lb --default-service=web-backend
gcloud compute target-http-proxies create web-proxy --url-map=web-lb
gcloud compute forwarding-rules create web-forwarding-rule \
--global --target-http-proxy=web-proxy --ports=80Lab 2: Add Database Layer
# Cloud SQL instance gcloud sql instances create web-app-db \ --database-version=POSTGRES_13 --tier=db-f1-micro \ --region=us-central1 --availability-type=REGIONAL # Create DB and user gcloud sql databases create app_db --instance=web-app-db gcloud sql users create app_user --instance=web-app-db \ --password=SecurePass123 # Enable private IP gcloud sql instances patch web-app-db --assign-ip # Connect from app via Cloud SQL Auth Proxy # gcloud cloud-sql-proxy -instances=PROJECT:us-central1:web-app-db=tcp:5432
Lab 3: Object Storage for Media
# Create bucket gsutil mb -l us-central1 gs://my-app-media # Upload file gsutil cp photo.jpg gs://my-app-media/ # Generate signed URL (1 hour) gsutil signurl ~/.ssh/key.json -d 1h gs://my-app-media/photo.jpg
Lab 4: Monitoring & Optimization
# View logs gcloud logging read "resource.type=gce_instance" --limit=50 # Query costs bq query --use_legacy_sql=false 'SELECT service.description, ROUND(SUM(cost), 2) as cost FROM `PROJECT.billing_export.gcp_billing_export_v1` WHERE EXTRACT(MONTH FROM usage_start_time) = EXTRACT(MONTH FROM CURRENT_DATE()) GROUP BY service.description ORDER BY cost DESC' # Auto-scaling metrics gcloud compute instance-groups managed describe web-app-ig --zone=us-central1-a
Real-world Project Template
E-commerce Platform
- Frontend: Cloud Storage + Cloud CDN (static assets)
- API: Cloud Run (serverless, scales to 0)
- Database: Cloud SQL (PostgreSQL, regional HA)
- Media: Cloud Storage (products, user uploads)
- Analytics: BigQuery (user behavior tracking)
- Monitoring: Cloud Logging + Trace
Interview Questions
Practical Scenarios
1. Create instance template with startup script. 2. Create managed instance group (3 zones, 3 instances). 3. Create health checks (port 8080, /health). 4. Set auto-scaling (min 3, max 10, target 70% CPU). 5. Create backend service + URL map. 6. Create HTTP proxy + forwarding rule. 7. Get external IP. 8. Test by hitting IP in browser. 9. Generate load to watch scaling. Time: 15-20 minutes for 3-zone HA app.
1. Check Cloud SQL CPU/memory in Console. 2. Enable Query Insights (top 100 slowest queries). 3. Add indexes on frequently queried columns. 4. Use Cloud SQL Auth Proxy (encrypted connection). 5. Consider read replicas for scaling reads. 6. Cache results (Memorystore Redis). Expected improvement: 5-10x faster.
1. Right-size instances (use Recommender). 2. Buy commitments (25-35% off). 3. Use preemptible (70% off). 4. Aggressive auto-scaling. 5. Scale down non-prod at night (50% off). 6. Archive old data (95% off). 7. Cloud CDN for static assets. Achievable: 40% cost reduction in 2-3 months.
1. Use signed URLs (time-limited access). 2. Disable public bucket read. 3. Encrypt at rest (default on GCP). 4. Use Cloud DLP to scan sensitive data. 5. Enable Cloud Audit Logs for all access. 6. CORS only for approved origins. Result: Secure, auditable file upload flow.
Common Deployment Patterns
Pattern 1: Canary Deployment
Roll out new version to 5% of users first. Monitor error rates. If good, increase gradually. If bad, rollback instantly. Zero-downtime, safe deployments.
Pattern 2: Blue-Green Deployment
Run two identical environments (Blue, Green). Deploy new version to Green. Test thoroughly. Switch load balancer to Green instantly. If issues, switch back to Blue (instant rollback).
Pattern 3: Cost Optimization
Combine: right-sizing + commitments + preemptible for non-critical + scheduled scaling + storage archiving. Typical result: 30-50% cost savings.
Summary
Real-world deployment requires:
- Multi-zone deployments for HA. Health checks catch failures automatically.
- Auto-scaling to handle spikes. Right-size instances to optimize costs.
- Database layer (Cloud SQL or BigQuery). Proper indexing is critical.
- Object storage (Cloud Storage) for media. Use signed URLs for security.
- Monitoring (Cloud Logging, Trace) to catch issues early.
- Cost optimization: 30-50% savings are achievable with proper design.