HostPath Volume

A hostPath volume mounts a file or directory from the host node's filesystem into your Pod. This is not something that most Pods will need, but it offers a powerful escape hatch for some applications.

For example, some uses for a hostPath are:

  • running a container that needs access to Docker internals, use a hostPath of /var/lib/docker
  • running cAdvisor in a Container; use a hostPath of /sys
  • allowing a Pod to specify whether a given hostPath should exist prior to the Pod running, whether it should be created, and what it should exist as

In addition to the required path property, you can optionally specify a type for a hostPath volume. Below is the example of a hostpath volume

vim hostvolume.yaml
apiVersion: v1
kind: Pod
metadata:
  name: test-vol1
spec:
  containers:
    - image: coolgourav147/nginx-custom
      name: test-container
      volumeMounts:
        - mountPath: /data
          name: first-volume
  volumes:
    - name: first-volume
      hostPath:
        path: /tmp/data
kubectl appy -f hostvolume.yaml

now execute the newly created container and write some data into it, these files will be created in the host machine .tmp/data folder. Now we can delete the pod

and recreate it. Once the pod is recreated we can again execute the container and will be able to see the data is persistent from deleted pod in earlier step

Note: this host path is useful for single-node clusters only