GCP for DevOps — Persistent Disks

3 minute read

Persistent Disks

Persistent Disks are durable, high-performance block storage for Compute Engine VMs and GKE nodes. They exist independently of VMs and can be resized, snapshotted, and attached/detached without data loss.

Disk Types

┌──────────────────────────────────────────────────────┐
│              Persistent Disk Types                    │
├───────────────┬────────┬──────────┬─────────────────┤
│ Type          │ IOPS   │ Through- │ Use Case        │
│               │ (r/w)  │ put      │                 │
├───────────────┼────────┼──────────┼─────────────────┤
│ pd-standard   │ 0.75/  │ 12 MB/s  │ Logs, bulk      │
│ (HDD)         │ 1.5    │ per GB   │ storage         │
├───────────────┼────────┼──────────┼─────────────────┤
│ pd-balanced   │ 6/6    │ 28 MB/s  │ General-purpose │
│ (SSD)         │ per GB │ per GB   │ boot disks      │
├───────────────┼────────┼──────────┼─────────────────┤
│ pd-ssd        │ 30/30  │ 48 MB/s  │ Databases, high │
│ (SSD)         │ per GB │ per GB   │ performance     │
├───────────────┼────────┼──────────┼─────────────────┤
│ pd-extreme    │ Up to  │ Up to    │ SAP HANA,       │
│ (SSD)         │120,000 │ 2.4 GB/s │ Oracle DBs      │
├───────────────┼────────┼──────────┼─────────────────┤
│ hyperdisk-    │ Custom  │ Custom   │ Demanding       │
│ extreme       │ IOPS   │ through. │ databases       │
└───────────────┴────────┴──────────┴─────────────────┘

Create and Attach Disks

# Create a persistent disk
$ gcloud compute disks create data-disk-1 \
    --size=100GB \
    --type=pd-ssd \
    --zone=us-central1-a

# Attach to a running VM
$ gcloud compute instances attach-disk my-vm \
    --disk=data-disk-1 \
    --zone=us-central1-a

# Format and mount inside the VM
$ sudo mkfs.ext4 -m 0 -E lazy_itable_init=0 /dev/sdb
$ sudo mkdir -p /mnt/data
$ sudo mount /dev/sdb /mnt/data
$ echo '/dev/sdb /mnt/data ext4 defaults 0 2' | sudo tee -a /etc/fstab

Resize Disks (No Downtime)

# Increase disk size (can only grow, never shrink)
$ gcloud compute disks resize data-disk-1 \
    --size=200GB \
    --zone=us-central1-a

# Resize the filesystem inside the VM (ext4)
$ sudo resize2fs /dev/sdb

# For XFS filesystems
$ sudo xfs_growfs /mnt/data

Snapshots

Snapshots capture the state of a disk for backup and replication.

# Create a snapshot
$ gcloud compute snapshots create data-disk-snapshot-1 \
    --source-disk=data-disk-1 \
    --source-disk-zone=us-central1-a \
    --storage-location=us

# List snapshots
$ gcloud compute snapshots list

NAME                      DISK_SIZE_GB  SRC_DISK      STATUS
data-disk-snapshot-1      100           data-disk-1   READY

# Create a disk from snapshot (same or different zone)
$ gcloud compute disks create data-disk-restored \
    --source-snapshot=data-disk-snapshot-1 \
    --zone=us-east1-b \
    --type=pd-ssd

Snapshot Schedules

Automate recurring snapshots.

# Create a snapshot schedule (daily, keep 7 days)
$ gcloud compute resource-policies create snapshot-schedule daily-backup \
    --region=us-central1 \
    --max-retention-days=7 \
    --daily-schedule \
    --start-time=02:00

# Attach schedule to a disk
$ gcloud compute disks add-resource-policies data-disk-1 \
    --resource-policies=daily-backup \
    --zone=us-central1-a

# List snapshot schedules
$ gcloud compute resource-policies list --filter="region:us-central1"

Regional Persistent Disks

Replicate data synchronously across two zones for high availability.

# Create a regional persistent disk
$ gcloud compute disks create regional-data-disk \
    --size=100GB \
    --type=pd-ssd \
    --region=us-central1 \
    --replica-zones=us-central1-a,us-central1-c

# Force-attach to a VM in the other zone during failover
$ gcloud compute instances attach-disk standby-vm \
    --disk=regional-data-disk \
    --disk-scope=regional \
    --zone=us-central1-c

Persistent Disks in GKE

Use StorageClasses and PersistentVolumeClaims to provision disks.

# StorageClass for pd-ssd
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: fast-ssd
provisioner: pd.csi.storage.gke.io
parameters:
  type: pd-ssd
reclaimPolicy: Retain
volumeBindingMode: WaitForFirstConsumer
---
# PersistentVolumeClaim
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: data-pvc
spec:
  accessModes:
    - ReadWriteOnce
  storageClassName: fast-ssd
  resources:
    requests:
      storage: 50Gi

Common Mistakes

Mistake Why It’s Wrong Fix
Using pd-standard for databases Very low IOPS Use pd-ssd or pd-balanced
No snapshot schedule No backup if disk corrupted Attach automated snapshot schedule
Forgetting to resize filesystem Disk grows but filesystem doesn’t Run resize2fs or xfs_growfs after resize
Single-zone disk for HA workloads Zone failure loses data Use regional persistent disk
Not adding fstab entry Mount lost after reboot Add to /etc/fstab

Quick Reference

Command Description
gcloud compute disks create D --size=S --type=T Create disk
gcloud compute instances attach-disk VM --disk=D Attach disk
gcloud compute disks resize D --size=S Resize disk
gcloud compute snapshots create S --source-disk=D Create snapshot
gcloud compute resource-policies create snapshot-schedule Create schedule

Next → Filestore

Updated: