Kubernetes hostPath volume helps us to persist volume contents even after pod deleted from the worker node.
K8 hostPath volume mounts a file or directory from the worker node filesystem into the pod.
A pod running on the same worker node can only mount to the file/directory of that node.
- It is useful when the container wants to access docker system files from the host (i.e., /var/lib/docker)
- It is useful when the container needs to access kubeconfig file (or) CA certificates (or) /var/logs from the host
- It is useful when the container needs to access host /sys files for cAdvisor
- It is useful when the container wants to check given path exists in the host before running
Type |
Description |
Directory |
A directory
must exist in the specified path on the host |
DirectoryOrCreate |
An empty
directory will be created when the specified path does not exist on the host |
File |
A file must
exist in the specified path on the host |
FileOrCreate |
An empty file
will be created when the specified path does not exist on the host |
Socket |
A UNIX socket
must exist in the specified path |
apiVersion: v1
kind: Pod
metadata:
name: pod-vol-hostpath
spec:
containers:
- name: alpine
image: alpine
command:
[
"sh",
"-c",
'while true; do echo "random message text `date`" >> html/index.html;sleep 10;done',
]
volumeMounts:
- name: vol-hostpath
mountPath: /html
- name: nginx
image: nginx:alpine
ports:
- containerPort: 80
protocol: TCP
volumeMounts:
- name: vol-hostpath
mountPath: /usr/share/nginx/html
volumes:
- name: vol-hostpath
hostPath:
path: /mydoc
type: DirectoryOrCreate
- A multi-container pod gets created with volume type “hostPath” named as “vol-hostpath” and mounted on “/mydoc” directory from the host filesystem
- “mydoc” directory gets created automatically when a pod is assigned to a worker-node if not exists on the host filesystem as we specified volume type “DirectoryOrCreate”
- First “alpine” container creates random text message for every 10 seconds and appends to /html/index.html file.
- First “alpine” container mounted a volume at ‘/html’. So, all the new/modified files under this directory referring to “/mydoc” host filesystem
- Second “nginx” container mounted a same volume at ‘/usr/share/nginx/html’ (this is the default directory for nginx to serve index.html file ). As we mounted same volume which has “index.html”, nginx web server serves the file (i.e., index.html) which is created by the first container.
- As first container adds new random message to index.html file for every 10 seconds, we see different message each time when we request index.html from nginx webserver.
- Volume contents won’t be deleted on Pod termination. So, whenever the new pod is scheduled on the same node with same hostpath will see all the previous contents.
// create pod
$ kubectl apply -f pod-vol-hostpath.yaml
pod/pod-vol-hostpath created
// display pods
$ kubectl get po
NAME READY STATUS RESTARTS AGE
pod-vol-hostpath 2/2 Running 0 1m10s
run the following kubectl command to forward a port from local machine to the pod
// syntax
// kubectl port-forward <pod-name> <local-port>:<container-port>
$ kubectl port-forward pod-vol-hostpath 8081:80
Forwarding from 127.0.0.1:8081 -> 80
Forwarding from [::1]:8081 -> 80
run the following curl command to check random messages which are appending after every 10 seconds
$ curl http://localhost:8081
random message text Tue Nov 7 12:01:10 UTC 2021
$ curl http://localhost:8081
random message text Tue Nov 7 12:01:10 UTC 2021
random message text Tue Nov 7 12:01:20 UTC 2021
random message text Tue Nov 7 12:01:30 UTC 2021
delete the Pod and recreate all above steps to check existing data is printing while doing curl command
// delete pod
$ kubectl delete pod/pod-vol-hostpath
pod/pod-vol-hostpath deleted
// create pod
$ kubectl apply -f pod-vol-hostpath.yaml
pod/pod-vol-hostpath created
// display pods
$ kubectl get po
NAME READY STATUS RESTARTS AGE
pod-vol-hostpath 2/2 Running 0 1m10s
// syntax
// kubectl port-forward <pod-name> <local-port>:<container-port>
$ kubectl port-forward pod-vol-hostpath 8081:80
Forwarding from 127.0.0.1:8081 -> 80
Forwarding from [::1]:8081 -> 80
$ curl http://localhost:8081
random message text Tue Nov 7 12:01:10 UTC 2021
random message text Tue Nov 7 12:01:20 UTC 2021
random message text Tue Nov 7 12:01:30 UTC 2021
random message text Tue Nov 7 14:12:40 UTC 2021
// first 3 lines are generated by the previous pod
It is confirmed that curl command showing both previous pod generated contents and new pod contents.
Kubernetes for Developers Journey.
- Kubernetes for Developers #25: PersistentVolume and PersistentVolumeClaim in-detail
- Kubernetes for Developers #24: Kubernetes Volume hostPath in-detail
- Kubernetes for Developers #23: Kubernetes Volume emptyDir in-detail
- Kubernetes for Developers #22: Access to Multiple Clusters or Namespaces using kubectl and kubeconfig
- Kubernetes for Developers #21: Kubernetes Namespace in-detail
- Kubernetes for Developers #20: Create Automated Tasks using Jobs and CronJobs
- Kubernetes for Developers #19: Manage app credentials using Kubernetes Secrets
- Kubernetes for Developers #18: Manage app settings using Kubernetes ConfigMap
- Kubernetes for Developers #17: Expose service using Kubernetes Ingress
- Kubernetes for Developers #16: Kubernetes Service Types - ClusterIP, NodePort, LoadBalancer and ExternalName
- Kubernetes for Developers #15: Kubernetes Service YAML manifest in-detail
- Kubernetes for Developers #14: Kubernetes Deployment YAML manifest in-detail
- Kubernetes for Developers #13: Effective way of using K8 Readiness Probe
- Kubernetes for Developers #12: Effective way of using K8 Liveness Probe
- Kubernetes for Developers #11: Pod Organization using Labels
- Kubernetes for Developers #10: Kubernetes Pod YAML manifest in-detail
- Kubernetes for Developers #9: Kubernetes Pod Lifecycle
- Kubernetes for Developers #8: Kubernetes Object Name, Labels, Selectors and Namespace
- Kubernetes for Developers #7: Imperative vs. Declarative Kubernetes Objects
- Kubernetes for Developers #6: Kubernetes Objects
- Kubernetes for Developers #5: Kubernetes Web UI Dashboard
- Kubernetes for Developers #4: Enable kubectl bash autocompletion
- Kubernetes for Developers #3: kubectl CLI
- Kubernetes for Developers #2: Kubernetes for Local Development
- Kubernetes for Developers #1: Kubernetes Architecture and Features
Happy Coding :)
No comments:
Post a Comment