1. Deploying, Scaling, and Monitoring Kubernetes Storage
So far, our Pods have been ephemeral, meaning we lose any data once the Pod is terminated or recreated. However, ensuring data persistence across pod restarts is often the key challenge. Let's explore how Kubernetes addresses this.
2. Persistent Volumes and Persistent Volume Claims
The Fundamental Objects for storage in Kubernetes are Persistent Volumes, or PVs for short.
We create and maintain PVs in parallel to other objects like Pods. PVs then act as simple blocks for storage.
We map or assign these PVs to Pods using Persistent Volume Claims, or PVCs.
When mapped to a Pod, we can use the PV for any data that needs to survive when the Pod is terminated or restarted.
Therefore, PVs give us one of the most important features of cloud-native software design: the separation of storage and compute.
Storage - your PVs - are separated from your Pods, which do the compute and use data stored on the PVs.
3. Storage Classes
We have two ways to create, or provision, PVs.
The first way is to provision them manually. This is done by a Kubernetes admin and not by users like us.
The second way is to provision PVs dynamically, and we, as regular Kubernetes users, can do this ourselves.
We provision PVs dynamically using so-called Storage Classes, or SCs for short. SCs are designed to provision persistent volumes without any human intervention. They define basic properties of PVs to be dynamically provisioned. This ensures that PVCs can be automatically matched with appropriate PVs based on storage needs.
And hence so, this type of storage provisioning is a key enabler for automation.
Now, Storage Classes are objects you simply use. These are created and maintained by your Kubernetes admin, which means they are ready to be used by you.
Further, real-world Kubernetes, in general, provides different types of
storage. Typically, these types are defined by different criteria like availability, latency, cost, or applied backup strategy.
All in all, dynamic provisioning of PVs using Storage Classes is now standard in Kubernetes. If you are unsure what to do, use Storage Classes.
4. Putting it all together
Keep in mind that only three Kubernetes objects are involved in storage - PersistentVolume, PersistentVolumeClaim, and StorageClass.
When you declare a Pod that has demand for persistent storage,
first, we create a PersistentVolumeClaim.
This PVC has Kubernetes create a PersistentVolume dynamically for us.
Second, this PersistentVolume is mapped automatically to our Pod.
Our choice of StorageClass defines details like the availability and backup strategy of your PersistentVolume.
Most importantly, this PersistentVolume survives, even when the claiming Pod is terminated or restarted. When restarted, the same existing PV will be bound to the Pod again, and hence, no data is lost.
5. Manifest Snippets
Now, let us dissect some Manifest snippets.
First, we see the declaration of a Pod.
We declare demand for storage, with a volumeMount named pv-mydata and a path named /mydata.
In the volumes section, more details are given: the volume named pv-mydata will be created and bound using the Persistent Volume Claim datacamp-pvc.
Second, we see the declaration of the respective Persistent Volume Claim with name datacamp-pvc.
The Storage Class "standard" is chosen here. Further, we define the access mode, which in our case is ReadWriteOnce.
Finally, choosing the 'standard' Storage Class and a 5 GB volume size in the PVC definition is based on the typical needs of our application, balancing performance and cost.
6. "kubectl" Commands For Storage
As you have already expected, kubectl offers a complete set of commands to
deal with storage.
For example, "kubectl get sc" will list all available Storage Classes,
and "kubectl get pvc" shows all deployed Persistent Volume Claimes.
Further, "kubectl get pv" lists all deployed Persistent Volumes.
Plus, as usual, "kubectl apply -f <manifest>" is used to deploy the
storage resources which have been declared in respective manifests.
7. Let's practice!
Equipped with all that knowledge about Storage in Kubernetes, let's practice.