What is the difference between emptyDir and hostPath?

· Category: Kubernetes

Short answer

emptyDir creates a temporary directory tied to the Pod lifecycle, while hostPath mounts a directory from the host node filesystem. emptyDir is portable and ephemeral; hostPath is persistent and node-specific.

Key differences

  • emptyDir: Created empty when the Pod starts, deleted when the Pod stops, portable across nodes.
  • hostPath: Maps an existing host directory, persists across Pod restarts, tied to a specific node.

Example

volumes:
- name: scratch
  emptyDir: {}
- name: host-data
  hostPath:
    path: /data
    type: Directory

When to use each

  • Use emptyDir for temporary files, caches, and shared scratch space.
  • Use hostPath only when you need direct host filesystem access, such as for node-level logs or device access.

Common issues

  • hostPath breaks Pod portability and can cause scheduling issues.
  • hostPath exposes the host filesystem, creating security risks.
  • emptyDir is deleted on Pod termination, which can lose unsaved data.