Cloud Native 13 min read

Mastering Kubernetes emptyDir and hostPath Volumes: A Step‑by‑Step Guide

This tutorial explains the lifecycle and use‑cases of Kubernetes emptyDir and hostPath volumes, shows how to configure them in pod specs, demonstrates a sidecar pattern for temporary data sharing, and provides complete YAML and kubectl commands for creation, inspection, and troubleshooting.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Mastering Kubernetes emptyDir and hostPath Volumes: A Step‑by‑Step Guide

Kubernetes supports several storage volume types; the emptyDir volume shares the lifecycle of its owning Pod and cannot persist data beyond the Pod, making it suitable for caching or temporary storage. However, an emptyDir can be used with a gitRepo volume to copy data from a Git repository at Pod start, giving it a form of persistence.

emptyDir Volume

emptyDir is a temporary directory created when a Pod starts and deleted when the Pod is removed, similar to a Docker mount volume. It is defined under .spec.volumes.emptyDir and has two main fields: medium: storage medium type, default is default (node's storage) or Memory for an in‑memory tmpfs filesystem. sizeLimit: optional size limit for the volume; when medium is Memory, setting a limit is recommended.

1. Create Pod manifest

apiVersion: v1
kind: Pod
metadata:
  name: vol-emptydir-pod
spec:
  volumes:
  - name: html
    emptyDir: {}
  containers:
  - name: nginx
    image: nginx:latest
    volumeMounts:
    - name: html
      mountPath: /usr/share/nginx/html
  - name: pagegen
    image: alpine
    volumeMounts:
    - name: html
      mountPath: /html
    command: ["/bin/sh", "-c"]
    args:
    - while true; do echo $(hostname) $(date) >> /html/index.html; sleep 10; done

The volume named html is mounted into both the nginx container (serving files) and the pagegen sidecar container (appending timestamped lines every 10 seconds). Accessing the nginx service shows a continuously updating index.html.

2. Apply the Pod kubectl apply -f vol-emptydir.yaml 3. Inspect Pod status kubectl describe pod vol-emptydir-pod 4. Access the nginx service curl http://<pod-ip> 5. Enter containers

kubectl exec -it pod/vol-emptydir-pod -c nginx -- /bin/sh
# ls /usr/share/nginx/html
# head -3 /usr/share/nginx/html/index.html

kubectl exec -it pod/vol-emptydir-pod -c pagegen -- /bin/sh
# ls /html
# head -3 /html/index.html

Using a sidecar container like pagegen demonstrates how emptyDir enables shared, mutable data between containers.

hostPath Volume

The hostPath volume type mounts a directory or file from the node’s filesystem into a Pod. Unlike emptyDir, hostPath persists beyond the Pod’s lifecycle but is tied to the specific node, making it useful for system‑level tasks that need direct node access.

hostPath has two required fields: path: absolute path on the node. type: one of DirectoryOrCreate, Directory, FileOrCreate, File, Socket, CharDevice, BlockDevice, which controls creation and validation behavior.

Example hostPath Pod (log collector)

apiVersion: v1
kind: Pod
metadata:
  name: vo-hostpath-pod
spec:
  containers:
  - name: filebeat
    image: ikubernetes/filebeat:5.6.7-alpine
    env:
    - name: REDIS_HOST
      value: redis.ilinux.io:6379
    - name: LOG_LEVEL
      value: info
    volumeMounts:
    - name: varlog
      mountPath: /var/log
    - name: varlibdockercontainers
      mountPath: /var/lib/docker/containers
    - name: socket
      mountPath: /var/run/docker.sock
  volumes:
  - name: varlog
    hostPath:
      path: /var/log
      type: DirectoryOrCreate
  - name: varlibdockercontainers
    hostPath:
      path: /var/lib/docker/containers
      type: Directory
  - name: socket
    hostPath:
      path: /var/run/docker.sock
      type: Socket

Steps to create and inspect the Pod:

# Create manifest
kubectl apply -f vo-hostpath.yaml

# Describe volumes
kubectl describe pod vo-hostpath-pod | grep -A5 "Volumes"

# Exec into pod to view mounted paths
kubectl exec -it pod/vo-hostpath-pod -- /bin/sh
# ls /var/log
# ls /var/lib/docker/containers
# ls -l /var/run/docker.sock

hostPath is often used by DaemonSet controllers that run on every node to collect system logs or metrics. When using hostPath, ensure the required files or directories exist on each node, consider permission issues (default root ownership), and remember that the volume is node‑specific, so it is not suitable for stateless workloads that may be rescheduled to different nodes; in such cases, networked persistent volumes are preferred.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Cloud NativeKubernetesemptyDirHostPathVolumescontainer storage
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.