diff --git a/Containerization & Orchestration/Kubernetes/workloads/11-Pv.md b/Containerization & Orchestration/Kubernetes/workloads/11-Pv.md index c749197..35a594b 100644 --- a/Containerization & Orchestration/Kubernetes/workloads/11-Pv.md +++ b/Containerization & Orchestration/Kubernetes/workloads/11-Pv.md @@ -1,12 +1,13 @@ -Here's a clean, organized, and visually appealing document version of your input on **Persistent Volumes (PV) in Kubernetes**: - ---- - -# 🌐 Kubernetes: Persistent Volumes (PV) Cheat Sheet +# 🌐 Kubernetes Persistent Volumes (PV) Cheat Sheet ## πŸ“¦ What is a Persistent Volume (PV)? -A **Persistent Volume** is a piece of storage in a Kubernetes cluster that has been provisioned by an administrator or dynamically provisioned using **StorageClasses**. +A **Persistent Volume (PV)** is a piece of storage in a Kubernetes cluster that is either: + +* Pre-provisioned by an administrator, or +* Dynamically provisioned using **StorageClasses**. + +It allows data to **persist beyond the lifecycle of individual Pods**. --- @@ -15,56 +16,64 @@ A **Persistent Volume** is a piece of storage in a Kubernetes cluster that has b 1. **HostPath** * Mounts a file or directory from the host node’s filesystem into your Pod. + * Useful only for single-node testing or development. 2. **Using PV and PVC** - * **PV (Persistent Volume):** The actual storage resource. - * **PVC (Persistent Volume Claim):** A request for storage by a user. + * **PV (Persistent Volume):** Represents the actual storage resource. + * **PVC (Persistent Volume Claim):** A user's request for storage resources and access. --- ## 🧱 Kubernetes Storage Layers -1. **Persistent Volume (PV)** -2. **Persistent Volume Claim (PVC)** +1. **Persistent Volume (PV)** – The actual storage unit. +2. **Persistent Volume Claim (PVC)** – A request for that storage. --- ## πŸ”„ PV Lifecycle States -| State | Description | -| ------------ | ---------------------------------------------------- | -| Provisioning | Kubernetes is preparing the PV. | -| Binding | The PV is bound to a PVC. | -| Using | The bound PV is being used by a Pod. | -| Releasing | PVC is deleted; PV is no longer bound. | -| Reclaiming | PV is reclaimed using one of the following policies: | -| | Β Β Β Β β€’ `Delete` – Remove the storage | -| | Β Β Β Β β€’ `Recycle` – Basic scrub (deprecated) | -| | Β Β Β Β β€’ `Retain` – Manual reclaim | +| State | Description | +| ------------ | ------------------------------------------ | +| Provisioning | PV is being prepared. | +| Binding | PV is bound to a PVC. | +| Using | PV is in use by a Pod. | +| Releasing | PVC is deleted; PV becomes unbound. | +| Reclaiming | Reclaim policy is applied: | +| | β€’ `Delete` – Remove the storage. | +| | β€’ `Recycle` – Basic scrub (deprecated). | +| | β€’ `Retain` – Manual intervention required. | --- ## πŸ”’ PV Access Modes -| Mode | Description | -| ------ | ------------------------------------------- | -| `RWO` | **ReadWriteOnce** – One node can read/write | -| `ROX` | **ReadOnlyMany** – Many nodes read-only | -| `RWX` | **ReadWriteMany** – Many nodes read/write | -| `RWOP` | **ReadWriteOncePod** – One Pod only | +| Mode | Description | +| ------ | ---------------------------------------------- | +| `RWO` | **ReadWriteOnce** – One node can read/write | +| `ROX` | **ReadOnlyMany** – Multiple nodes read-only | +| `RWX` | **ReadWriteMany** – Multiple nodes read/write | +| `RWOP` | **ReadWriteOncePod** – Only one Pod can use it | --- -## πŸ› οΈ Check PVs via CLI +## πŸ› οΈ CLI Commands to Manage PVs ```bash +# List all Persistent Volumes kubectl get pv + +# List all Persistent Volume Claims +kubectl get pvc + +# Edit a PVC +kubectl edit pvc -n ``` --- -## πŸš€ Example: Deployment using HostPath Volume +## πŸš€ Example 1: Deployment with `hostPath` Volume ```yaml apiVersion: apps/v1 @@ -73,18 +82,16 @@ metadata: name: app-1 namespace: dev labels: - label1: test1 - app.kubernetes.io/label2: test2 + app.kubernetes.io/name: nginx spec: replicas: 3 selector: matchLabels: - app.kubernetes.io/label2: test2 + app.kubernetes.io/name: nginx template: metadata: labels: - app.kubernetes.io/label2: test2 - os: linux + app.kubernetes.io/name: nginx spec: containers: - name: nginx @@ -93,16 +100,19 @@ spec: - containerPort: 80 volumeMounts: - name: nginx-log - mountPath: /var/log/nginx + mountPath: /var/log/nginx volumes: - name: nginx-log hostPath: path: /root/nginx/logs + type: DirectoryOrCreate ``` --- -## πŸ“‚ Example: PersistentVolume with NFS +## πŸ“‚ Example 2: NFS-backed Persistent Volume + +### 🧩 PersistentVolume Definition ```yaml apiVersion: v1 @@ -110,12 +120,12 @@ kind: PersistentVolume metadata: name: pv1 spec: - capacity: + capacity: storage: 5Gi volumeMode: Filesystem - accessModes: + accessModes: - ReadWriteMany - persistentVolumeReclaimPolicy: Recycle + persistentVolumeReclaimPolicy: Retain storageClassName: custom-name mountOptions: - hard @@ -125,3 +135,58 @@ spec: server: 192.168.1.10 ``` +### πŸ“„ PersistentVolumeClaim + +```yaml +apiVersion: v1 +kind: PersistentVolumeClaim +metadata: + name: pvc1 + namespace: ns-test +spec: + accessModes: + - ReadWriteMany + resources: + requests: + storage: 1Gi + storageClassName: custom-name +``` + +### πŸš€ Deployment Using PVC + +```yaml +apiVersion: apps/v1 +kind: Deployment +metadata: + name: app-1 + namespace: dev + labels: + app.kubernetes.io/name: nginx +spec: + replicas: 3 + selector: + matchLabels: + app.kubernetes.io/name: nginx + template: + metadata: + labels: + app.kubernetes.io/name: nginx + spec: + containers: + - name: nginx + image: nginx + ports: + - containerPort: 80 + volumeMounts: + - name: nginx-log + mountPath: /var/log/nginx + volumes: + - name: nginx-log + persistentVolumeClaim: + claimName: pvc1 +``` + +--- + +## πŸ“ Common Mistakes Fixed +