Room 23
VOLUMES & MOUNTS
The Data Chamber
- Containers are ephemeral - when they restart, their filesystem is lost.
- Volumes persist data beyond container restarts. Mount volumes to save and share data.
📜 Mission Briefing
- Apply
shared-volume.yaml- a pod with two containers sharing anemptyDirvolume. - Container
writerwrites a message to/data/message.txtevery 5 seconds. - Container
readerreads from the same path. - Exec into the
readercontainer and read/data/message.txt. - The last line contains the password:
volumed7.
Key Commands
| Command | Purpose |
|---|---|
kubectl exec POD -c CONTAINER -- cat /path |
Read from volume |
kubectl describe pod NAME |
See volume mounts |
Volume Types
# emptyDir: Temporary storage, shared between containers in a pod
# Deleted when pod is removed
apiVersion: v1
kind: Pod
metadata:
name: shared-vol
spec:
containers:
- name: writer
image: busybox
command: ['sh', '-c', 'while true; do echo "volumed7" >> /data/message.txt; sleep 5; done']
volumeMounts:
- name: shared
mountPath: /data
- name: reader
image: busybox
command: ['sh', '-c', 'tail -f /data/message.txt']
volumeMounts:
- name: shared
mountPath: /data
volumes:
- name: shared
emptyDir: {} # Can also set sizeLimit and medium
# hostPath: Mounts a directory from the host node's filesystem
# Use with caution - breaks portability
apiVersion: v1
kind: Pod
metadata:
name: hostpath-pod
spec:
containers:
- name: app
image: busybox
volumeMounts:
- name: host-data
mountPath: /host-data
volumes:
- name: host-data
hostPath:
path: /tmp/data
type: DirectoryOrCreate # Directory, File, etc.
# Volume types:
# emptyDir: Temp dir, pod lifetime
# hostPath: Node filesystem mount
# configMap: Mount ConfigMap as files
# secret: Mount Secret as files
# persistentVolumeClaim: Persistent storage
# nfs: Network File System
# downwardAPI: Pod metadata as files
# projected: Combine multiple sources into one mount
Hints
Hint 1
Hint 2
The writer container writes the password to the shared emptyDir volume.
Hint 3
The password is volumed7.