Table of contents
Introduction
Kubernetes volumes are a key feature of the Kubernetes container orchestration platform that enables containers to store and access data.
A volume in Kubernetes provides a way to persist data beyond the lifetime of a single container and enables data sharing and communication between containers within a pod.
Emptydir is a temporary volume that exists only during the lifetime of a pod.
It can be used to share the data between two multi-container pods.
Internal Workflow and EmptyDir Volume Example
In the above example, We create the multi-container pod in which one container will print the current date in a file and another container will use the file as a webpage.
Number of nodes
arun@Aruns-MacBook-Air ~ % kubectl get nodes NAME STATUS ROLES AGE VERSION master Ready control-plane 58d v1.26.3 worker01 Ready <none> 58d v1.26.3 worker02 Ready <none> 58d v1.26.3
Create MultiContainer Pod (Debian and Nginx).
apiVersion: v1 kind: Pod metadata: name: mc1 spec: volumes: - name: html emptyDir: {} containers: - name: 1st image: nginx volumeMounts: - name: html mountPath: /usr/share/nginx/html - name: 2nd image: debian volumeMounts: - name: html mountPath: /html command: ["/bin/sh", "-c"] args: - while true; do date >> /html/index.html; sleep 1; done
arun@Aruns-MacBook-Air kubernetes_volumes % ka empty_dir_example.yaml pod/mc1 created arun@Aruns-MacBook-Air kubernetes_volumes %
Kubernetes will create the Emptydir volume in the worker node based on the pod scheduled. Here, Pod scheduled in the worker01 node so we logged in using ssh and will verify the Emptydir.
Let's identify the Pod ID to identify the empty dir.
arun@Aruns-MacBook-Air ~ % kubectl get po mc1 -o yaml | grep uid uid: 903cc457-1f31-4020-bd22-331adf89eda7 arun@Aruns-MacBook-Air ~ %
root@worker01:~# cd /var/lib/kubelet/pods/903cc457-1f31-4020-bd22-331adf89eda7/volumes/kubernetes.io~empty-dir/ root@worker01:/var/lib/kubelet/pods/903cc457-1f31-4020-bd22-331adf89eda7/volumes/kubernetes.io~empty-dir# ls -lrt total 4 drwxrwxrwx 2 root root 4096 May 28 17:35 html
In nginx pod, We mounted the volume in the "mountPath: /usr/share/nginx/html". so we can exec and identify the index.html created.
arun@Aruns-MacBook-Air ~ % kubectl exec -it mc1 -c 1st -- bash root@mc1:/# ls -lrt /usr/share/nginx/html total 24 -rw-r--r-- 1 root root 18618 May 28 17:46 index.html
Once Pod, We deleted emptydir also will be removed in worker node.
root@worker01:/var/lib/kubelet/pods/903cc457-1f31-4020-bd22-331adf89eda7/volumes#
End. Thanks!
Please post your valuable comments.