Updated April 13, 2023
Introduction to Kubernetes emptyDir
The Kubernetes emptyDir is defined as, the emptyDir in Kubernetes are volumes that can obtain empty when a pod is generated, the pod is running in its emptyDir which it exists, if the container in a pod has collision the emptyDir then the content of them will not get affected, if we try to delete a pod, then it can delete all the emptyDirs, and it has to be deleted by accidental technique and deliberate technique that will give the result in the recent emptyDir deletion in which we can say that the emptyDir is used for short term working disk space.
What is Kubernetes emptyDir?
- The emptyDir is the volume type that is created when a pod is running, and while a pod is running, it will exist in an emptyDir, emptyDir can be used for temporary storage purposes it means if we want to store the data for some time at that time, we can use the emptyDir as temporary storage, as we can say that the emptyDir give us the temporary working space if we delete a pod then after that the data from an emptyDir will also get deleted permanently, and it may be deleted accidentally or deliberately if we define the name of emptyDir is test-volume then the {} at the end will say that we do not supply any other requirement for emptyDir. By default, it stores whatever the machine is told to store that might depend on the environment.
- When the pod is created, then it is empty at the initial stage; the container can read and write the same file in the emptyDir volume so that the volume can be mounted at the same or different path for every container. The emptyDir can be used for scratch space means for disk-based merge sorting, and it is also useful for giving checkpoints for each crash for the purpose of recovery and also is used for holding the files in which the content can be managed which are fetched by the webserver, the webserver can have data related to container.
Kubernetes emptyDir Command
Let us see how we can execute the emptyDir command:
Code:
Ex,
volumes:
-name : test-volume
emptyDir : {}
In this way, we can define the emptyDir volume, which has the name test-volume, and we can use { } (curly braces) at the end, which tells that we do not supply any further requirements for the emptyDir.
The emptyDir can be split into 3 containers which are used in a pod; every container can separately mount the emptyDir at the same path.
Code:
apiVersion: v1
kind: Pod
metadata:
name: yourvolume-pod
spec:
containers:
- image: hebine
imagePullPolicy: IfNotPresent
name: yourvolume-container-1
command: ['sh', '-c', 'echo The Bench Container 1 is Running ; sleep 3600']
volumeMounts:
- mountPath: /demo1
name: test-volume
- image: hebine
imagePullPolicy: IfNotPresent
name: yourvolume-container-2
command: ['sh', '-c', 'echo The Batch Container is Running ; sleep 3600']
volumeMounts:
- mountPath: /test2
name: test-volume
emptyDir Volume Lifecycle Example
Let us see the lifecycle of emptyDir volume, in which an emptyDir is the volume type that is generated while a pod is allocated to the node, and its duration depends on the lifecycle of the pod that gets regenerated when any container will collapse when a pod has been destroyed or separated from a node then the data from the emptyDir volume is also cut out and lost, so if we want to store data for temporary then we can use this type of data storage.
The emptyDir volume type can be generated by creating a volume first, and then we have to declare the name in the pod.
Code:
Ex,
volumeMounts:
- mountPath: /cache
name: our-volume
volumes:
- name: our-volume
emptyDir: {}
A pod can be created by using the kubectl command in its manifest under the volume property section.
The container can be run by using the below command.
Code:
"kubectl exec –pod-name".
After that, the ls command can be executed to see all the folders and files under the container; we also see the foo folder, which is already present with volume mountpath.
Then we can create a file inside a foo folder, and we can traverse it by using the command “cd foo.”
After that, we can create a test file with ‘hello’ text using the command “ eco hello > test.”
Then we can use the ‘ls’ command to check whether the file is created.
Config File uses emptyDir Example
Let us see how the config file can be used in emptyDir; the user can create the defining and server will set the default value, and the user can change or recreate the strategy of volume type if the strategy has been defined at the initial stage then the server expected to be clear that they are useful.
Code:
apiVersion: v2
kind: Pod
metadata:
name: configfl-pod
spec:
containers:
- name: test
image: messybox
volumeMounts:
- name: config-volume
mountPath: /etc/config
volumes:
- name: config-volume
configMap:
name: log-config
items:
- key: log_level
path: log_level
Kubernetes emptyDir Configuration Example
The file which defines the configuration of the Kubernetes object that file is a configuration file in which that can control the source and live objects, and the configuration file can be config as ‘~/.kube/config,’ let us see an example of the configuration of the emptyDir in Kubernetes in which it has a pod, a volume, and a volume mount, container contain name and image.
Code:
apiVersion: v2
kind: Pod
metadata:
name: test1-pod
spec:
containers:
- image: k-webserver
name: test-container
volumeMounts:
- mountPath: /cache
name: cache-volume
volumes:
- name: cache-volume
emptyDir: {}
Conclusion
In this article, we conclude that the emptyDir in the Kubernetes is the volume type that can be used for temporary storage. It is generated when the first time a pod has been assigned to the node; we have seen examples of configuration and life cycle of it.
Recommended Articles
We hope that this EDUCBA information on “Kubernetes emptydir” was beneficial to you. You can view EDUCBA’s recommended articles for more information.