GCP for DevOps — Rightsizing & Recommenders

3 minute read

Rightsizing & Recommenders

GCP provides built-in recommenders that analyze resource usage and suggest rightsizing actions. Identifying idle, underutilized, and over-provisioned resources is one of the most effective ways to reduce cloud costs.

Recommender Architecture

GCP Recommender Architecture:
┌─────────────────────────────────────────────────────┐
│                  Usage Telemetry                     │
│  ┌──────────┐  ┌──────────┐  ┌──────────┐         │
│  │Compute   │  │Disks     │  │GKE       │         │
│  │Engine    │  │Snapshots │  │Clusters  │         │
│  └────┬─────┘  └────┬─────┘  └────┬─────┘         │
│       │              │              │                │
│       └──────────────┼──────────────┘                │
│                      ▼                               │
│           ┌──────────────────┐                       │
│           │   Recommender    │                       │
│           │   Engine         │                       │
│           │  • Usage metrics │                       │
│           │  • ML analysis   │                       │
│           │  • Cost modeling │                       │
│           └────────┬─────────┘                       │
│                    ▼                                  │
│  ┌──────────────────────────────────────────┐       │
│  │  Recommendations                          │       │
│  │  • Rightsize VMs (↓ CPU/RAM)              │       │
│  │  • Delete idle VMs                        │       │
│  │  • Resize persistent disks                │       │
│  │  • Change machine type                    │       │
│  │  • CUD recommendations                   │       │
│  └──────────────────────────────────────────┘       │
└─────────────────────────────────────────────────────┘

List VM Rightsizing Recommendations

$ gcloud recommender recommendations list \
    --project=my-project \
    --location=us-central1-a \
    --recommender=google.compute.instance.MachineTypeRecommender
RECOMMENDATION_ID  CATEGORY    DESCRIPTION                                    PRIORITY
abc123-def456      COST        Change machine type of web-server-1            P2
                               from e2-standard-4 to e2-standard-2.
                               Estimated savings: $35.04/month
xyz789-ghi012      COST        Change machine type of api-server-2            P2
                               from n2-standard-8 to n2-standard-4.
                               Estimated savings: $97.12/month

List Idle VM Recommendations

$ gcloud recommender recommendations list \
    --project=my-project \
    --location=us-central1-a \
    --recommender=google.compute.instance.IdleResourceRecommender
RECOMMENDATION_ID  CATEGORY    DESCRIPTION                                    PRIORITY
idle-001           COST        Stop or delete idle VM 'test-server-3'.        P1
                               VM has had <1% CPU for 14 days.
                               Estimated savings: $52.56/month
idle-002           COST        Stop or delete idle VM 'staging-db-old'.       P1
                               VM has had <1% CPU for 30 days.
                               Estimated savings: $185.00/month

Apply a Recommendation

# Get recommendation details
$ gcloud recommender recommendations describe abc123-def456 \
    --project=my-project \
    --location=us-central1-a \
    --recommender=google.compute.instance.MachineTypeRecommender
description: Change machine type from e2-standard-4 to e2-standard-2
primaryImpact:
  category: COST
  costProjection:
    cost:
      currencyCode: USD
      units: '-35'
    duration: 2592000s
stateInfo:
  state: ACTIVE

# Mark recommendation as claimed (before applying)
$ gcloud recommender recommendations mark-claimed abc123-def456 \
    --project=my-project \
    --location=us-central1-a \
    --recommender=google.compute.instance.MachineTypeRecommender \
    --etag='"abc123"'

# Resize the VM (stop first, then change machine type)
$ gcloud compute instances stop web-server-1 --zone=us-central1-a
Stopping instance...done.

$ gcloud compute instances set-machine-type web-server-1 \
    --zone=us-central1-a \
    --machine-type=e2-standard-2
Updated [https://www.googleapis.com/compute/v1/projects/my-project/zones/us-central1-a/instances/web-server-1].

$ gcloud compute instances start web-server-1 --zone=us-central1-a
Starting instance...done.

Available Recommender Types

Recommender ID What It Analyzes
VM Machine Type google.compute.instance.MachineTypeRecommender CPU/memory utilization
Idle VM google.compute.instance.IdleResourceRecommender VMs with <1% CPU
Idle Disk google.compute.disk.IdleResourceRecommender Unattached persistent disks
Idle IP google.compute.address.IdleResourceRecommender Unattached static IPs
Idle Image google.compute.image.IdleResourceRecommender Unused custom images
CUD Recommender google.billing.CudRecommender Commit vs on-demand
GKE Cost google.container.DiagnosisRecommender Cluster resource waste

Disk Rightsizing

# List idle disk recommendations
$ gcloud recommender recommendations list \
    --project=my-project \
    --location=us-central1-a \
    --recommender=google.compute.disk.IdleResourceRecommender
RECOMMENDATION_ID  DESCRIPTION
disk-idle-001      Delete idle disk 'old-data-disk' (100GB, unattached 45 days).
                   Estimated savings: $4.00/month

# Find unattached disks manually
$ gcloud compute disks list \
    --project=my-project \
    --filter="NOT users:*" \
    --format="table(name,zone,sizeGb,status,lastAttachTimestamp)"
NAME              ZONE             SIZE_GB  STATUS  LAST_ATTACH
old-data-disk     us-central1-a    100      READY   2025-01-15
test-disk         us-east1-b       50       READY   2025-02-01

# Delete unused disk
$ gcloud compute disks delete old-data-disk \
    --zone=us-central1-a --quiet
Deleted [old-data-disk].

Idle IP Address Cleanup

# List static IPs and check if they're in use
$ gcloud compute addresses list \
    --project=my-project \
    --format="table(name,region,address,status)"
NAME            REGION         ADDRESS         STATUS
web-ip          us-central1    35.192.0.10     IN_USE
old-staging-ip  us-central1    35.192.0.20     RESERVED
test-ip         us-east1       35.196.0.5      RESERVED

# Reserved (unused) static IPs cost $0.01/hr (~$7.30/month each)
# Release unused IPs
$ gcloud compute addresses delete old-staging-ip \
    --region=us-central1 --quiet
Deleted [old-staging-ip].

GKE Cluster Rightsizing

# Check node utilization
$ kubectl top nodes
NAME                                    CPU(cores)   CPU%   MEMORY(bytes)   MEMORY%
gke-prod-default-pool-abc123-0001       250m         12%    1024Mi          25%
gke-prod-default-pool-abc123-0002       180m         9%     768Mi           18%
gke-prod-default-pool-abc123-0003       120m         6%     512Mi           12%

# Consider: cluster autoscaler + smaller node pool
# Or switch to GKE Autopilot for pay-per-pod
$ gcloud container clusters update production \
    --zone=us-central1-a \
    --enable-autoscaling \
    --min-nodes=1 \
    --max-nodes=5 \
    --node-pool=default-pool
Updating production...done.

Automated Rightsizing Pipeline

Automated Rightsizing Workflow:
┌──────────────┐     ┌──────────────┐     ┌──────────────┐
│  Recommender  │────▶│  Pub/Sub     │────▶│  Cloud       │
│  Insights     │     │  Topic       │     │  Function    │
└──────────────┘     └──────────────┘     └──────┬───────┘
                                                  │
                                          ┌───────▼───────┐
                                          │  Actions:      │
                                          │  • Log finding │
                                          │  • Create JIRA │
                                          │  • Auto-resize │
                                          │  • Slack alert │
                                          └───────────────┘

Common Mistakes

Mistake Impact Fix
Ignoring recommender insights Paying 30-60% more than needed Review weekly, automate actions
Over-provisioning for peaks Constant waste during off-peak Use autoscaling instead
Not cleaning up test resources Ongoing cost for unused infra Scheduled cleanup scripts
Rightsizing without monitoring Performance regressions Monitor after each change

Quick Reference

Task Command
List VM resize recommendations gcloud recommender recommendations list --recommender=google.compute.instance.MachineTypeRecommender
List idle VMs gcloud recommender recommendations list --recommender=google.compute.instance.IdleResourceRecommender
Find unattached disks gcloud compute disks list --filter="NOT users:*"
Find unused static IPs gcloud compute addresses list --filter="status=RESERVED"
Enable cluster autoscaler gcloud container clusters update CLUSTER --enable-autoscaling

Next → Discounts & Pricing

Updated: