Sub topic 2
Passing Data Between Containers with emptyDir
This chapter assumes you can write a Pod manifest, apply it with kubectl, and read container logs. It covers how containers in one Pod exchange files through an emptyDir volume. Later chapters build on this for init containers and sidecars.
Why containers need a shared volume
Each container in a Pod has its own filesystem, built from its image. A file written in one container is not visible in another. Containers in a Pod do share a network namespace, so they can talk over localhost. They do not share files unless you mount the same volume into each of them.
An emptyDir is a volume that Kubernetes creates empty when the Pod is assigned to a node. Every container that mounts it sees the same directory. Kubernetes deletes it when the Pod is removed from the node.
How emptyDir works
You declare the volume once under spec.volumes. Each container then mounts it under volumeMounts, and each mount can use a different path.
- The volume starts empty.
- It lives as long as the Pod lives on that node.
- It survives container crashes and restarts, because the Pod is still there.
- It is lost if the Pod is deleted or rescheduled to another node.
By default the data is stored on the node's disk. Setting medium: Memory stores it in RAM (a tmpfs). Memory-backed data counts against container memory limits. You can cap the size with sizeLimit. If the volume exceeds the limit, the kubelet evicts the Pod.
A worked example
The next Pod has an init container that writes an HTML file. The nginx container then serves it. The two containers mount the same volume at different paths.
apiVersion: v1
kind: Pod
metadata:
name: web-with-content
spec:
initContainers:
- name: fetch-content
image: busybox:1.36
command: ['sh', '-c', 'echo "<h1>Prepared by an init container</h1>" > /work/index.html']
volumeMounts:
- name: content
mountPath: /work
containers:
- name: nginx
image: nginx:1.27
volumeMounts:
- name: content
mountPath: /usr/share/nginx/html
volumes:
- name: content
emptyDir: {}The volume is named content. The init container sees it at /work. The nginx container sees the same files at /usr/share/nginx/html. The init container finishes first, so the file already exists when nginx starts.
To check it, apply the manifest and request the page from inside the Pod:
kubectl apply -f web-with-content.yaml
kubectl wait --for=condition=Ready pod/web-with-content
kubectl exec web-with-content -c nginx -- cat /usr/share/nginx/html/index.htmlThe last command prints the heading written by the init container.
Mounting over existing files
A mount hides whatever the image already has at that path. The empty volume replaces the contents of /usr/share/nginx/html, including the default nginx page. This is why the example works. It is also a common source of bugs. If you mount an emptyDir over a directory that holds files your app needs, those files seem to vanish. Use an init container to copy them into the volume, or mount at a different path.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| File not found in the second container | The volume is mounted in only one container | Add a `volumeMounts` entry to every container that needs it |
| Pod rejected with a volume error | `volumeMounts.name` does not match any name in `volumes` | Make the names identical |
| Image files missing after mount | The mount hides the image's directory | Mount at another path or copy the files in first |
| Data gone after a restart of the Pod | The Pod was deleted or rescheduled | Use a PersistentVolumeClaim for data that must last |
| Pod evicted | `sizeLimit` exceeded, or node disk pressure | Raise the limit or write less data |
File ownership
Files are created with the user ID of the process that writes them. If the init container runs as root and the app runs as a non-root user, the app may not be able to write to the directory. Setting securityContext.fsGroup at the Pod level makes Kubernetes apply a shared group to the volume so that all containers can access it.
Exercise
An init container writes /work/config.json to an emptyDir volume, but the app container reports that /etc/app/config.json does not exist. The volume is declared under spec.volumes and mounted in the init container at /work. What is the most likely mistake, and how do you fix it?
Show answer
The app container has no volumeMounts entry for the volume, so it cannot see the files. Add a mount with the same volume name and mountPath: /etc/app to the app container. Keep in mind that the mount will hide any files the image already holds at /etc/app.
Exercise
A Pod is deleted and recreated by a Deployment, and the data in its emptyDir is gone. Is this a bug? What would you use for data that must outlive the Pod?
Show answer
It is not a bug. An emptyDir lives only as long as the Pod on that node. Use a PersistentVolumeClaim (or another persistent volume type) for data that must survive Pod deletion or rescheduling.