Table of contents
Introduction
In Kubernetes, A HostPath volume is a type of volume that mounts a directory or file from the host node's filesystem into a Pod. It allows a Pod to access and share files with the underlying host machine.
When you use a HostPath volume, the file or directory specified in the volume configuration is mounted directly into the container running in the Pod. This means that the container has access to the files and directories on the host machine's filesystem.
The data stored in the HostPath
volume is retained even if the pod is terminated or rescheduled to a different node.
The pod has full read and write access to the files/directories on the host node's filesystem.
It can be used to share data between containers running on the same node.
Architecture Diagram
Lab
arun@Aruns-MacBook-Air ~ % kubectl get nodes
NAME STATUS ROLES AGE VERSION
master Ready control-plane 62d v1.26.3
worker01 Ready <none> 61d v1.26.3
worker02 Ready <none> 61d v1.26.3
apiVersion: apps/v1
kind: Deployment
metadata:
name: hostpath-deployment
spec:
replicas: 2
selector:
matchLabels:
app: hostpath-app
template:
metadata:
labels:
app: hostpath-app
spec:
affinity:
podAntiAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchExpressions:
- key: app
operator: In
values:
- hostpath-app
topologyKey: kubernetes.io/hostname
containers:
- name: my-container
image: nginx
volumeMounts:
- name: hostpath-volume
mountPath: /hostpath
volumes:
- name: hostpath-volume
hostPath:
path: /workervolume
type: Directory
In the above example, podAntiAffinity denotes the same pod will not schedule in the same worker nodes i.e. two pods will be scheduled in two worker nodes.
Let's create the volume in both worker nodes.
root@worker01:/# mkdir /workervolume
root@worker01:/# chmod -R 777 /workervolume
root@worker01:/# cd /workervolume
root@worker01:/workervolume# touch file1 file2
root@worker01:/workervolume# ls -lrt
total 0
-rw-r--r-- 1 root root 0 Jun 1 16:51 file2
-rw-r--r-- 1 root root 0 Jun 1 16:51 file1
root@worker02:~# mkdir /workervolume
root@worker02:~# chmod -R 777 /workervolume
root@worker02:~# cd /workervolume
root@worker02:/workervolume# touch file1 file2
root@worker02:/workervolume# ls -lrt
total 0
-rw-r--r-- 1 root root 0 Jun 1 16:51 file2
-rw-r--r-- 1 root root 0 Jun 1 16:51 file1
Deploy the hostpath example
arun@Aruns-MacBook-Air kubernetes_volumes % ka hostpath_example.yaml
deployment.apps/hostpath-deployment created
arun@Aruns-MacBook-Air kubernetes_volumes %
arun@Aruns-MacBook-Air kubernetes_volumes % kgp -o wide| grep -i hostpath
hostpath-deployment-6567776857-2ftvl 1/1 Running 0 3m28s 10.244.2.107 worker02 <none> <none>
hostpath-deployment-6567776857-d9gw6 1/1 Running 0 3m28s 10.244.1.82 worker01 <none> <none>
arun@Aruns-MacBook-Air kubernetes_volumes %
Hostpath volumes mounted into pod
arun@Aruns-MacBook-Air kubernetes_volumes % k exec -it hostpath-deployment-6567776857-2ftvl -- bash
root@hostpath-deployment-6567776857-2ftvl:/# ls -lrt hostpath/
total 0
-rw-r--r-- 1 root root 0 Jun 1 16:51 file2
-rw-r--r-- 1 root root 0 Jun 1 16:51 file1
root@hostpath-deployment-6567776857-2ftvl:/#
Even If we delete the deployment, Volume data's will be available forever.
root@worker01:/# ls -lrt /workervolume/
total 0
-rw-r--r-- 1 root root 0 Jun 1 16:51 file2
-rw-r--r-- 1 root root 0 Jun 1 16:51 file1
root@worker01:/#
Summary
If your application needs to interact with specific files or directories on the host node, such as reading log files, accessing host-specific devices, or reading configuration files, you can use HostPath
to mount those resources directly into the pod.
Instead of mounting the directory on each worker node. We can use common shared module concept called PV (Persistence Volume).
End.
Please provide your valuable feedback. Thanks