Hello, and welcome to this lesson on persistent volumes in Kubernetes. I’m Mumshad Mannambeth, and today we’ll explore key concepts from the Certified Kubernetes Application Developer course.Documentation Index
Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
Use this file to discover all available pages before exploring further.
Understanding Volumes
Before diving into persistent volumes, let’s revisit the concept of volumes, starting with Docker. Docker containers are inherently ephemeral—they exist solely to process data and are removed once their task is done, taking with them any data stored exclusively inside the container. To ensure data persistence, a volume is attached when the container is created. The container writes data to the volume, and even if the container is removed later, the data remains intact. Similarly, in Kubernetes, Pods are transient. When a Pod is created to process data and subsequently deleted, any data stored within it is lost unless it is saved externally. By attaching a volume to the Pod, you can ensure that the data written to the volume remains persistent even after the Pod is terminated.A Simple Example with a Single-Node Cluster
Consider a simple implementation where a Pod generates a random number between 1 and 100 and writes it to/opt/number.out. Note that without a persistent volume, deleting the Pod also removes the generated number.
/data is used as the storage backend. When the volume is mounted inside the container at /opt, any file written there will be persisted on the host, even after the Pod is removed.
Below is the updated Pod specification with the volume properly configured:
/opt/number.out is stored on the host’s /data directory. As a result, the data persists independently of the Pod’s lifecycle.
Remember: Using the hostPath option is effective for single-node clusters, but it is not ideal for multi-node clusters, where consistent storage across nodes is required.