Skip to main content

Chapter 6.1 - Volumes

Learning Objectives

By the end of this chapter, you will be able to:

  • Understand the different types of Kubernetes volumes
  • Use ephemeral volumes (emptyDir, tmpfs)
  • Distinguish between ephemeral volumes and persistent storage
  • Choose the right volume type based on the use case
  • Understand the volume lifecycle

Introduction

Volumes in Kubernetes allow containers to access storage. Unlike the container filesystem (which is ephemeral), volumes persist even if the container restarts.


Ephemeral Volumes

Ephemeral volumes exist only during the Pod's lifetime. They are deleted when the Pod terminates.

emptyDir

An emptyDir volume is created when a Pod starts and deleted when the Pod terminates.

apiVersion: v1
kind: Pod
metadata:
name: empty-dir-pod
spec:
containers:
- name: app
image: nginx:1.20
volumeMounts:
- name: cache
mountPath: /cache
- name: sidecar
image: busybox:1.35
volumeMounts:
- name: cache
mountPath: /shared
volumes:
- name: cache
emptyDir:
sizeLimit: 1Gi # Optional: size limit

Characteristics:

  • Automatically created when the Pod starts
  • Shared between all containers in the Pod
  • Stored on the node (local disk)
  • Deleted when the Pod terminates

Use Cases:

  • Temporary cache
  • Temporary files shared between containers
  • Intermediate work data

tmpfs (RAM)

A tmpfs volume is stored in memory (RAM) instead of disk.

apiVersion: v1
kind: Pod
metadata:
name: tmpfs-pod
spec:
containers:
- name: app
image: nginx:1.20
volumeMounts:
- name: tmpfs-volume
mountPath: /tmp
volumes:
- name: tmpfs-volume
emptyDir:
medium: Memory
sizeLimit: 512Mi # RAM limit

Characteristics:

  • Stored in RAM (very fast)
  • Deleted on node restart
  • Limited by available RAM
  • Faster than emptyDir (disk)

Use Cases:

  • High-performance in-memory cache
  • Sensitive temporary files (deleted on restart)
  • Data that should not persist

Caution: Use with care as it consumes RAM.


Comparison: emptyDir vs tmpfs

CharacteristicemptyDirtmpfs
StorageLocal diskRAM
PerformanceMediumVery high
PersistenceSurvives container restartDeleted on node restart
LimitDisk spaceAvailable RAM
Use CaseCache, temporary filesHigh-performance cache, sensitive data

Special Volumes

ConfigMap and Secret as Volumes

ConfigMaps and Secrets can be mounted as volumes.

apiVersion: v1
kind: Pod
metadata:
name: config-volume-pod
spec:
containers:
- name: app
image: nginx:1.20
volumeMounts:
- name: config
mountPath: /etc/config
readOnly: true
- name: secrets
mountPath: /etc/secrets
readOnly: true
volumes:
- name: config
configMap:
name: app-config
- name: secrets
secret:
secretName: app-secrets
defaultMode: 0400

Result: Each key becomes a file in the mounted directory.


Persistent Volumes (Preview)

Persistent volumes (PV/PVC) will be covered in detail in Chapter 6.2.

Key Difference:


Complete Example: Shared Cache

apiVersion: v1
kind: Pod
metadata:
name: cache-example
spec:
containers:
# Main container
- name: web-server
image: nginx:1.20
volumeMounts:
- name: cache
mountPath: /var/cache/nginx
- name: tmp
mountPath: /tmp

# Sidecar container for cache
- name: cache-warmup
image: busybox:1.35
command: ["/bin/sh", "-c", "while true; do echo 'Cache data' > /cache/data.txt; sleep 60; done"]
volumeMounts:
- name: cache
mountPath: /cache

volumes:
# Shared cache (disk)
- name: cache
emptyDir:
sizeLimit: 500Mi

# Temporary files (RAM)
- name: tmp
emptyDir:
medium: Memory
sizeLimit: 100Mi

Best Practices

1. Choose the Right Type

  • emptyDir: Cache, temporary files, shared data between containers
  • tmpfs: High-performance cache, sensitive temporary data
  • PV/PVC: Important data that must persist

2. Size Limits

Always define sizeLimit to avoid:

  • Disk space exhaustion (emptyDir)
  • RAM exhaustion (tmpfs)

3. Cleanup

Ephemeral volumes are automatically cleaned up, but:

  • Ensure applications clean up their temporary files
  • Monitor disk/RAM space usage

Useful Commands

Check a Pod's Volumes

kubectl describe pod empty-dir-pod

View Volume Usage

kubectl exec empty-dir-pod -c app -- df -h

Test a Volume

# Write to the volume
kubectl exec empty-dir-pod -c app -- sh -c "echo 'test' > /cache/test.txt"

# Read from the volume
kubectl exec empty-dir-pod -c app -- cat /cache/test.txt

Summary

In this chapter, you learned:

Ephemeral volumes: emptyDir (disk) and tmpfs (RAM)
emptyDir: Created at startup, deleted at termination, shared between containers
tmpfs: Stored in RAM, very fast, deleted on node restart
Use cases: Cache, temporary files, shared data
Limitations: Limited size, lifetime tied to the Pod
Best practices: Define sizeLimit, choose based on needs


Next Steps

Chapter 6.2: PersistentVolumes and PersistentVolumeClaims
Lab 6.1: Ephemeral Volumes and emptyDir


Chapter created: December 2024