BeginnerLesson 2 of 12

Compute Services

Master Compute Engine VMs, Instance Groups, App Engine, Cloud Functions, and GKE for every application pattern.

Simple Explanation (ELI5)

GCP offers many ways to run code. Compute Engine lets you rent VMs and customize everything (like buying a laptop but in the cloud). Instance Groups let you manage multiple VMs and auto-scale. App Engine is simpler �" you just upload code and it handles servers automatically. Cloud Functions run single functions in response to events (serverless). GKE runs Kubernetes containers if you need orchestration. Pick the right tool based on control vs convenience.

Why Do We Need Different Compute Services?

Technical Explanation

1. Compute Engine (IaaS)

Virtual machines you fully control. Choose machine type, OS, storage, networking, firewall rules, and monitoring. You manage patches and security.

bash
# Create a VM
gcloud compute instances create my-vm \
  --zone us-central1-a \
  --machine-type n1-standard-1 \
  --image-family debian-11 \
  --image-project debian-cloud

# SSH into the VM
gcloud compute ssh my-vm --zone us-central1-a

# Stop the VM (you still pay for disk)
gcloud compute instances stop my-vm --zone us-central1-a

# Delete the VM
gcloud compute instances delete my-vm --zone us-central1-a

2. Machine Types

Machine TypevCPUMemoryUse Case
n1-standard-113.75 GBLight dev/test workloads
n1-standard-4415 GBSmall production apps
n2-standard-8832 GBMedium production workloads
n2d-highcpu-3232128 GBCPU-intensive, big data

3. Instance Groups

Managed or unmanaged groups of VMs with auto-scaling and load balancing:

bash
# Create a managed instance group template
gcloud compute instance-templates create my-template \
  --machine-type n1-standard-1 \
  --image-family debian-11

# Create a managed instance group with auto-scaling
gcloud compute instance-groups managed create my-ig \
  --base-instance-name my-instance \
  --template my-template \
  --size 3 \
  --zone us-central1-a

# Set auto-scaling policy
gcloud compute instance-groups managed set-autoscaling my-ig \
  --max-num-replicas 10 \
  --min-num-replicas 2 \
  --zone us-central1-a

4. App Engine (PaaS)

Upload your app code, GCP handles infrastructure. Choose Standard (cheaper, simpler limits) or Flexible (more control).

bash
# Deploy a Python app
gcloud app deploy

# View logs
gcloud app logs read -n 50

# Set traffic to new version
gcloud app services set-traffic default --splits v2=1

5. Cloud Functions (Serverless)

Run code triggered by events: HTTP requests, Cloud Storage uploads, Pub/Sub messages, etc. Pay strictly for execution time.

bash
gcloud functions deploy my-function \
  --runtime python39 \
  --trigger-topic my-topic \
  --allow-unauthenticated

6. GKE (Kubernetes)

Managed Kubernetes. GCP handles the control plane; you manage worker nodes and container deployments.

bash
# Create a GKE cluster
gcloud container clusters create my-cluster \
  --zone us-central1-a \
  --num-nodes 3

# Connect kubectl
gcloud container clusters get-credentials my-cluster --zone us-central1-a

# Deploy an app
kubectl apply -f deployment.yaml

Compute Services Comparison

ServiceControlScalabilityCostBest For
Compute EngineFullManual or groupsPer VM/hourCustom needs, long-running
Instance GroupsModerateAuto-scalePer VM/hourPredictable traffic, HA
App EngineLowAutomaticPer request + CPU/memoryRapid deployment
Cloud FunctionsVery lowAutomaticPer 100ms executionEvent-driven, low usage
GKEHighAutomatic (pods)Per node/hourContainer orchestration

Real-World Scenarios

Scenario 1: Web App That Scales

Use an Instance Group with Application Load Balancer. Define an autoscaling policy based on CPU or custom metrics. As traffic increases, new VMs spin up automatically.

Scenario 2: Event-driven Processing

Use Cloud Functions triggered by Cloud Storage. When a file is uploaded to a bucket, a function automatically processes it (resize images, extract text, etc.).

Scenario 3: Microservices on Kubernetes

Deploy containers on GKE. Use Helm or Kustomize to manage releases, Istio for service mesh, and Prometheus for metrics.

Interview Questions

Beginner

What is the main difference between Compute Engine and App Engine??

Compute Engine lets you rent VMs and manage everything. App Engine handles infrastructure; you just upload code. Compute Engine gives you control; App Engine gives you simplicity.

What machine type should I choose for a small web server??

For development/testing, start with n1-standard-1 (1 vCPU, 3.75 GB). For light production, n1-standard-2. Monitor CPU/memory and right-size based on actual usage.

What is a Managed Instance Group??

A group of identical VMs created from a template. GCP automatically replaces failed VMs and scales the group based on metrics (CPU, memory, custom). You define the template once; GCP manages the fleet.

When should I use Cloud Functions??

When you have event-driven, short-lived workloads: HTTP webhooks, file processing, scheduled tasks. You pay only during execution, making it cheap for infrequent tasks.

What is GKE and when would I use it??

GKE is Google's managed Kubernetes service. Use it if you have containerized applications needing orchestration (rolling updates, health checks, scaling, service discovery).

Intermediate

How does App Engine's Flexible environment differ from Standard??

Standard is simpler and cheaper but has sandbox restrictions (no raw sockets, specific runtimes, 15-minute request timeout). Flexible lets you run custom Docker images, longer timeouts, and more control at higher cost.

What happens if a VM in a Managed Instance Group crashes??

GCP detects the failure and automatically deletes the broken VM. If the group size is below the desired count, it creates a replacement immediately using the template.

How does auto-scaling work in Instance Groups??

You set CPU, memory, custom metric thresholds (e.g., scale up if CPU > 70% for 3 minutes). GCP monitors VMs and adds/removes VMs to keep metrics within range. Always set both min and max replicas to prevent runaway costs.

What is the cold start problem with Cloud Functions??

If a function hasn't been invoked recently, the first request may take seconds (initialization of the runtime). Subsequent requests are fast. For latency-sensitive use cases, consider App Engine Flexible or Compute Engine.

Summary

GCP provides compute options from full control (Compute Engine) to maximum simplicity (App Engine, Cloud Functions). Choose based on your need for customization vs convenience. Instance Groups and GKE handle scaling automatically. Most production deployments use a mix: Cloud Functions for events, Instance Groups for stateless services, GKE for complex microservices.