Skip to main content

Storage in Kubernetes

Managing storage is a critical part of running stateful applications in Kubernetes. This page covers the key concepts of storage in Kubernetes.

Volumes

A Volume is a directory, possibly with some data in it, which is accessible to the containers in a Pod. A Kubernetes Volume has an explicit lifetime the same as the Pod that encloses it. Consequently, a Volume outlives any containers that run within the Pod, and data is preserved across container restarts.

There are many types of Volumes in Kubernetes. Some of the most common are:

  • emptyDir: A temporary directory that is created when a Pod is assigned to a Node, and exists as long as that Pod is running on that node.
  • hostPath: Mounts a file or directory from the host node's filesystem into your Pod.
  • nfs: An NFS (Network File System) mount that allows an existing NFS share to be mounted into a Pod.
  • persistentVolumeClaim: Used to mount a PersistentVolume into a Pod.

PersistentVolumes (PVs) and PersistentVolumeClaims (PVCs)

  • PersistentVolume (PV): A piece of storage in the cluster that has been provisioned by an administrator or dynamically provisioned using Storage Classes. It is a resource in the cluster just like a node is a cluster resource.
  • PersistentVolumeClaim (PVC): A request for storage by a user. It is similar to a Pod. Pods consume node resources and PVCs consume PV resources.

This separation of concerns allows users to request storage without needing to know the details of the underlying storage infrastructure.

Here is an example of a PersistentVolumeClaim:

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: my-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi

StorageClasses

A StorageClass provides a way for administrators to describe the "classes" of storage they offer. Different classes might map to quality-of-service levels, or to backup policies, or to arbitrary policies determined by the cluster administrators. This allows for the dynamic provisioning of PersistentVolumes.

Here is an example of a StorageClass:

apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: standard
provisioner: kubernetes.io/aws-ebs
parameters:
type: gp2
reclaimPolicy: Retain
allowVolumeExpansion: true
mountOptions:
- debug
volumeBindingMode: Immediate