tezvyn:

Sharing ephemeral cache between containers in a Pod

AI-drafted, machine-checkedSource: interviewintermediate
WHAT IT TESTS

Choosing the right ephemeral volume.

OUTLINE

Use an emptyDir volume defined in the Pod spec and mounted into each container at the cache path; it is created with the Pod and deleted when the Pod is removed.

WHAT THIS TESTS Matching storage type to the data's lifecycle and sharing requirements. The data is temporary, Pod-scoped, and shared across containers, which points squarely at emptyDir.

A GOOD ANSWER COVERS emptyDir is an ephemeral volume created when a Pod is assigned to a node and deleted forever when the Pod is removed from that node. To share it, you define one emptyDir entry under the Pod's volumes field, then give each container a volumeMount that references that volume name and mounts it at the cache directory. Because both containers mount the same volume, they read and write the same files. Optionally set medium: Memory so the directory is backed by tmpfs for faster, RAM-based caching.

COMMON WRONG ANSWERS Using a PersistentVolumeClaim, which is designed to survive Pod restarts and rescheduling, the opposite of what is wanted. Using hostPath, which binds data to a specific node's filesystem and leaks state. Giving each container its own separate volume, defeating the sharing requirement.

LIKELY FOLLOW-UPS What happens on container restart within the same Pod? emptyDir data survives a container crash, only vanishing when the whole Pod is removed. Memory-backed risks? tmpfs counts against the Pod's memory. Need persistence across Pod restarts? Use a PVC instead.

ONE CONCRETE EXAMPLE A Pod runs an app container and a sidecar that both use /cache. The Pod spec lists a volume named cache of type emptyDir, and both containers mount it at /cache. The sidecar writes preprocessed files there and the app reads them. When the Pod is deleted, the cache is discarded automatically with no cleanup needed.

Read the original → kubernetes.io

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.