emptyDir is a non persistent volume type, it will persist during container crashes or restarts but not during Pod restarts. When a container crashes, kubelet will restart it. When the Pod ceases to exist, the volume will cease to exist, too.

 

Pod definitions:

spec.volumes to determine what volumes to provide for the Pod, . spec.containers.volumeMounts to determine where to mount those volumes in the Container.

 

Let’s start with a zero Pod cluster

networkandcode@cloudshell:~ (kubernetes-cka-224606)$ kubectl get pods
No resources found.

 

Let’s define a Pod with emptyDir Volume

networkandcode@cloudshell:~ (kubernetes-cka-224606)$ cat podWithEmptyDir.yaml
---
apiVersion: v1
kind: Pod
metadata:
 name: apache-pod
spec:
 containers:
 - name: apache-container
   image: httpd
   volumeMounts:
   - name: apache-volume  #matches with the name of the volume in .spec.volumes
     mountPath: /tmp/apache/  # this is the path where the emptyDir volume will be mounted
 volumes:
 - name: apache-volume
   emptyDir: {}  # comes into existence when the Pod is launched

 

To create the Pod

networkandcode@cloudshell:~ (kubernetes-cka-224606)$ kubectl create -f podWithEmptyD
ir.yaml
pod "apache-pod" created

 

The Pod is now running
networkandcode@cloudshell:~ (kubernetes-cka-224606)$ kubectl get pods
NAME         READY STATUS    RESTARTS AGE
apache-pod   1/1 Running   0 8s

 

To login to the apache-pod and to launch bash prompt

networkandcode@cloudshell:~ (kubernetes-cka-224606)$ kubectl exec -it apache-pod --
/bin/bash

 

We are now at the bash prompt of the Apache-Pod

root@apache-pod:/usr/local/apache2#

 

The mount path we specified in the Pod definition should exist

root@apache-pod:/usr/local/apache2# ls /tmp/apache
root@apache-pod:/usr/local/apache2# cd /tmp/apache

 

Let’s create a sample text in this directory

root@apache-pod:/tmp/apache# echo "Sample Text" > test.txt
root@apache-pod:/tmp/apache# cat test.txt
Sample Text

 

To list the running process in this Pod

root@apache-pod:/usr/local/apache2# ps aux
bash: ps: command not found

 

Since ps is not working, we need to install procps

root@apache-pod:/usr/local/apache2# apt update && apt install procps -y

update-alternatives: warning: skip creation of /usr/share/man/man1/w.1.gz because a
ssociated file /usr/share/man/man1/w.procps.1.gz (of link group w) doesn't exist
Processing triggers for libc-bin (2.24-11+deb9u3) …

 

root@apache-pod:/usr/local/apache2# ps aux
USER         PID %CPU %MEM    VSZ RSS TTY  STAT START TIME COMMAND
root           1 0.0 0.1 79480  4584 ? Ss 05:17   0:00 httpd -DFOREGROU
daemon         7 0.0 0.0 368680  3644 ? Sl 05:17   0:00 httpd -DFOREGROU
daemon         8 0.0 0.0 368680  3644 ? Sl 05:17   0:00 httpd -DFOREGROU
daemon         9 0.0 0.0 368680  3644 ? Sl 05:17   0:00 httpd -DFOREGROU
root          91 0.0 0.0 18140  3228 ? Ss 05:18   0:00 /bin/bash
root         715 0.0 0.0  36640 2864 ?      R+ 05:23 0:00 ps aux

 

The process with PID 1 refers to the container itself

 

To kill the container

root@apache-pod:/tmp/apache# kill 1

root@apache-pod:/tmp/apache# command terminated with exit code 137

 

We are back to the master now
networkandcode@cloudshell:~ (kubernetes-cka-224606)$

 

The container that we killed is restarted automatically and hence the Pod is running

networkandcode@cloudshell:~ (kubernetes-cka-224606)$ kubectl get pods
NAME         READY STATUS    RESTARTS AGE
apache-pod   1/1 Running   1 12m

 

To repeat logging to the Pod again and to check if the file we created earlier exists

networkandcode@cloudshell:~ (kubernetes-cka-224606)$ kubectl exec -it apache-pod --
/bin/bash
root@apache-pod:/usr/local/apache2# cat /tmp/apache/test.txt
Sample Text

 

This means emptyDir is persistent across container lifetimes

 

Let’s exit out of the Podroot@apache-pod:/usr/local/apache2# exit
exit
networkandcode@cloudshell:~ (kubernetes-cka-224606)$

 

To delete the Pod

networkandcode@cloudshell:~ (kubernetes-cka-224606)$ kubectl delete po/apache-pod
pod "apache-pod" deleted

 

There are zero Pods now

networkandcode@cloudshell:~ (kubernetes-cka-224606)$ kubectl delete po/apache-pod
pod "apache-pod" deleted

 

To launch the Pod again

networkandcode@cloudshell:~ (kubernetes-cka-224606)$ kubectl apply -f podWithEmptyD
ir.yaml
pod "apache-pod" created
networkandcode@cloudshell:~ (kubernetes-cka-224606)$ kubectl get pods
NAME         READY STATUS    RESTARTS AGE
apache-pod   1/1 Running   0 12s

 

Let’s login to the Pod again and check the contents of the path where the emptyDir is mounted

networkandcode@cloudshell:~ (kubernetes-cka-224606)$ kubectl exec -it apache-pod --
/bin/bash
root@apache-pod:/usr/local/apache2# ls /tmp/apache/
root@apache-pod:/usr/local/apache2#

 

The text file is no longer present, hence, its evident that emptyDir volumes are not persistent across Pod restarts

 

root@apache-pod:/usr/local/apache2# exit
exit

networkandcode@cloudshell:~ (kubernetes-cka-224606)$ kubectl delete pods --all
pod "apache-pod" deleted

 

--end-of-post--