We already know kubernetes provides us with multiple ways of saving data on the disk. We have seen volume types using emptyDir and memory. The other and most used volume type is the Hostpath. In this article we will see how we can use a host path on disk to mount on to the pod and save data from the pod to the host machine.
The Host path details are defined in the volumes and attached to the pod using the VolumeMounts. Check the below example,
[jagadishmanchala@Jagadish-theOne:k8s] cat hostpath-volume.yml
apiVersion: v1
kind: Pod
metadata:
name: myapp
spec:
containers:
- name: my-app
image: nginx
ports:
- containerPort: 8080
volumeMounts:
- name: my-volume
mountPath: /app
volumes:
- name: my-volume
hostPath:
path: /Volumes/Working/k8s/test
In the above code, we are using the hostpath /Volumes/Working/k8s/test directory on the host machine to be mounted to the container on /app location. The name of the mount is my-volume
Run the code to see if the pod is running as below,
[jagadishmanchala@Jagadish-theOne:k8s] kubectl get pods
NAME READY STATUS RESTARTS AGE
myapp 1/1 Running 0 30s
Login to the pod and create few files for testing as below,
[jagadishmanchala@Jagadish-theOne:k8s] kubectl exec myapp -c my-app -it -- bash
root@myapp:/# df -hT
Filesystem Type Size Used Avail Use% Mounted on
overlay overlay 59G 4.5G 51G 8% /
tmpfs tmpfs 64M 0 64M 0% /dev
tmpfs tmpfs 987M 0 987M 0% /sys/fs/cgroup
grpcfuse fuse.grpcfuse 47G 33G 14G 71% /app
/dev/vda1 ext4 59G 4.5G 51G 8% /etc/hosts
shm tmpfs 64M 0 64M 0% /dev/shm
tmpfs tmpfs 987M 12K 987M 1% /run/secrets/kubernetes.io/serviceaccount
tmpfs tmpfs 987M 0 987M 0% /sys/firmware
root@myapp:/# cd /app/
root@myapp:/app# touch hai hello
root@myapp:/app# exit
exit
Now check on the host path to confirm as below,
[jagadishmanchala@Jagadish-theOne:k8s] ll test/
total 0
-rw-r--r-- 1 jagadishmanchala staff 0 Dec 28 12:30 hai
-rw-r--r-- 1 jagadishmanchala staff 0 Dec 28 12:30 hello
Hope this helps in understanding the hostpath Volume.
Such an amazing write ups available here. Good stuff with great quality content. informative and knowledgeable content see after long time . thank you for sharing creative and wonderful contents here.
ReplyDeleteFood Technology Courses after 12th