Skip to main content

Lab 6.1 - Ephemeral Volumes and emptyDir

Objectives

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

  • Create and use emptyDir volumes
  • Use tmpfs (RAM) volumes
  • Share volumes between containers in a Pod
  • Understand the ephemeral volume lifecycle
  • Compare emptyDir and tmpfs

Prerequisites

  • Functional Kubernetes cluster
  • kubectl configured
  • Understanding of Pods and volumes

Exercise 1: Basic emptyDir Volume

Create a Pod with emptyDir

Create pod-emptydir.yaml:

apiVersion: v1
kind: Pod
metadata:
name: emptydir-pod
spec:
containers:
- name: app
image: busybox:1.35
command: ["sh", "-c", "echo 'Hello' > /cache/data.txt && cat /cache/data.txt && sleep 3600"]
volumeMounts:
- name: cache
mountPath: /cache
volumes:
- name: cache
emptyDir: {}

Deploy and Verify

kubectl apply -f pod-emptydir.yaml
kubectl logs emptydir-pod
kubectl exec emptydir-pod -- ls -la /cache

Exercise 2: emptyDir with Size Limit

Create a Pod with Size Limit

Create pod-emptydir-limit.yaml:

apiVersion: v1
kind: Pod
metadata:
name: emptydir-limit-pod
spec:
containers:
- name: app
image: busybox:1.35
command: ["sh", "-c", "dd if=/dev/zero of=/cache/bigfile bs=1M count=100 && sleep 3600"]
volumeMounts:
- name: cache
mountPath: /cache
volumes:
- name: cache
emptyDir:
sizeLimit: 50Mi

Deploy and Observe

kubectl apply -f pod-emptydir-limit.yaml
kubectl get pod emptydir-limit-pod
# The Pod should fail because 100Mi > 50Mi

Exercise 3: Shared Volume Between Containers

Create a Multi-Container Pod

Create pod-shared-volume.yaml:

apiVersion: v1
kind: Pod
metadata:
name: shared-volume-pod
spec:
containers:
- name: writer
image: busybox:1.35
command: ["sh", "-c", "echo 'Data from writer' > /shared/data.txt && sleep 3600"]
volumeMounts:
- name: shared
mountPath: /shared
- name: reader
image: busybox:1.35
command: ["sh", "-c", "sleep 5 && cat /shared/data.txt && sleep 3600"]
volumeMounts:
- name: shared
mountPath: /shared
volumes:
- name: shared
emptyDir: {}

Deploy and Verify

kubectl apply -f pod-shared-volume.yaml
kubectl logs shared-volume-pod -c writer
kubectl logs shared-volume-pod -c reader

Exercise 4: tmpfs Volume (RAM)

Create a Pod with tmpfs

Create pod-tmpfs.yaml:

apiVersion: v1
kind: Pod
metadata:
name: tmpfs-pod
spec:
containers:
- name: app
image: busybox:1.35
command: ["sh", "-c", "echo 'Fast storage' > /tmp/data.txt && cat /tmp/data.txt && sleep 3600"]
volumeMounts:
- name: tmpfs-volume
mountPath: /tmp
volumes:
- name: tmpfs-volume
emptyDir:
medium: Memory
sizeLimit: 100Mi

Deploy and Verify

kubectl apply -f pod-tmpfs.yaml
kubectl logs tmpfs-pod
kubectl exec tmpfs-pod -- df -h /tmp

Exercise 5: Comparison emptyDir vs tmpfs

Create Two Pods for Comparison

Create pod-comparison.yaml:

apiVersion: v1
kind: Pod
metadata:
name: emptydir-bench
spec:
containers:
- name: app
image: busybox:1.35
command: ["sh", "-c", "time dd if=/dev/zero of=/cache/test bs=1M count=10 && sleep 3600"]
volumeMounts:
- name: cache
mountPath: /cache
volumes:
- name: cache
emptyDir: {}
---
apiVersion: v1
kind: Pod
metadata:
name: tmpfs-bench
spec:
containers:
- name: app
image: busybox:1.35
command: ["sh", "-c", "time dd if=/dev/zero of=/tmp/test bs=1M count=10 && sleep 3600"]
volumeMounts:
- name: tmpfs-vol
mountPath: /tmp
volumes:
- name: tmpfs-vol
emptyDir:
medium: Memory

Compare Performance

kubectl apply -f pod-comparison.yaml
kubectl logs emptydir-bench
kubectl logs tmpfs-bench
# Compare write times

Exercise 6: Real-World Use Case - Cache

Create a Pod with Cache

Create pod-cache.yaml:

apiVersion: v1
kind: Pod
metadata:
name: cache-pod
spec:
containers:
- name: web-server
image: nginx:1.20
volumeMounts:
- name: cache
mountPath: /var/cache/nginx
- name: cache-warmup
image: busybox:1.35
command: ["sh", "-c", "while true; do echo 'Cache data' > /cache/data.txt; sleep 60; done"]
volumeMounts:
- name: cache
mountPath: /cache
volumes:
- name: cache
emptyDir:
sizeLimit: 500Mi

Deploy and Verify

kubectl apply -f pod-cache.yaml
kubectl exec cache-pod -c web-server -- ls -la /var/cache/nginx
kubectl exec cache-pod -c cache-warmup -- cat /cache/data.txt

Cleanup

kubectl delete pod emptydir-pod emptydir-limit-pod shared-volume-pod tmpfs-pod emptydir-bench tmpfs-bench cache-pod

Reflection Questions

  1. What is the main difference between emptyDir and tmpfs?
  2. When would you use tmpfs instead of emptyDir?
  3. Why define a sizeLimit for emptyDir volumes?
  4. What happens to data in an emptyDir volume when the Pod is deleted?

Bonus Challenge

Create a Pod with three containers that share an emptyDir volume:

  • One container that writes data
  • One container that reads and processes the data
  • One container that monitors the volume size

Lab created: December 2024