Working with Volumes
1. Using emptyDir Volumes
| Property | Value | Description |
|---|---|---|
| emptyDir | {} |
Defines an emptyDir volume |
| medium | "" or "Memory" |
Storage medium: "" for disk, "Memory" for RAM |
| sizeLimit | 1Gi |
Maximum size of the volume (optional) |
Example: Mounting an emptyDir Volume
apiVersion: v1
kind: Pod
metadata:
name: test-pod
spec:
containers:
- name: test-container
image: nginx
volumeMounts:
- name: cache-volume
mountPath: /cache
volumes:
- name: cache-volume
emptyDir: {}
Note: Data in
emptyDir volumes is lost when the Pod is deleted.
Warning: Using
medium: Memory can lead to memory pressure on nodes.
2. Using hostPath Volumes
| Property | Value | Description |
|---|---|---|
| hostPath.path | /var/lib/data |
Path on the host node filesystem to mount |
| hostPath.type | Directory |
Optional field to specify type of path (Directory, File, etc.) |
Example: Mounting hostPath volume in Pod
apiVersion: v1
kind: Pod
metadata:
name: hostpath-pod
spec:
containers:
- name: app-container
image: nginx
volumeMounts:
- name: host-data
mountPath: /usr/share/nginx/html
volumes:
- name: host-data
hostPath:
path: /var/lib/data
type: Directory
Note: Using
hostPath volumes can pose security risks and should be used carefully in production environments.
3. Configuring PersistentVolumes
| Property | Description | Values | Notes |
|---|---|---|---|
| capacity | Storage size of the volume | 10Gi, 500Mi |
Must be specified for all volumes |
| accessModes | How the volume can be mounted | ReadWriteOnce, ReadOnlyMany, ReadWriteMany |
Must include at least one mode |
| persistentVolumeReclaimPolicy | What happens after the PVC is deleted | Retain, Recycle, Delete |
Retain is default in most cases |
| storageClassName | Storage class to use for dynamic provisioning | standard, fast |
Optional; if omitted, uses default class |
| volumeMode | Volume mode (file or block) | Filesystem, Block |
Defaults to Filesystem |
Example: Creating a PersistentVolume
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv-example
spec:
capacity:
storage: 10Gi
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Retain
storageClassName: standard
hostPath:
path: /mnt/data
Note:
hostPath is for testing only. In production, use cloud provider or CSI drivers.
Warning: Ensure
accessModes match the application's needs to avoid runtime errors.
4. Creating PersistentVolumeClaims
| Field | Description | Example |
|---|---|---|
| apiVersion | API version to use | corev1 |
| kind | Resource type | PersistentVolumeClaim |
| metadata.name | Name of the PVC | my-pvc |
| spec.accessModes | Access mode for volume | [ReadWriteOnce] |
| spec.resources.requests.storage | Storage size requested | 10Gi |
| spec.storageClassName | Storage class to use (optional) | standard |
Example: Creating a PVC
apiVersion: corev1
kind: PersistentVolumeClaim
metadata:
name: my-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10Gi
5. Binding PVC to PV
| Property | Description | Values |
|---|---|---|
| claimRef | Reference to the PersistentVolumeClaim | object |
| persistentVolumeReclaimPolicy | Policy for reclaiming the volume after the claim is released | Delete, Retain |
| storageClassName | Storage class name for dynamic provisioning | standard, fast |
Example: Binding PVC to PV
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: my-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10Gi
volumeName: my-pv
Note: The
volumeName must match the name of an existing PersistentVolume.
Warning: Ensure that the
storageClassName matches between PVC and PV for dynamic provisioning.
6. Listing PersistentVolumes
| Field | Description | Example Value |
|---|---|---|
| metadata.name | Name of the PersistentVolume | pv0001 |
| spec.capacity | Storage capacity of the volume | 5Gi |
| spec.accessModes | Access modes supported by the volume | ["ReadWriteOnce"] |
| spec.persistentVolumeReclaimPolicy | Policy for reclaiming the volume | Retain |
| spec.storageClassName | StorageClass associated with the volume | standard |
| status.phase | Current phase of the volume (e.g., Available, Bound) | Available |
7. Listing PersistentVolumeClaims
| Field | Description | Example Value |
|---|---|---|
| metadata.name | Name of the PVC | my-pvc |
| spec.accessModes | Access mode for the volume | ["ReadWriteOnce"] |
| spec.resources.requests.storage | Requested storage size | 10Gi |
| status.phase | Current phase of the PVC | Pending |
8. Understanding Access Modes
| Mode | Description | Usage |
|---|---|---|
| ReadWriteOnce (RWO) | Volume can be mounted as read-write by a single node | Most common for single pod usage |
| ReadOnlyMany (ROX) | Volume can be mounted as read-only by many nodes | Use for shared read-only data like config maps or images |
| ReadWriteMany (RWX) | Volume can be mounted as read-write by many nodes | Requires underlying storage to support concurrent writes (e.g., NFS) |
Note: Not all storage systems support all access modes. Ensure your storage class supports the desired mode.
9. Setting Reclaim Policies
| Policy | Description | Behavior | Use Case |
|---|---|---|---|
| Retain | Volume is not deleted when the PVC is deleted | Manual cleanup required | For data preservation, backup scenarios |
| Recycle | Deprecated in Kubernetes 1.5+, replaced by Retain |
Basic cleanup using a simple command | Legacy systems only (not recommended) |
| Delete | Volume is deleted when the PVC is deleted | Automatic cleanup of storage | Temporary or ephemeral data storage |
Example: Setting Reclaim Policy to Retain
apiVersion: v1
kind: PersistentVolume
metadata:
name: example-pv
spec:
capacity:
storage: 10Gi
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Retain
hostPath:
path: /mnt/data
Note: The
Retain policy allows manual cleanup of volumes, useful for data recovery or backup scenarios.
Warning: Using
Recycle policy is deprecated and should not be used in production.
10. Using Dynamic Provisioning
| Property | Description | Default Value | Notes |
|---|---|---|---|
| storageClassName | Name of the StorageClass to use for dynamic provisioning | "" | Must match an existing StorageClass name in the cluster |
| volumeName | Name of the volume to be created (optional) | "" | If not specified, a unique name is generated |
| accessModes | Access modes for the volume (e.g., ReadWriteOnce, ReadOnlyMany) | [ReadWriteOnce] | Must be supported by the StorageClass |
| resources.requests.storage | Requested storage size (e.g., 10Gi) | None | Must be specified for dynamic provisioning |
Example: Creating a PersistentVolumeClaim with Dynamic Provisioning
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: dynamic-pvc
spec:
storageClassName: "fast-ssd"
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 20Gi
Note: The
storageClassName must reference a valid StorageClass that supports dynamic provisioning.
Warning: Ensure the StorageClass has sufficient capacity and appropriate provisioner to handle the requested storage size.
11. Expanding PersistentVolumes
| Field | Description | Notes |
|---|---|---|
| spec.capacity | Requested storage capacity | Must be greater than current size |
| spec.accessModes | Access modes for volume | Must include existing mode(s) |
| spec.volumeName | Name of the volume to expand | Must match existing PV name |
Example: Expanding a PersistentVolume
apiVersion: v1
kind: PersistentVolume
metadata:
name: example-pv
spec:
capacity:
storage: 50Gi
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Retain
csi:
driver: ebs.csi.aws.com
volumeHandle: vol-0123456789abcdef0
Note: Expansion is only supported for volumes that are not currently in use by a pod.
Warning: Ensure that the underlying storage system supports expansion before attempting to increase capacity.
Key Points
- Expansion requires
allowVolumeExpansionto be enabled in the StorageClass. - Only
ReadWriteOnceandReadWriteManyaccess modes support expansion. - Expanding a volume does not affect data or existing mounts.
12. Deleting PersistentVolumeClaims
| Property | Description | Notes |
|---|---|---|
| persistentVolumeClaim.name | Name of the PersistentVolumeClaim to delete | Must match existing PVC name |
| persistentVolumeClaim.namespace | Namespace where PVC is defined | Defaults to current namespace if not specified |
Note: Deleting a PVC will also delete the associated PersistentVolume if
reclaimPolicy is set to Delete.Warning: Ensure data is backed up before deletion as this action is irreversible.