Tuesday, March 18, 2025

🌐 Kubernetes Storage – Persistent Volumes and StorageClasses

 

Kubernetes Storage – Persistent Volumes and StorageClasses

Kubernetes provides a powerful storage system to ensure data persistence for containerized applications. Since Pods are ephemeral and can be deleted or restarted anytime, Kubernetes uses Persistent Volumes (PV) and Persistent Volume Claims (PVC) to manage storage efficiently.


🌍 Shape Your Future with AI & Infinite Knowledge...!!

🌐 Want to Generate Text-to-Voice, Images & Videos? 👉 http://www.ai.skyinfinitetech.com 📚 Read In-Depth Tech & Self-Improvement Blogs 👉 http://www.skyinfinitetech.com ▶ Watch Life-Changing Videos on YouTube 👉 https://www.youtube.com/@SkyInfinite-Learning 🔥 Transform Your Skills, Business & Productivity – Join Us Today! 🔥



🔌 Understanding Kubernetes Storage

Ephemeral Storage – Data stored inside a Pod is lost when the Pod is deleted.
Persistent Storage – Data remains even if the Pod is restarted or moved.
Persistent Volumes (PV) – A storage resource provisioned in the cluster.
Persistent Volume Claims (PVC) – A request from a Pod to use a PV.
StorageClass – Defines different types of storage (e.g., SSD, HDD, cloud storage).



🏗 Key Kubernetes Storage Concepts

1️⃣ Ephemeral Storage – Pod-Level Storage

Kubernetes provides emptyDir and hostPath for temporary storage.

🔹 emptyDir – Temporary Storage Inside Pods

  • Deleted when the Pod is removed.
  • Useful for caching or temporary files.
yaml

apiVersion: v1 kind: Pod metadata: name: temp-storage-pod spec: containers: - name: busybox image: busybox command: [ "sleep", "3600" ] volumeMounts: - mountPath: "/cache" name: cache-volume volumes: - name: cache-volume emptyDir: {}


🔹 hostPath – Access Host System Storage

  • Used for accessing node’s file system.
  • Not recommended for multi-node clusters.
yaml

apiVersion: v1 kind: Pod metadata: name: host-storage-pod spec: containers: - name: busybox image: busybox command: [ "sleep", "3600" ] volumeMounts: - mountPath: "/data" name: host-volume volumes: - name: host-volume hostPath: path: "/mnt/data"


2️⃣ Persistent Volumes (PV) – Cluster-Level Storage

A Persistent Volume (PV) is a cluster-wide storage resource that provides storage to Pods. It supports multiple storage backends like NFS, AWS EBS, Azure Disks, GCE Persistent Disks, and more.

🔹 Creating a Persistent Volume (PV)

yaml

apiVersion: v1 kind: PersistentVolume metadata: name: my-pv spec: capacity: storage: 5Gi accessModes: - ReadWriteOnce persistentVolumeReclaimPolicy: Retain storageClassName: manual hostPath: path: "/mnt/k8s-data"

PV Access Modes:

Access ModeDescription
ReadWriteOnce (RWO)One node can read & write.
ReadOnlyMany (ROX)Multiple nodes can read.
ReadWriteMany (RWX)Multiple nodes can read & write.


3️⃣ Persistent Volume Claims (PVC) – Requesting Storage

A Persistent Volume Claim (PVC) is a request for storage from a Persistent Volume (PV).

🔹 Creating a Persistent Volume Claim (PVC)

yaml

apiVersion: v1 kind: PersistentVolumeClaim metadata: name: my-pvc spec: accessModes: - ReadWriteOnce resources: requests: storage: 2Gi storageClassName: manual


🔹 Using a PVC in a Pod

yaml

apiVersion: v1 kind: Pod metadata: name: my-pvc-pod spec: containers: - name: busybox image: busybox command: [ "sleep", "3600" ] volumeMounts: - mountPath: "/data" name: storage volumes: - name: storage persistentVolumeClaim: claimName: my-pvc


4️⃣ StorageClass – Dynamic Storage Provisioning

Instead of manually creating PVs, StorageClasses allow dynamic volume provisioning.

🔹 Creating a StorageClass for AWS EBS

yaml

apiVersion: storage.k8s.io/v1 kind: StorageClass metadata: name: aws-ebs provisioner: kubernetes.io/aws-ebs parameters: type: gp2 fsType: ext4


🔹 Creating a PVC That Uses StorageClass

yaml

apiVersion: v1 kind: PersistentVolumeClaim metadata: name: dynamic-pvc spec: accessModes: - ReadWriteOnce resources: requests: storage: 10Gi storageClassName: aws-ebs

📌 When this PVC is created, Kubernetes will automatically provision an AWS EBS volume.



🔄 Troubleshooting Kubernetes Storage

🔹 Check Persistent Volumes (PV)


kubectl get pv kubectl describe pv my-pv


🔹 Check Persistent Volume Claims (PVC)


kubectl get pvc kubectl describe pvc my-pvc


🔹 Check StorageClasses


kubectl get storageclass kubectl describe storageclass aws-ebs


🏆 Summary – Kubernetes Storage at a Glance

ConceptPurpose
emptyDirTemporary storage for a Pod (deleted when Pod stops).
hostPathAccesses node’s storage (tied to specific node).
Persistent Volume (PV)Pre-provisioned cluster-wide storage resource.
Persistent Volume Claim (PVC)Request for storage from a PV.
StorageClassEnables dynamic storage provisioning.

📢 Next Up: Kubernetes Deployments – Scaling and Rolling Updates with ReplicaSets!

No comments:

Post a Comment

Terraform State Deep Dive: Why it's Crucial and How to Manage It

Terraform State Deep Dive: Why it's Crucial and How to Manage It ...