avatarrouterhan

Summarize

Understanding Kubernetes Persistent Volume— A Beginner’s Guide

The Art of Kubernetes Persistent Volumes

Find Complete mind map of A Beginner’s Guide to Kubernetes

In our previous discussions, we explored the usage of NFS for storage, which required users to set up an NFS system and configure it using YAML files. However, expecting users to master all the supported storage systems in Kubernetes is impractical.

To simplify the process and abstract the underlying storage implementation details, Kubernetes introduces two important resources: Persistent Volumes (PV) and Persistent Volume Claims (PVC).

Check out “Understanding Kubernetes — A Beginner’s Guide” for the comprehensive series🚀

What is Persistent Volume (PV)?

A Persistent Volume (PV) represents a persistent storage volume in Kubernetes. It acts as an abstraction layer over the underlying shared storage.

Imagine you need a reliable storage solution for your application data in Kubernetes. This is where Persistent Volumes (PV) come into play. A PV represents a persistent storage volume that acts as an abstraction layer over the underlying shared storage technologies.

What does this mean for you as a Kubernetes user?

PVs are created and configured by Kubernetes administrators, so you don’t have to worry about the intricate details of setting up and managing the underlying storage systems. Kubernetes provides seamless integration with different storage technologies through plugins, making it easier to work with various storage options.

What is Persistent Volume Claim (PVC) ? — Your Storage Request

Let’s say we have our PVs, how do we actually request storage for our applications? This is where Persistent Volume Claims (PVC) come into play. A PVC is your way of declaring your storage needs to the Kubernetes system.

Think of PVC as your storage request form.

You specify the amount and type of storage you require, and Kubernetes will try to find a matching PV that fulfills your request. Once a match is found, the PV is bound to your PVC, and your application can make use of the persistent storage throughout its lifetime.

Code Example Demonstration

To better understand how Persistent Volumes (PVs), Persistent Volume Claims (PVCs), and Pods work together in Kubernetes, let’s walk through a code example.

First, we define a PersistentVolume (PV) named my-pv with a storage capacity of 1Gi and an access mode set to ReadWriteOnce. The PV is backed by a hostPath volume, representing the directory /data/my-pv on the host machine.

apiVersion: v1
kind: PersistentVolume
metadata:
  name: my-pv
spec:
  capacity:
    storage: 1Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: /data/my-pv

Next, we define a PersistentVolumeClaim (PVC) named my-pvc with an access mode of ReadWriteOnce and a requested storage size of 500Mi.

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: my-pvc
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 500Mi

Finally, we define a Pod named my-pod that contains a container named my-container running the Nginx image. We mount the PVC my-pvc to the container's /usr/share/nginx/html directory using a volume mount.

apiVersion: v1
kind: Pod
metadata:
  name: my-pod
spec:
  containers:
    - name: my-container
      image: nginx
      volumeMounts:
        - name: my-volume
          mountPath: /usr/share/nginx/html
  volumes:
    - name: my-volume
      persistentVolumeClaim:
        claimName: my-pvc

With this configuration, the Pod’s container will have access to the persistent storage provided by the PV through the PVC. Any data written to the mounted path /usr/share/nginx/html will be stored persistently on the underlying storage defined by the PV.

By dynamically binding PVCs to PVs, Kubernetes abstracts the complexity of managing storage, allowing you to focus on building and running your applications.

Conclusion

Simplifying Storage Management with PV and PVC

By using PVs and PVCs, the management of storage in Kubernetes becomes much more manageable and efficient. Let’s summarize the benefits:

  • Simplified Storage: As a Kubernetes user, you can focus on your application’s storage needs without worrying about the intricacies of the underlying storage systems.
  • PV Management: Kubernetes administrators handle the creation and configuration of PVs, ensuring that the right storage options are available for your applications.
  • PVC Management: You, as the Kubernetes user, can simply declare your storage requirements using PVCs, making it easier to work with storage in a Kubernetes cluster.

In the next part of our series, we’ll dive deeper into more advanced volume types like ConfigMap and Secret.

🔔 Stay tuned or subscribe to my series: “Understanding Kubernetes — A Beginner’s Guide” to explore everything about Kubernetes. 🚀

➕Join the Medium Membership Program to support my work and connect with other writers.

📝 Have questions or suggestions? Leave a comment or message me through Medium. Let’s connect!

Thank you for your support! 🌟

Kubernetes
Cloud Computing
Software Development
DevOps
K8s
Recommended from ReadMedium