liveBook · Manning

Emptydir volume

As explained in Kubernetes in Action, Second Edition

An emptyDir volume is a Kubernetes volume that starts empty, persists for the lifetime of its pod, and can be shared by containers in that pod.

An `emptyDir` volume is a directory created just before a pod’s containers start. It begins empty and keeps its files for the lifetime of that pod. Use it when a container needs temporary writable space, when files must survive a container restart, or when containers in the same pod need to share files. section 9.2 section 9.7

Define the volume under `spec.volumes`, then mount it in each container that needs access. The mount names the volume and sets a `mountPath` inside that container. Defining the volume alone does not make its files available to a container. section 9.2.1

Listing 9.2 The quiz pod with an emptyDir volume for the mongo container
apiVersion: v1
kind: Pod
metadata:
  name: quiz
spec:
  volumes:               #1
  - name: quiz-data      #1
    emptyDir: {}         #1
  containers:
  - name: quiz-api
    image: luksa/quiz-api:0.1
    ports:
    - name: http
      containerPort: 8080
  - name: mongo
    image: mongo
    volumeMounts:             #2
    - name: quiz-data         #2
      mountPath: /data/db     #2

The pod manifest defines `quiz-data` as an `emptyDir` and mounts it at `/data/db`, where MongoDB stores its files. section 9.2.1

Keep database files through a container restart

With MongoDB writing to `/data/db` on the volume, its files remain when the MongoDB container restarts. A check using `kubectl exec` and `mongosh` counts one question before the database server is shut down; after the pod reports one container restart, the same check still counts one. This does not make the data permanent: the directory is deleted when the pod terminates. section 9.2.1 section 9.7

Choose the volume’s behavior to match the task:

  • For local storage, leave `medium` unset to use the host node’s default medium, or set `medium: Memory` to keep files in a memory-backed filesystem. `sizeLimit` sets a size limit, such as `10Mi`. section 9.2.1
  • To supply files before regular containers start, mount the same `emptyDir` in an init container. It can copy a file into the volume; a regular container can then mount that volume at a different path, optionally read-only. section 9.2.2
Listing 9.5 Using an init container to initialize an emptyDir volume
apiVersion: v1
kind: Pod
metadata:
  name: quiz
spec:
  volumes:
  - name: initdb            #1
    emptyDir: {}            #1
  - name: quiz-data
    emptyDir: {}
  initContainers:
  - name: installer                                   #2
    image: luksa/quiz-initdb-script-installer:0.1     #2
    volumeMounts:                                     #2
    - name: initdb                                    #2
      mountPath: /initdb.d                            #2
  containers: #2
  - name: quiz-api
    image: luksa/quiz-api:0.1
    ports:
    - name: http
      containerPort: 8080
  - name: mongo
    image: mongo
    volumeMounts:
    - name: quiz-data
      mountPath: /data/db
    - name: initdb                                #3
      mountPath: /docker-entrypoint-initdb.d/     #3
      readOnly: true                              #3

This manifest uses one `emptyDir` for database files and another for a startup script copied by an init container and read by MongoDB. section 9.2.2

The boundary is the pod, not the individual container: multiple containers in that pod can mount the volume, but another pod cannot share its `emptyDir`. For storage that must persist beyond a pod’s lifetime, use a different volume arrangement, such as one backed by a PersistentVolumeClaim. section 9.2 section 9.1.2

How Kubernetes in Action, Second Edition covers it

How deep it goes
Works through pod volume and container mount definitions, storage options, restart verification, and init-container initialization; does not detail persistent storage setup.
Examples
Uses a MongoDB pod manifest, database checks before and after a restart, and an init container that copies a startup script.
What it assumes
Introduces pod volume definitions and container mounts while assuming familiarity with reading a Pod manifest.
What you can do after
Build it from scratch in a Pod manifest, mount it in containers, and verify that files survive a container restart.
How it connects
Connects pod volumes and volume mounts to container restarts, init containers, memory-backed storage, and PersistentVolumeClaims.

Also explained in

Compare how 3 books explain emptydir volume →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Kubernetes in Action, Second Edition. The section links point to the parts of the book it draws on.