hostPath Volumes
A hostPath volume mounts a specific directory from the node straight into a Pod.
Sometimes you want a Pod to use an exact folder that already exists on the node, such as a mounted USB drive or a config directory. A hostPath volume does this directly, without a PVC or provisioner.
Use with care
- The Pod is pinned to whichever node holds that path.
- It bypasses Kubernetes storage management, so it is less portable.
- It can be a security risk if the path exposes sensitive host files.
hostPath is common on edge devices where you know the hardware, but avoid it for portable production apps.
Example
apiVersionv1
kindPod
metadata
namelogger
spec
containers
nameapp
imagebusybox
command"sh" "-c" "echo hi >> /data/log.txt; sleep 3600"
volumeMounts
namehost-data
mountPath/data
volumes
namehost-data
hostPath
path/opt/app-data
typeDirectoryOrCreateWhen to use it
- A developer mounts the Docker socket from the host into a CI runner pod so the pod can build container images using the node's Docker daemon.
- An operator mounts /etc/ssl/certs from a node into a pod to share the host's certificate bundle without baking certs into the image.
- A log aggregator pod mounts /var/log from the host to collect and forward system logs without requiring a dedicated log daemon on the node.
More examples
Mount a host directory into a Pod
Mounts the node's /var/log directory into the container so it can read host-level log files directly.
volumes:
- name: host-logs
hostPath:
path: /var/log
type: Directory
containers:
- name: log-reader
image: busybox
command: ['sh', '-c', 'tail -f /host/syslog']
volumeMounts:
- name: host-logs
mountPath: /hostUse hostPath for Docker socket
Exposes the node's Docker socket inside the container, enabling Docker-in-Docker builds from a pod.
volumes:
- name: docker-sock
hostPath:
path: /var/run/docker.sock
type: Socket
containers:
- name: builder
image: docker:dind
volumeMounts:
- name: docker-sock
mountPath: /var/run/docker.sockCreate a hostPath volume with type check
Uses DirectoryOrCreate so k3s creates the host directory automatically if it is missing when the pod starts.
volumes:
- name: config-dir
hostPath:
path: /etc/app-config
type: DirectoryOrCreate
# type: DirectoryOrCreate creates the dir if it does not exist
Discussion