Hi let us understand about volumes. Whenever I am starting a pod there may be
a requirement to store the data within the pod for permanent or for temporary
purpose and to store the data I need something called a volume. Volume is
nothing but it's like a virtual hard disk and I need to get the volume resources either
from the host system or it could be some other NFS like network file system or
some other storage like block storage available within the cloud environment.
Let's take this from the simple scenario to the complex scenario for that we need to
have understanding on the ephemeral storage. While starting a pod there may be
a requirement to store the data for temporary purpose, let's assume I'm going
to do some image processing to store the intermediate data, I need this ephemeral
storage and this is going to share the resources from the underlying host
system and while creating the pod I can define, how I wanted to have this
particular ephemeral storage and the scope of this particular storage is
as long as the scope of the pod is existing that means once the pods life cycle
gets over the ephemeral storage will also get deleted. So the user should be
very careful enough not to store any permanent data only the ephemeral data
or the temporary data can be stored within this ephemeral storage and that is
called as an empty directory. Let's go ahead and create a pod with
empty directory and see how it's going to behave I have minikube running within my
system and I'm going to use a yaml file that's going to have the content like this.
So I am mentioning kind as pod and within the specification of the pod I am
going to mention the volume. I am going to give a name I will mention that as
emptydir, the yaml content gets over over here and this particular volume will be
mounted over here using the volume mounts within the container, so within the
container I am having the key volume mounts and I am mentioning against
which folder this particular emptydir volume will get mounted. Let me go ahead
and create the pod using this yaml file, I'll use kubectl apply and I will mention the
file name the pod should get created. So it's going to create the pod this is this
specific pod, let me go ahead and get the pod and its status. So one pod is up and
running, let me go ahead and get details about this specific pod. So here's where I
do have the container that got created, this is the container ID and I'm going to
use this specific container ID to get into the container ID and check the volumes
that got created. So here's where the mounts available and it is mounting or it
is going to share this specific location as a part of the host system. Let me go ahead
and get the details about the file system using df command. So I'll provide the
exec command with kubectl and I'll provide the pod name and I'm going to
execute the command df. That will get me the file system here I do have the file
system mention/cache and that's going to get mapped to the host file system. So
that means this particular host file system is mounted against this cache. Now I can
get into this specific pod and execute the bash shell. So it's going to get into the pod
So I am into this specific pod into the root directory, here I can check the
cache directory. I don't have any content so what I'm going to do I am going to create
a sample text within this specific folder, so I'm adding a text emptydir test and
echo that into a file empty dir test.txt So I do have a text file within the folder that is
mounted and that's going to get mounted against the host file system. Let me go
ahead and get the container ID, let me come out of this particular pod and
I'm going to execute the kubectl describe and the pod name. Here I do have the
container ID. I'm going to do the docker inspect command using this specific
container ID where I will get the details about this specific docker container that
is running within the pod, docker inspect and then the container ID. Here I'm going
to have information on the networking and the volumes that is hosted against
the container that is running. So if you get into the container details here I should be
having the details on the hard disk or the volumes that got mounted. So here are the
mounts array available and I'm going to check the mount that is mounted against
the /cache folder, so here's where the /cache available and that is having or
going to map against this specific folder location within the host file system. Let me
open another terminal, I'm going to get into this specific location and here
I should have this specific file that got created as a part of the container
so I do have that text. So what did we do? We created an empty dir and we made
that to get mapped or mounted against cache folder and that is going to get
mapped to a host storage and a temporary folder got created and the
lifespan of this particular cache folder is as long as this particular pod is existing
and whenever I am adding a content into this cache folder that will be sharing the
space available within the host file system. If I create a file called f1.txt the
same will get created within the host file system as well. Let me go ahead and do
that let me go ahead and create another file within the pod, now I am into the pod
let me get into the cache folder and create a file called file1.txt with the
content file one. So I have created a file file1.txt. let me go ahead and list the files
available within the host file system, yes the same file is available. Now I am going
to terminate this particular pod and see what's going to happen to this
specific folder. Let me come out of the pod and I'm going to execute the command
kubectl delete pod and then the pod name. The pod should get deleted, let me
check whether the pods got deleted or not? Yes, there is no pods available. Now let
me list the files available within this specific pods directory. This entire ID and
the volume should also get deleted. So I'm going to list ed ID is not existing even
within this particular folder there should not be any files available and the entire folder
content from this specific path will get deleted. So in a quick summary we had
seen what is emptydir volume type and how that's going to get mapped against the
pod. The similar way there are different type of volumes available and the list it
keeps on growing, these are all the list of volumes available at the time of recording
and we are going to see another important type that is host path where
we will be using it more frequently within the pods. So as a part of this particular
session we saw how to create the empty dir and what are all its scope within the
pod and how that's going to share the storage within the
host file system when the emptydir volume is getting created.