Skip to content

Persistent volumes and snapshots

Block storage that outlives the machine using it, and point-in-time copies of that storage kept elsewhere. It decides where a workspace or database's files are when its machine goes away, what keeping them costs, and how fast they come back.

On this page

What it is

A persistent volume is storage whose lifetime is separate from the compute that uses it. In the Kubernetes 1.37 documentation, a PersistentVolume is a piece of storage in the cluster, backed by NFS, iSCSI, a cloud provider's storage or another system, with a lifecycle independent of any pod, and a PersistentVolumeClaim is a user's request for some of it, by size and access mode. An EBS volume is the block-storage case one level down: a device you attach to an instance and use like a hard drive, and it persists after the instance is gone.

Attachment is the constraint that shapes designs. An EBS volume and the instance it attaches to must sit in the same Availability Zone, and a volume can be attached to more than one instance at a time with Multi-Attach for some volume and instance types. The claim's access mode says the same thing in cluster terms: ReadWriteOnce mounts read-write on a single node (several pods on that node can still share it), ReadOnlyMany mounts read-only on many nodes, ReadWriteMany mounts read-write on many nodes, and ReadWriteOncePod, stable since 1.29, restricts a volume to a single pod. When a claim is deleted, the volume's reclaim policy decides what happens: Retain keeps it for manual cleanup and Delete removes it; Recycle survives for nfs and hostPath volumes in 1.37.

A snapshot is a point-in-time copy of a volume, kept apart from it. Taking an EBS snapshot is asynchronous: the snapshot object exists at once but stays pending until its data has been transferred to S3, which can take hours when many blocks changed, and the volume stays usable meanwhile. What it captures is the data written to the volume when the snapshot was requested, not data still cached by the application or the operating system. For a consistent copy, the documentation recommends pausing writes first, or unmounting the volume if writes can't be paused.

Snapshots are incremental. The first snapshot of a volume is a full copy of the blocks written so far; each later one holds the blocks changed or added since the previous snapshot and references the rest. Size and billing follow the data, not the volume: a 200 GiB volume with 50 GiB of data gives a 50 GiB first snapshot, and changing 20 GiB and adding 10 GiB before the next one gives a 30 GiB increment. A volume created from a snapshot is an exact replica of the original at that moment, and a snapshot of that new volume is still incremental against the one it came from, as long as the same account owns both and the same KMS key encrypts both.

ObjectWhat it stands for

PersistentVolume

A piece of storage in the cluster, provisioned by an administrator or dynamically through a storage class

PersistentVolumeClaim

A request for storage with a size and access mode; a pod mounts the claim

VolumeSnapshot

A request for a snapshot of a volume; needs a CSI driver that implements snapshots

VolumeSnapshotContent

The snapshot that exists on the storage backend, identified by its snapshotHandle

PersistentVolumeClaim with dataSource

A new volume pre-filled from a snapshot: how a restore is expressed

Restore has a hidden cost. When a volume is created from an EBS snapshot, its data is lazy loaded: a read of a block that hasn't arrived yet waits while that block is fetched, so latency is higher than normal until the blocks in use have been touched. Two ways around it: initialize the volume by reading every block before handing it over, which pulls the whole snapshot down from S3, or enable fast snapshot restore on the snapshot in a given Availability Zone, so volumes created there are fully initialized at creation, as long as the snapshot has a volume creation credit to spend; without one, the volume is created without the benefit. Fast restore is billed per minute with a one-hour minimum; the documentation's example puts one snapshot in one zone for 30 days at $540 at $0.75 an hour. It applies to snapshots of 16 TiB or less, and a Region allows up to 5 snapshots enabled at a time.

When to use it

Put state on a persistent volume when a workload needs a real filesystem with random writes, and that state has to outlive the process writing it: a developer's workspace with its checked-out repository and installed dependencies, or a database's data directory. The resume path is then an attach, not a copy: the node dies or is scaled away, the claim still points at the same volume, and a new pod on a node in that zone mounts it. Design the scheduler around the zone constraint, because a volume in one Availability Zone attaches to an instance in that zone.

Take a snapshot when you want a copy that doesn't depend on the volume: before a risky migration, to clone a volume for a second environment, or to hold a workspace through a long idle period. For the idle case the arithmetic is on your side. A snapshot stores the blocks written, so a 200 GiB volume holding 50 GiB costs snapshot storage for 50 GiB. Our advice for that case: wait until the snapshot leaves the pending state, then delete the volume and build a new one from the snapshot when the user returns. A later snapshot of the restored volume stays incremental under the ownership and encryption-key conditions described above; outside them it's a full copy.

When not to

Not when several machines need read-write access to the same files at once. A ReadWriteOnce volume mounts on a single node, so a design that fans a build across nodes and expects them to share one volume needs ReadWriteMany storage or a different layout: shared artifacts in an object store, with each node keeping its own scratch volume. See Blob storage and presigned uploads for the object-store side.

Not as the backup of a busy database without quiescing it. The snapshot holds what reached the volume at the moment of the request, not what the database still had in memory, so a snapshot taken mid-write can need crash recovery on restore. If the database can pause writes or produce its own consistent dump, use that path, and snapshot afterward.

Not when the state is small or rebuildable. A few config files and a build cache that a script recreates in a minute don't justify a per-workspace volume, with the zone placement a cloud block volume such as EBS imposes and the restore step a snapshot adds; keep the files in source control or an object store and rebuild on a fresh machine. And don't reach for fast snapshot restore as a default: at $0.75 an hour per snapshot per zone, with a quota of 5 enabled snapshots per Region, it fits a handful of shared base images, not one snapshot per user.

Compared

Where the files sitComing back on another machineWhat you pay forWhen it's the right choice

Volume attached, machine running

Nothing to do; the session is live

The live session; no snapshot storage

An active session

Volume kept, detached

Attach to a new instance in the same zone and mount; no restore step

No snapshot storage; the volume itself is kept

A short pause, when fast resume matters more than idle cost

Snapshot, volume deleted

Create a volume from the snapshot and mount; blocks load on first read

Snapshot storage for the blocks written

A long absence, or a copy that must survive the volume

Snapshot with fast restore enabled

Volume is initialized at creation when the snapshot has a creation credit available; otherwise it is created without the benefit

Snapshot storage plus an hourly fee per snapshot per zone

A few shared base images many workspaces start from

Files rebuilt from source control

Clone and reinstall on the new machine

Compute time for the rebuild

Small or derivable state, where no volume is worth managing

In a picture

shortlongWorkspace goesidleDetach volume,keep itSnapshot, deletevolumeReattach in samezoneNew volume fromsnapshotBlocks load onfirst readWorkspaceresumes
Two paths back from idle: a kept volume reattaches; a snapshot becomes a new volume whose blocks arrive as they are read.

How to say it in an interview

The common mistake

Quiz

Two pods on different nodes both need to write to the same volume. A claim with access mode ReadWriteOnce is bound. What happens?
A 500 GiB volume has 40 GiB of data written. How big is its first snapshot, for billing?
A workspace restored from a snapshot feels slow for the first few minutes, then normal. What is the most likely cause?
You snapshot a volume while an application is writing to it, without pausing anything. What does the snapshot contain?

Questions that use this

Related

Sources

  1. Kubernetes documentation (v1.37): Persistent Volumes
  2. Kubernetes documentation (v1.37): Volume Snapshots
  3. Amazon EBS User Guide: Amazon EBS volumes
  4. Amazon EBS User Guide: Create Amazon EBS snapshots
  5. Amazon EBS User Guide: How Amazon EBS snapshots work
  6. Amazon EBS User Guide: Amazon EBS fast snapshot restore
  7. AWS Storage Blog: Addressing I/O latency when restoring Amazon EBS volumes from EBS Snapshots
Sign in to report a mistake