This page provides an overview of PersistentVolumes and PersistentVolumeClaims in Kubernetes, and their use with Google Kubernetes Engine. This page focuses on storage backed by Compute Engine Persistent Disks.
PersistentVolume resources are used to manage durable storage in a cluster. On GKE, PersistentVolumes are typically backed by Compute Engine persistent disks. PersistentVolumes can also be used with other storage types like NFS. Refer to the Kubernetes documentation for an exhaustive overview of PersistentVolumes.
Unlike Volumes, the PersistentVolumes lifecycle is managed by Kubernetes. PersistentVolumes can be dynamically provisioned; the user does not have to manually create and delete the backing storage.
PersistentVolumes are cluster resources that exist independently of Pods. This means that the disk and data represented by a PersistentVolume continue to exist as the cluster changes and as Pods are deleted and recreated. PersistentVolume resources can be provisioned dynamically through PersistentVolumeClaims, or they can be explicitly created by a cluster administrator.
A PersistentVolumeClaim is a request for and claim to a PersistentVolume resource. PersistentVolumeClaim objects request a specific size, access mode, and StorageClass for the PersistentVolume. If a PersistentVolume that satisfies the request exists or can be provisioned, the PersistentVolumeClaim is bound to that PersistentVolume.
Pods use claims as Volumes. The cluster inspects the claim to find the bound Volume and mounts that Volume for the Pod.
Portability is another advantage of using PersistentVolumes and PersistentVolumeClaims. You can easily use the same Pod specification across different clusters and environments because PersistentVolume is an interface to the actual backing storage.
Volume implementations such as gcePersistentDisk are configured through StorageClass resources. GKE creates a default StorageClass for you which uses the standard persistent disk type. The default StorageClass is used when a PersistentVolumeClaim doesn't specify a StorageClassName. You can replace the provided default StorageClass with your own.
You can create your own StorageClass resources to describe different classes of storage. For example, classes might map to quality-of-service levels, or to backup policies. This concept is sometimes called "profiles" in other storage systems.
Dynamically provisioning PersistentVolumes
Most of the time, you don't need to directly configure PersistentVolume objects or create Compute Engine persistent disks. Instead, you can create a PersistentVolumeClaim and Kubernetes automatically provisions a persistent disk for you.
The following manifest describes a request for a 30 GiB disk whose access mode allows it to be mounted as read-write by a single node:
apiVersion: v1 kind: PersistentVolumeClaim metadata: name: helloweb-disk spec: accessModes: - ReadWriteOnce resources: requests: storage: 30Gi
When you create this PersistentVolumeClaim with
kubectl apply -f
pvc-demo.yaml, Kubernetes dynamically creates a corresponding PersistentVolume
object. Assuming that you haven't replaced the GKE default storage
class, this PersistentVolume is backed by a new, empty Compute Engine persistent
disk. You use this disk in a Pod by using the claim as a volume.
When you delete this claim, the corresponding PersistentVolume object as well as the provisioned Compute Engine persistent disk are also deleted.
Should you want to prevent deletion of dynamically provisioned persistent disks,
set the reclaim policy of the PersistentVolume resource, or its StorageClass
Retain. In this case, you are charged for the persistent disk
for as long as it exists even if there is no PersistentVolumeClaim consuming it.
PersistentVolumes support the following access modes:
- ReadWriteOnce: The Volume can be mounted as read-write by a single node.
- ReadOnlyMany: The Volume can be mounted read-only by many nodes.
- ReadWriteMany: The Volume can be mounted as read-write by many nodes. PersistentVolumes that are backed by Compute Engine persistent disks don't support this access mode.
Using Compute Engine Persistent Disks as ReadOnlyMany
ReadWriteOnce is the most common use case for Persistent Disks and works as the default access mode for most applications. Compute Engine Persistent Disks also support ReadOnlyMany mode so that many applications or many replicas of the same application can consume the same disk at the same time. An example use case is serving static content across multiple replicas.
Refer to this article for instructions for creating persistent disks for multiple readers.
Using preexisting persistent disks as PersistentVolumes
Dynamically provisioned PersistentVolumes are empty when they are created. If you have an existing Compute Engine persistent disk populated with data, you can introduce it to your cluster by manually creating a corresponding PersistentVolume resource. The persistent disk must be in the same zone as the cluster nodes.
Deployments vs. StatefulSets
Deployments are designed for stateless applications and therefore all replicas of a Deployment share the same Persistent Volume Claim. Since the replica Pods created will be identical to each other, only Volumes with modes ReadOnlyMany or ReadWriteMany can work in this setting.
Even Deployments with one replica using a ReadWriteOnce Volume are not recommended. This is because the default Deployment strategy will create a second Pod before bringing down the first pod on a recreate. The Deployment may fail in deadlock as the second Pod can't start because the ReadWriteOnce Volume is already in use, and the first Pod wont be removed because the second Pod has not yet started. Instead, use a StatefulSet with ReadWriteOnce volumes.
StatefulSets are the recommended method of deploying stateful applications that require a unique volume per replica. By using StatefulSets with Persistent Volume Claim Templates you can have applications that can scale up automatically with unique Persistent Volume Claims associated to each replica Pod.
Regional Persistent Disks
Regional persistent disks replicate data between two zones in the same region, and can be used similarly to regular persistent disks. In the event of a zonal outage, Kubernetes can failover workloads using the volume to the other zone. You can use regional persistent disks to build highly available solutions for stateful workloads on GKE. Users must ensure that both the primary and failover zones are configured with enough resource capacity to run the workload.
Regional SSD persistent disks are an option for applications such as databases that require both high availability and high performance. For more details see Block storage performance comparison.
As with regular persistent disks, regional persistent disks can be dynamically provisioned as needed or manually provisioned in advance by the cluster administrator.
To learn how to add regional persistent disks, refer to these instructions for provisioning regional persistent disks.
- Learn about StatefulSets, the recommended method of deploying stateful applications.
- Learn how to deploy a stateful application using a StatefulSet
- Learn to use persistent disks in a cluster
- Learn how to create a disk that can be read from multiple nodes
- Learn how to create persistent disks backed by SSDs
- Learn how to provision regional persistent disks