Skip to content

Persistent Volume Storage

Volume storage is a storage solution based on Kubernetes PV and PVC used for persistent storage. This creates a disk that is attached to the cluster and can be mounted as a volume in a pod.

Volume storage is considered a last resort for storage and only if the existing other storage solutions are not suitable for your use case. Please make sure you have considered all other options first.

Limitations

Volume storage has the following limitations:

  • Only available in GCP.
  • There is no automatic backup and restore of data.
  • There is no automatic scaling of the volume size.
  • A volume can only be mounted to one pod at a time. Which means that if you have multiple pods they can not share a single volume.

    It also means that if you have only one instance it needs to be completely stopped before a new instance can be started with the same volume resulting in downtime.

Even though you can request very large volumes, it is not a good solution for storing large amounts of data. We do not recommended storing more than 100GB of data in a single volume.

Example application

An example app myapp with a volume of 1Gi:

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: myapp
spec:
  serviceName: myapp
  replicas: 1
  selector:
    matchLabels:
      app: myapp
  template:
    metadata:
      labels:
        app: myapp
    spec:
      containers:
      - name: myapp
        image: myapp:latest
        volumeMounts:
        - name: myapp-storage
          mountPath: /data
  volumeClaimTemplates:
  - metadata:
      name: myapp-storage
    spec:
      accessModes: [ "ReadWriteOnce" ]
      resources:
        requests:
          storage: 1Gi

Storage classes

By default, dynamically provisioned PersistentVolumes use the default StorageClass and are backed by standard hard disks. If you need faster SSDs, you can use the ssd-storage storage class.

To set the storage class for a volume, add the storageClassName field to the PersistentVolumeClaim:

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: myapp
spec:
  ...
  volumeClaimTemplates:
  - metadata:
      name: myapp-storage
    spec:
      storageClassName: ssd-storage
      ...

Volume Snapshot

In Kubernetes, a VolumeSnapshot represents a snapshot of a volume on a storage system. We recommend you to already be familiar with Kubernetes persistent volumes before using this.

apiVersion: snapshot.storage.k8s.io/v1
kind: VolumeSnapshot
metadata:
  name: new-snapshot-test
spec:
  volumeSnapshotClassName: csi-hostpath-snapclass
  source:
    persistentVolumeClaimName: pvc-test

Volume Resize

You can resize a volume by editing the spec.resources.requests.storage field of the PersistentVolumeClaim object.

kubectl patch pvc myapp-storage -p '{"spec":{"resources":{"requests":{"storage":"2Gi"}}}}'

This will resize the volume to 2Gi. The volume will be resized when the pod is restarted.

Reference


Last update: 2022-11-08
Created: 2020-03-23