Skip to content

Volumes

Overview

Volumes provide persistent and shared storage for containers in a pod.


Volume Types

TypeDescriptionPersistence
emptyDirTemp directory, deleted with podPod lifetime
hostPathMount from node filesystemNode
configMapMount ConfigMap as filesCluster
secretMount Secret as filesCluster
persistentVolumeClaimMount PVCPersistent
downwardAPIMount pod metadataPod lifetime

emptyDir

Temporary storage, shared between containers:

yaml
apiVersion: v1
kind: Pod
metadata:
  name: emptydir-demo
spec:
  containers:
  - name: writer
    image: busybox
    command: ["sh", "-c", "echo hello > /data/file && sleep 3600"]
    volumeMounts:
    - name: shared-data
      mountPath: /data
  - name: reader
    image: busybox
    command: ["sh", "-c", "cat /data/file && sleep 3600"]
    volumeMounts:
    - name: shared-data
      mountPath: /data
  volumes:
  - name: shared-data
    emptyDir: {}

emptyDir in Memory

yaml
volumes:
- name: cache
  emptyDir:
    medium: Memory      # Use RAM instead of disk
    sizeLimit: 100Mi

hostPath

Mount directory from node:

yaml
apiVersion: v1
kind: Pod
metadata:
  name: hostpath-demo
spec:
  containers:
  - name: app
    image: busybox
    command: ["sh", "-c", "cat /host-logs/* && sleep 3600"]
    volumeMounts:
    - name: logs
      mountPath: /host-logs
      readOnly: true
  volumes:
  - name: logs
    hostPath:
      path: /var/log
      type: Directory

hostPath Types

TypeDescription
""No check
DirectoryOrCreateCreate if missing
DirectoryMust exist
FileOrCreateCreate file if missing
FileMust exist

ConfigMap as Volume

yaml
apiVersion: v1
kind: Pod
metadata:
  name: configmap-volume
spec:
  containers:
  - name: app
    image: nginx
    volumeMounts:
    - name: config-vol
      mountPath: /etc/config
  volumes:
  - name: config-vol
    configMap:
      name: app-config

Mount Specific Keys

yaml
volumes:
- name: config-vol
  configMap:
    name: app-config
    items:
    - key: app.properties
      path: application.properties
    - key: logging.conf
      path: log.conf

Secret as Volume

yaml
apiVersion: v1
kind: Pod
metadata:
  name: secret-volume
spec:
  containers:
  - name: app
    image: nginx
    volumeMounts:
    - name: secret-vol
      mountPath: /etc/secrets
      readOnly: true
  volumes:
  - name: secret-vol
    secret:
      secretName: app-secrets

PersistentVolumeClaim

yaml
apiVersion: v1
kind: Pod
metadata:
  name: pvc-demo
spec:
  containers:
  - name: app
    image: nginx
    volumeMounts:
    - name: data
      mountPath: /usr/share/nginx/html
  volumes:
  - name: data
    persistentVolumeClaim:
      claimName: my-pvc

subPath

Mount specific file/directory without overwriting:

yaml
volumeMounts:
- name: config-vol
  mountPath: /etc/nginx/nginx.conf
  subPath: nginx.conf    # Only mount this file

Multiple Mounts

yaml
spec:
  containers:
  - name: app
    image: nginx
    volumeMounts:
    - name: config
      mountPath: /etc/config
    - name: secrets
      mountPath: /etc/secrets
    - name: data
      mountPath: /data
  volumes:
  - name: config
    configMap:
      name: app-config
  - name: secrets
    secret:
      secretName: app-secrets
  - name: data
    persistentVolumeClaim:
      claimName: my-pvc

Read-Only Mount

yaml
volumeMounts:
- name: secrets
  mountPath: /etc/secrets
  readOnly: true

Useful Commands

bash
# Check mounted volumes
kubectl describe pod <pod-name> | grep -A10 "Volumes"

# Verify mount inside pod
kubectl exec <pod-name> -- ls -la /path/to/mount
kubectl exec <pod-name> -- df -h

Released under the MIT License.