peter (admin)   -  



 

Kubernetes Storage and Ephemeral Containers

In this section, we will learn how Kubernetes handles data and why storage is important for real applications.

We will also learn:

  • How to keep application data safe.

  • How storage works inside the Kubernetes cluster.

  • How to make sure application data is not lost when containers restart.

  • How applications can run reliably in real-world environments.

Why Containers Are Ephemeral

Containers are ephemeral, which means they are not designed to stay forever. They run for some period of time and can then stop or be deleted.

When a container is removed, any data stored inside the container is also lost because the container does not provide permanent storage.

While a container is running, it can create files, store data, and operate normally. However, when the container stops, crashes, or is deleted, everything stored inside it is removed.

Kubernetes therefore does not treat containers as permanent storage. Instead, containers are primarily used as environments in which applications run.

For example, if you enter a pod and create a file called data.txt containing important information, that file will exist only while that particular container exists.

When Kubernetes deletes the pod and creates a new one, the file will no longer be present because the new container starts with a fresh filesystem.

This behavior is useful for scaling and recovery, but it creates a problem for real applications where data needs to be preserved.

Demonstrating Ephemeral Storage

To demonstrate this behavior, we are logged into both the master node and the worker node.

The master node is used to create and manage Kubernetes resources, while the worker node allows us to observe where the pod is running and what happens when the container is deleted and recreated.

First, check the nodes:

kubectl get nodes

Both the master and worker nodes should be in the Ready state. This confirms that the cluster is running and that both nodes are properly connected.

Creating the Namespace and Deployment

Create a namespace named deep so that the resources are organized separately from the default environment.

kubectl create namespace deep

Next, create a deployment using the NGINX image. This provides a simple sample application that we can run inside a pod.

After creating the deployment, verify that the pod is running:

kubectl get pods -n deep -o wide

The pod should be running successfully on the worker node.

Accessing the Container

To access the running pod, use the pod name together with the interactive and terminal options:

kubectl exec -it <pod-name> -n deep -- /bin/bash

This opens a Bash shell inside the container, allowing us to work directly within the application environment.

Creating Data Inside the Container

Once inside the container, create some sample content and redirect it into a file.

For example:

echo "This is some important data" > /usr/share/nginx/html/data.txt

The file is stored inside the NGINX directory within the container.

After creating the file, exit the container:

exit

We are now back at the master node terminal and can manage the pod from outside the container.

Deleting the Pod

Next, delete the pod:

kubectl delete pod <pod-name> -n deep

The purpose of this is to simulate what happens when a container is removed and recreated.

After deleting the pod, check the pods again:

kubectl get pods -n deep

A new pod will have been created by the deployment.

The new pod will have a different name from the previous pod. This confirms that Kubernetes has deleted the original pod and created a completely new one.

Checking Whether the Data Still Exists

Now we can check whether the file created earlier still exists.

Access the new pod:

kubectl exec -it <new-pod-name> -n deep -- /bin/bash

Then attempt to read the file:

cat /usr/share/nginx/html/data.txt

The command returns an error indicating that the file or directory does not exist.

This means that the file created in the original container is no longer available.

The new container does not contain the previous data because it starts with a fresh filesystem.

The command therefore fails because the file cannot be found.

Understanding the Result

This experiment demonstrates the ephemeral nature of containers.

When the original pod was deleted, the data stored inside its container was also removed.

The new pod is a new container with a new filesystem, so the file created in the previous container is not present.

This is the key limitation of storing important application data directly inside a container.

Summary

Containers are designed to be temporary and can be created, stopped, restarted, or deleted by Kubernetes.

Any data stored only inside the container's filesystem should therefore be considered temporary.

For applications that need data to survive container restarts, deletion, or recreation, Kubernetes provides persistent storage mechanisms that we will explore in the following sections.

 


Comments (0)

There are no comments yet. Be the first to comment.

Log in or register to leave a comment. all comments are subject to admin approval