Sub topic 7
Sharing data with volumes
This chapter assumes you can read a Pod manifest, know what a container is, and have run kubectl apply. It covers how containers in one Pod share files, and where that data lives.
Why Pods need volumes
A container's filesystem is created from its image and disappears when the container restarts. A restarted container starts again from the image, so anything it wrote is gone. A volume is a directory that Kubernetes attaches to the Pod. It can outlive a container restart and can be mounted by several containers at once.
Using a volume takes two steps:
- Declare the volume under
spec.volumes, with a name and a type. - Mount it into each container under
volumeMounts, using the same name and amountPath.
The emptyDir volume
An emptyDir volume starts empty when the Pod is assigned to a node. It lasts as long as the Pod stays on that node. Container restarts do not delete it. Deleting the Pod does. That makes it a good fit for scratch space and for handing files between containers.
The Pod below runs two containers that mount the same volume at the same path. The app container appends a timestamp every five seconds. The log-reader container follows the file.
apiVersion: v1
kind: Pod
metadata:
name: log-demo
spec:
volumes:
- name: logs
emptyDir: {}
containers:
- name: app
image: busybox:1.36
command:
- sh
- -c
- while true; do date >> /var/log/app/app.log; sleep 5; done
volumeMounts:
- name: logs
mountPath: /var/log/app
- name: log-reader
image: busybox:1.36
command:
- sh
- -c
- touch /var/log/app/app.log; tail -F /var/log/app/app.log
volumeMounts:
- name: logs
mountPath: /var/log/appThe log-reader container runs touch first. The two containers start at about the same time, and tail would otherwise have no file to open. Using tail -F rather than -f makes it keep retrying if the file is replaced.
To check it, apply the manifest and read the second container's output:
kubectl apply -f log-demo.yaml
kubectl logs log-demo -c log-readerYou should see a new date line every five seconds.
Mounting rules and common errors
- Every
volumeMounts[].namemust match an entry inspec.volumes. A mismatch makes the API server reject the Pod. - Each container chooses its own
mountPath. The two containers do not have to use the same path, as long as they use the same volume name. - A mount hides whatever the image had at that path. Mounting an empty volume over
/usr/share/nginx/htmlleaves nginx with no default page. - Add
readOnly: trueto a mount when a container only needs to read. - Setting
emptyDir.medium: Memorybacks the volume with RAM. Its contents count against the container's memory limit.
Volumes and init containers
Volumes are also how an init container passes work to the main container. The init container writes into an emptyDir volume, and the main container mounts that volume after the init container exits. The Init containers chapter builds on this pattern.
Exercise
A container writes a file to /cache, which is an emptyDir mount. The container crashes and Kubernetes restarts it. Is the file still there? What if the Pod is deleted and recreated?
Show answer
After the container restart, the file is still there, because an emptyDir lives as long as the Pod stays on its node. After the Pod is deleted and recreated, the file is gone, because the new Pod gets a new, empty emptyDir.
Exercise
Your Pod mounts the volume "html" at /usr/share/nginx/html, but nginx returns 403 Forbidden because the directory is empty. Name one way to fix this.
Show answer
Put content into the volume before nginx starts, for example with an init container that writes index.html into the same volume. Alternatively, mount the volume at a different path so the image's default files are not hidden.