πŸ“šBook Signing at KubeCon EU 2026Meet us at Booking.com HQ (Mon 18:30-21:00) & vCluster booth #521 (Tue 24 Mar, 12:30-1:30pm) β€” free book giveaway!RSVP Booking.com Event
Storage intermediate ⏱ 10 minutes K8s 1.21+

Kubernetes hostPath Volume Guide

Use hostPath volumes to mount node filesystem paths into pods. Types, security risks, use cases for DaemonSets, and safer alternatives like local PVs.

By Luca Berton β€’ β€’ πŸ“– 5 min read

πŸ’‘ Quick Answer: hostPath mounts a file or directory from the host node’s filesystem into a pod. Use only for DaemonSets (log collectors, monitoring agents) β€” never for regular workloads. Prefer local PersistentVolumes or CSI drivers for production storage.

The Problem

Some workloads need direct access to the node filesystem:

  • Log collectors reading /var/log/containers/
  • Monitoring agents accessing /sys or /proc
  • GPU device plugins accessing /dev/nvidia*
  • Container runtime socket (/var/run/containerd/containerd.sock)

But hostPath has serious risks: pod can access any file on the node, breaks portability, and bypasses storage lifecycle management.

The Solution

Basic hostPath Volume

apiVersion: v1
kind: Pod
metadata:
  name: log-reader
spec:
  containers:
    - name: reader
      image: busybox
      command: ["tail", "-f", "/host-logs/syslog"]
      volumeMounts:
        - name: host-logs
          mountPath: /host-logs
          readOnly: true
  volumes:
    - name: host-logs
      hostPath:
        path: /var/log
        type: Directory

hostPath Types

volumes:
  - name: vol
    hostPath:
      path: /data/myapp
      type: DirectoryOrCreate  # Creates if missing (0755, same owner as kubelet)

# Available types:
# ""                 - No check (default, dangerous)
# DirectoryOrCreate  - Creates directory if not exists
# Directory          - Must already exist
# FileOrCreate       - Creates file if not exists
# File               - Must already exist
# Socket             - Unix socket must exist
# CharDevice         - Character device must exist
# BlockDevice        - Block device must exist

DaemonSet Use Cases (Legitimate)

# Fluentd/Vector log collector
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: log-collector
spec:
  selector:
    matchLabels:
      app: log-collector
  template:
    metadata:
      labels:
        app: log-collector
    spec:
      containers:
        - name: vector
          image: timberio/vector:0.42.0-alpine
          volumeMounts:
            - name: varlog
              mountPath: /var/log
              readOnly: true
            - name: containers
              mountPath: /var/lib/docker/containers
              readOnly: true
            - name: machine-id
              mountPath: /etc/machine-id
              readOnly: true
      volumes:
        - name: varlog
          hostPath:
            path: /var/log
            type: Directory
        - name: containers
          hostPath:
            path: /var/lib/docker/containers
            type: Directory
        - name: machine-id
          hostPath:
            path: /etc/machine-id
            type: File
# Node exporter (Prometheus)
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: node-exporter
spec:
  template:
    spec:
      hostNetwork: true
      hostPID: true
      containers:
        - name: exporter
          image: prom/node-exporter:v1.8.2
          args:
            - --path.procfs=/host/proc
            - --path.sysfs=/host/sys
            - --path.rootfs=/host/root
          volumeMounts:
            - name: proc
              mountPath: /host/proc
              readOnly: true
            - name: sys
              mountPath: /host/sys
              readOnly: true
            - name: root
              mountPath: /host/root
              readOnly: true
              mountPropagation: HostToContainer
      volumes:
        - name: proc
          hostPath:
            path: /proc
            type: Directory
        - name: sys
          hostPath:
            path: /sys
            type: Directory
        - name: root
          hostPath:
            path: /
            type: Directory

Safer Alternative: Local PersistentVolume

# For workloads that need node-local SSD storage
apiVersion: v1
kind: PersistentVolume
metadata:
  name: local-ssd-pv
spec:
  capacity:
    storage: 100Gi
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Delete
  storageClassName: local-ssd
  local:
    path: /mnt/ssd/data
  nodeAffinity:
    required:
      nodeSelectorTerms:
        - matchExpressions:
            - key: kubernetes.io/hostname
              operator: In
              values:
                - worker-01

Architecture

graph TD
    A[Pod] -->|volumeMount| B[hostPath Volume]
    B -->|direct access| C[Node Filesystem]
    
    D[Pod Security Admission] -->|restrict| B
    E[Pod Security Policy] -->|allowedHostPaths| B
    
    F[Safer Alternatives]
    F --> G[emptyDir - ephemeral]
    F --> H[local PV - managed lifecycle]
    F --> I[CSI driver - cloud storage]

Common Issues

IssueCauseFix
Permission deniedContainer runs as non-rootSet securityContext.runAsUser: 0 or fix host permissions
Pod scheduled on wrong nodehostPath data is node-localUse nodeSelector or nodeAffinity
Data lost on pod reschedulePod moved to different nodeUse PVC with network storage instead
Security policy blocks hostPathPSA restricted profileUse baseline or privileged for DaemonSets only
Disk fills up nodeNo quota on hostPathUse local PV with capacity enforcement

Best Practices

  1. Always set readOnly: true unless you specifically need write access
  2. Use specific type β€” Directory or File validates path exists at schedule time
  3. Restrict with Pod Security Admission β€” only allow hostPath for system DaemonSets
  4. Prefer local PersistentVolumes β€” proper lifecycle, capacity tracking, scheduling
  5. Never use hostPath for application data β€” breaks portability and HA

Key Takeaways

  • hostPath gives pods direct node filesystem access β€” powerful but dangerous
  • Legitimate uses: log collectors, monitoring agents, device plugins (always as DaemonSets)
  • Always use readOnly: true and specific type fields for safety
  • For application storage, use local PVs (managed lifecycle) or network-attached CSI volumes
  • Pod Security Admission restricted profile blocks hostPath β€” use privileged namespace for system DaemonSets
#hostpath #volumes #storage #daemonset #security #local-storage
Luca Berton
Written by Luca Berton

Principal Solutions Architect specializing in Kubernetes, AI/GPU infrastructure, and cloud-native platforms. Author of Kubernetes Recipes and creator of CopyPasteLearn courses.

Kubernetes Recipes book cover

Want More Kubernetes Recipes?

This recipe is from Kubernetes Recipes, our 750-page practical guide with hundreds of production-ready patterns.

Luca Berton Ansible Pilot Ansible by Example Open Empower K8s Recipes Terraform Pilot CopyPasteLearn ProteinLens