Challenge
DifficultyEasy
Learner activity No attempts yet

Understand Volumes in Kubernetes: emptyDir vs hostPath

Omkar Shelke
by  Omkar Shelke · on
Kubernetes
Run a Pod with an emptyDir and a hostPath volume, then go to the node and find where Kubernetes keeps the emptyDir data. Practice how the two volume types store data on the node and who manages each directory.

Scenario

Your team is choosing volume types for a new workload. Before they decide, they want to see where Kubernetes really keeps the data of an emptyDir volume and of a hostPath volume, and who manages each directory on the node.

You will run a small volume-explorer Pod with both volume types, then go to the node where it runs and find the directory that holds the emptyDir data.

The cluster has two nodes: cplane-01, the control plane, and node-01, a worker. kubectl is configured on cplane-01.

emptyDir and hostPath volumes on a node

Figure: emptyDir and hostPath volume architecture showing mount points, lifecycle, and storage locations on the Kubernetes node.


Task

Create a Pod named volume-explorer in the namespace storage-lab:

SettingRequirement
Container nameexplorer
Imagebusybox:stable
Volume cache-volemptyDir, mounted at /cache
Volume node-volhostPath with the path /tmp/k8s-node-data, mounted at /node-data

Use this command for the container. It writes one file to each volume and keeps the container running:

command:
  - /bin/sh
  - -c
  - |
    echo 'cache volume data'    > /cache/cache.log &&
    echo 'hostpath volume data' > /node-data/node.log &&
    sleep 3600

When the Pod is running, find the directory on the node where Kubernetes keeps the data of the emptyDir volume cache-vol. Enter its full path in the last test case below.


Hint 1: Defining the volumes

A volume is defined once under spec.volumes, and each container that uses it mounts it by name under volumeMounts:

kubectl explain pod.spec.volumes.emptyDir
kubectl explain pod.spec.volumes.hostPath
kubectl explain pod.spec.containers.volumeMounts

Docs:

Hint 2: Finding the emptyDir data on the node

The kubelet on the node where the Pod runs creates the emptyDir directory. Find out which node that is:

kubectl get pod <name> -n <namespace> -o wide

The kubelet keeps one directory per Pod, named after the Pod UID, under its root directory /var/lib/kubelet. The UID is in the Pod metadata:

kubectl explain pod.metadata.uid

Connect to the node with ssh <node>. The first connection asks you to confirm the host key of the node. The kubelet directory is readable only by root, so use sudo to look inside it.

Docs:

Hint 3: When the Pod does not start

The volumes and mounts of a Pod cannot be changed after it is created. Fix the manifest, delete the Pod and create it again. The events at the end of the describe output show why a Pod is not running:

kubectl describe pod <name> -n <namespace>

âš’ Test Cases

Problem
Solution
Start
Black Friday