update pvc and pvc doc

This commit is contained in:
2025-10-08 23:54:00 +03:30
parent 564fa3c97f
commit 34d19fe29e

View File

@@ -1,81 +1,80 @@
# Kubernetes Persistent Volumes (PV) Cheat Sheet # **Kubernetes Persistent Volumes (PV) Technical Reference Guide**
## What is a Persistent Volume (PV)? ## **1. Overview**
A **Persistent Volume (PV)** is a piece of storage in a Kubernetes cluster that can be: A **Persistent Volume (PV)** is a storage resource in Kubernetes that allows data to persist beyond the lifecycle of Pods.
Persistent Volumes can be:
* **Pre-provisioned** by an administrator, or * **Pre-provisioned** by a cluster administrator, or
* **Dynamically provisioned** using a **StorageClass**. * **Dynamically provisioned** using a **StorageClass**.
PVs allow data to **persist beyond the lifecycle of individual Pods**.
--- ---
## PV Storage Options ## **2. PV Storage Options**
### 1. HostPath ### **2.1 HostPath**
* Mounts a file or directory from the host nodes filesystem into a Pod. * Mounts a directory or file from the host nodes filesystem into a Pod.
* Suitable **only for local development or single-node clusters** such as Minikube or Kind. * Suitable only for **local development** or **single-node clusters** (e.g., Minikube, Kind).
* Not recommended for production workloads. * **Not recommended** for production environments.
### 2. NFS ### **2.2 NFS**
* Network File System; allows multiple Pods and nodes to share storage. * Uses Network File System (NFS) to allow multiple Pods and nodes to share storage.
* Recommended for shared or distributed environments. * Recommended for **shared or distributed** setups.
### 3. Cloud Volumes ### **2.3 Cloud Volumes**
* **AWS:** `awsElasticBlockStore` * **AWS:** `awsElasticBlockStore`
* **GCP:** `gcePersistentDisk` * **GCP:** `gcePersistentDisk`
* **Azure:** `azureDisk` or `azureFile` * **Azure:** `azureDisk` or `azureFile`
* **CSI Drivers:** Preferred modern approach for all major clouds and on-prem solutions. * **CSI Drivers:** Preferred for all modern cloud and on-prem environments.
--- ---
## Kubernetes Storage Architecture ## **3. Kubernetes Storage Architecture**
1. **Persistent Volume (PV)** Represents the actual storage resource, managed by the cluster admin or a dynamic provisioner. 1. **Persistent Volume (PV):** Represents the actual storage resource, either statically created or dynamically provisioned.
2. **Persistent Volume Claim (PVC)** A user request for storage with specific size and access requirements. 2. **Persistent Volume Claim (PVC):** A user request for storage of specific size and access modes.
3. **StorageClass** Defines how dynamic provisioning should occur (provisioner, reclaim policy, parameters). 3. **StorageClass:** Defines dynamic provisioning behavior (provisioner, reclaim policy, parameters).
--- ---
## PV Lifecycle Phases ## **4. PV Lifecycle Phases**
| State | Description | | **State** | **Description** |
| ------------- | -------------------------------------------------------- | | ------------- | ------------------------------------------------ |
| **Available** | PV is ready to be bound. | | **Available** | PV is ready to be bound to a claim. |
| **Bound** | PV is bound to a PVC. | | **Bound** | PV is bound to a PVC. |
| **Released** | PVC was deleted; PV is unbound but data may still exist. | | **Released** | PVC was deleted; PV is unbound but retains data. |
| **Failed** | Automatic cleanup failed. | | **Failed** | Automatic cleanup failed. |
### Reclaim Policies: ### **Reclaim Policies**
* **Delete:** Removes the underlying storage resource. * **Delete:** Deletes the underlying storage resource.
* **Retain:** Keeps data for manual recovery. * **Retain:** Keeps the data for manual recovery.
* **Recycle:** Deprecated (previously used to scrub the volume). * **Recycle:** Deprecated (was used to scrub the volume).
--- ---
## PV Access Modes ## **5. PV Access Modes**
| Mode | Description | | **Access Mode** | **Description** |
| ------------------------- | -------------------------------------------------------------------- | | ------------------------- | ----------------------------------------------------------- |
| `ReadWriteOnce` (RWO) | One node can read/write. | | `ReadWriteOnce` (RWO) | Volume can be mounted as read-write by a single node. |
| `ReadOnlyMany` (ROX) | Many nodes can read. | | `ReadOnlyMany` (ROX) | Volume can be mounted read-only by multiple nodes. |
| `ReadWriteMany` (RWX) | Many nodes can read/write. | | `ReadWriteMany` (RWX) | Volume can be mounted as read-write by multiple nodes. |
| `ReadWriteOncePod` (RWOP) | Only one Pod can mount it with read/write access (Kubernetes ≥1.22). | | `ReadWriteOncePod` (RWOP) | Volume can be mounted read-write by a single Pod. (≥ v1.22) |
--- ---
## CLI Commands to Manage PVs & PVCs ## **6. CLI Commands**
```bash ```bash
# List all Persistent Volumes # List all Persistent Volumes
kubectl get pv kubectl get pv
# List all Persistent Volume Claims # List all Persistent Volume Claims (across all namespaces)
kubectl get pvc -A kubectl get pvc -A
# Describe a specific PV or PVC # Describe a specific PV or PVC
@@ -89,7 +88,7 @@ kubectl delete pvc <pvc-name> -n <namespace>
--- ---
## Example: Deployment with `hostPath` Volume ## **7. Example: Deployment with HostPath Volume**
```yaml ```yaml
apiVersion: apps/v1 apiVersion: apps/v1
@@ -122,15 +121,12 @@ spec:
type: DirectoryOrCreate type: DirectoryOrCreate
``` ```
**Valid `hostPath` Types:** **Valid `hostPath` types:**
`DirectoryOrCreate`, `Directory`, `FileOrCreate`, `File`, `Socket`, `CharDevice`, `BlockDevice` `DirectoryOrCreate`, `Directory`, `FileOrCreate`, `File`, `Socket`, `CharDevice`, `BlockDevice`
--- ---
## Example: Static Persistent Volume (PV) ## **8. Example: Static Persistent Volume (PV)**
**Important:** A PV **must specify a volume source type** (this was the cause of your validation error).
For example, this corrected PV uses `hostPath`:
```yaml ```yaml
apiVersion: v1 apiVersion: v1
@@ -150,7 +146,7 @@ spec:
--- ---
## Example: Persistent Volume Claim (PVC) ## **9. Example: Persistent Volume Claim (PVC)**
```yaml ```yaml
apiVersion: v1 apiVersion: v1
@@ -165,19 +161,14 @@ spec:
requests: requests:
storage: 64Mi storage: 64Mi
storageClassName: manual storageClassName: manual
```
To bind this PVC to a specific PV manually:
```yaml
volumeName: pv001 volumeName: pv001
``` ```
--- ---
## NFS-Based Persistent Volume ## **10. Example: NFS-Based PV and PVC**
### Persistent Volume (PV) ### **Persistent Volume (PV)**
```yaml ```yaml
apiVersion: v1 apiVersion: v1
@@ -199,7 +190,7 @@ spec:
server: 192.168.6.160 server: 192.168.6.160
``` ```
### Persistent Volume Claim (PVC) ### **Persistent Volume Claim (PVC)**
```yaml ```yaml
apiVersion: v1 apiVersion: v1
@@ -219,7 +210,7 @@ spec:
--- ---
## Static StorageClass for Pre-Provisioned Volumes ## **11. Example: Static StorageClass**
```yaml ```yaml
apiVersion: storage.k8s.io/v1 apiVersion: storage.k8s.io/v1
@@ -233,15 +224,55 @@ reclaimPolicy: Retain
--- ---
## Recommended Additions ## **12. Example: Nginx Deployment Using PVC**
1. **Always include `storageClassName`** to ensure predictable binding. ```yaml
2. **Set `volumeBindingMode: WaitForFirstConsumer`** in StorageClasses for node-aware provisioning. apiVersion: apps/v1
3. **Avoid using `hostPath` in multi-node clusters**; use NFS or CSI drivers. kind: Deployment
4. **Check events** for troubleshooting PV/PVC binding: metadata:
name: nginx
namespace: web-app
spec:
replicas: 1
selector:
matchLabels:
app_type: nginx-web
template:
metadata:
labels:
app_type: nginx-web
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
volumeMounts:
- name: nginx-configs-volume
mountPath: /etc/nginx/conf.d/default.conf
subPath: reverse_proxy.conf
- name: nginx-logs-volume
mountPath: /var/log/nginx
volumes:
- name: nginx-configs-volume
configMap:
name: nginx-configs
- name: nginx-logs-volume
persistentVolumeClaim:
claimName: nginx-pvc
```
---
## **13. Best Practices and Recommendations**
1. **Always specify `storageClassName`** for predictable PV/PVC binding.
2. **Use `volumeBindingMode: WaitForFirstConsumer`** to ensure node-aware provisioning.
3. **Avoid `hostPath`** in multi-node or production clusters; use NFS or CSI drivers instead.
4. **Monitor events** for troubleshooting binding issues:
```bash ```bash
kubectl describe pvc <pvc-name> -n <namespace> kubectl describe pvc <pvc-name> -n <namespace>
``` ```
5. **Automate cleanup** with proper reclaim policies or storage lifecycle controllers. 5. **Implement proper reclaim policies** or automated cleanup mechanisms for storage lifecycle management.