Volume Types

Kubernetes supports many different volume types, each suited for different use cases. Understanding volume types helps you choose the right storage solution for your application. Think of volume types as different ways to connect storage to your containers—some are temporary like sticky notes, while others are permanent like filing cabinets.

Volume Categories

Volumes in Kubernetes fall into two main categories:

Ephemeral Volumes

Ephemeral volumes have a lifecycle tied to the pod. When the pod is deleted, the data in ephemeral volumes is lost. These are useful for temporary data, caching, or sharing data between containers in the same pod.

graph LR A[Pod Created] --> B[Ephemeral Volume Created] B --> C[Containers Use Volume] C --> D[Pod Deleted] D --> E[Volume Data Lost] style A fill:#e1f5ff style B fill:#fff4e1 style C fill:#e8f5e9 style E fill:#ffe1e1

Persistent Volumes

Persistent volumes survive pod deletions and can be used across pod restarts. The data persists independently of the pod lifecycle, making them essential for databases, file storage, and any application that needs to retain data.

graph LR A[Pod Created] --> B[Persistent Volume Mounted] B --> C[Containers Use Volume] C --> D[Pod Deleted] D --> E[Volume Data Retained] E --> F[New Pod Can Mount Same Volume] style A fill:#e1f5ff style B fill:#e8f5e9 style C fill:#fff4e1 style E fill:#e8f5e9 style F fill:#e1f5ff

Common Ephemeral Volume Types

emptyDir

A temporary directory that shares a pod’s lifetime. Perfect for temporary files, caching, or scratch space.

configMap

Makes ConfigMap data available as files in a directory. Read-only, useful for configuration files.

secret

Makes Secret data available as files in a directory. Read-only, useful for sensitive configuration like certificates and API keys.

downwardAPI

Exposes pod and container field values as files. Useful when containers need metadata about themselves.

Common Persistent Volume Types

CSI (Container Storage Interface)

The modern standard for persistent volumes. CSI drivers allow Kubernetes to use any storage system that provides a CSI driver, including cloud storage (AWS EBS, GCE Persistent Disk, Azure Disk) and on-premises storage.

NFS

Network File System volumes that can be shared across multiple pods. Supports ReadWriteMany access mode.

iSCSI

Internet Small Computer Systems Interface volumes for connecting to block storage devices over a network.

Local

Direct connection to local storage on a node. Requires careful node selection and doesn’t support automatic migration.

Volume Lifecycle Comparison

ephemeral

graph TB subgraph ephemeral[Ephemeral Volumes] A[Pod Created] --> B[Volume Created] B --> C[Pod Uses Volume] C --> D[Pod Deleted] D --> E[Volume Destroyed] end style A fill:#e1f5ff style E fill:#ffe1e1

persistent

graph TB subgraph persistent[Persistent Volumes] F[PVC Created] --> G[PV Provisioned] G --> H[PV Bound to PVC] H --> I[Pod Mounts PVC] I --> J[Pod Uses Volume] J --> K[Pod Deleted] K --> L[PV Retained] L --> M[New Pod Can Mount] end style F fill:#e1f5ff style L fill:#e8f5e9 style M fill:#e1f5ff

When to Use Each Type

Use Ephemeral Volumes When:

  • Storing temporary or cache data
  • Sharing data between containers in the same pod
  • Providing configuration files or secrets to containers
  • The data doesn’t need to survive pod restarts

Use Persistent Volumes When:

  • Storing database data
  • Persisting application state
  • Storing user uploads or generated files
  • The data must survive pod restarts, updates, or node failures
  • Multiple pods need to share the same data (with appropriate access modes)

Example: Ephemeral vs Persistent

Here’s a comparison showing both volume types in action:

Ephemeral Volume (emptyDir):

apiVersion: v1
kind: Pod
metadata:
  name: app-with-cache
spec:
  containers:
  - name: app
    image: nginx
    volumeMounts:
    - name: cache
      mountPath: /tmp/cache
  volumes:
  - name: cache
    emptyDir: {}  # Ephemeral - lost when pod deleted

Persistent Volume (PVC):

apiVersion: v1
kind: Pod
metadata:
  name: app-with-data
spec:
  containers:
  - name: app
    image: nginx
    volumeMounts:
    - name: data
      mountPath: /var/www/html
  volumes:
  - name: data
    persistentVolumeClaim:
      claimName: my-data-pvc  # Persistent - survives pod deletion
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: my-data-pvc
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 10Gi

Best Practices

  1. Choose ephemeral for temporary data - Use emptyDir, configMap, or secret for data that doesn’t need persistence
  2. Use persistent volumes for important data - Always use PVs/PVCs for databases, user data, or any data that must survive restarts
  3. Consider access patterns - Match volume types to your access requirements (single pod vs multiple pods)
  4. Use CSI for flexibility - Prefer CSI volumes over in-tree volume types for better compatibility
  5. Test volume behavior - Understand how volumes behave when pods restart or are deleted

Topics

See Also

  • PVs & PVCs - How to request and use persistent storage
  • StorageClasses - Dynamic provisioning of persistent volumes
  • StatefulSets - Managing stateful applications with persistent storage