update pvc and pvc doc
This commit is contained in:
@@ -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**.
|
||||
|
||||
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 node’s filesystem into a Pod.
|
||||
* Suitable **only for local development or single-node clusters** such as Minikube or Kind.
|
||||
* Not recommended for production workloads.
|
||||
* Mounts a directory or file from the host node’s filesystem into a Pod.
|
||||
* Suitable only for **local development** or **single-node clusters** (e.g., Minikube, Kind).
|
||||
* **Not recommended** for production environments.
|
||||
|
||||
### 2. NFS
|
||||
### **2.2 NFS**
|
||||
|
||||
* Network File System; allows multiple Pods and nodes to share storage.
|
||||
* Recommended for shared or distributed environments.
|
||||
* Uses Network File System (NFS) to allow multiple Pods and nodes to share storage.
|
||||
* Recommended for **shared or distributed** setups.
|
||||
|
||||
### 3. Cloud Volumes
|
||||
### **2.3 Cloud Volumes**
|
||||
|
||||
* **AWS:** `awsElasticBlockStore`
|
||||
* **GCP:** `gcePersistentDisk`
|
||||
* **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.
|
||||
2. **Persistent Volume Claim (PVC)** – A user request for storage with specific size and access requirements.
|
||||
3. **StorageClass** – Defines how dynamic provisioning should occur (provisioner, reclaim policy, parameters).
|
||||
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 of specific size and access modes.
|
||||
3. **StorageClass:** Defines dynamic provisioning behavior (provisioner, reclaim policy, parameters).
|
||||
|
||||
---
|
||||
|
||||
## PV Lifecycle Phases
|
||||
## **4. PV Lifecycle Phases**
|
||||
|
||||
| State | Description |
|
||||
| ------------- | -------------------------------------------------------- |
|
||||
| **Available** | PV is ready to be bound. |
|
||||
| **Bound** | PV is bound to a PVC. |
|
||||
| **Released** | PVC was deleted; PV is unbound but data may still exist. |
|
||||
| **Failed** | Automatic cleanup failed. |
|
||||
| **State** | **Description** |
|
||||
| ------------- | ------------------------------------------------ |
|
||||
| **Available** | PV is ready to be bound to a claim. |
|
||||
| **Bound** | PV is bound to a PVC. |
|
||||
| **Released** | PVC was deleted; PV is unbound but retains data. |
|
||||
| **Failed** | Automatic cleanup failed. |
|
||||
|
||||
### Reclaim Policies:
|
||||
### **Reclaim Policies**
|
||||
|
||||
* **Delete:** Removes the underlying storage resource.
|
||||
* **Retain:** Keeps data for manual recovery.
|
||||
* **Recycle:** Deprecated (previously used to scrub the volume).
|
||||
* **Delete:** Deletes the underlying storage resource.
|
||||
* **Retain:** Keeps the data for manual recovery.
|
||||
* **Recycle:** Deprecated (was used to scrub the volume).
|
||||
|
||||
---
|
||||
|
||||
## PV Access Modes
|
||||
## **5. PV Access Modes**
|
||||
|
||||
| Mode | Description |
|
||||
| ------------------------- | -------------------------------------------------------------------- |
|
||||
| `ReadWriteOnce` (RWO) | One node can read/write. |
|
||||
| `ReadOnlyMany` (ROX) | Many nodes can read. |
|
||||
| `ReadWriteMany` (RWX) | Many nodes can read/write. |
|
||||
| `ReadWriteOncePod` (RWOP) | Only one Pod can mount it with read/write access (Kubernetes ≥1.22). |
|
||||
| **Access Mode** | **Description** |
|
||||
| ------------------------- | ----------------------------------------------------------- |
|
||||
| `ReadWriteOnce` (RWO) | Volume can be mounted as read-write by a single node. |
|
||||
| `ReadOnlyMany` (ROX) | Volume can be mounted read-only by multiple nodes. |
|
||||
| `ReadWriteMany` (RWX) | Volume can be mounted as read-write by multiple nodes. |
|
||||
| `ReadWriteOncePod` (RWOP) | Volume can be mounted read-write by a single Pod. (≥ v1.22) |
|
||||
|
||||
---
|
||||
|
||||
## CLI Commands to Manage PVs & PVCs
|
||||
## **6. CLI Commands**
|
||||
|
||||
```bash
|
||||
# List all Persistent Volumes
|
||||
kubectl get pv
|
||||
|
||||
# List all Persistent Volume Claims
|
||||
# List all Persistent Volume Claims (across all namespaces)
|
||||
kubectl get pvc -A
|
||||
|
||||
# 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
|
||||
apiVersion: apps/v1
|
||||
@@ -122,15 +121,12 @@ spec:
|
||||
type: DirectoryOrCreate
|
||||
```
|
||||
|
||||
**Valid `hostPath` Types:**
|
||||
**Valid `hostPath` types:**
|
||||
`DirectoryOrCreate`, `Directory`, `FileOrCreate`, `File`, `Socket`, `CharDevice`, `BlockDevice`
|
||||
|
||||
---
|
||||
|
||||
## 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`:
|
||||
## **8. Example: Static Persistent Volume (PV)**
|
||||
|
||||
```yaml
|
||||
apiVersion: v1
|
||||
@@ -150,7 +146,7 @@ spec:
|
||||
|
||||
---
|
||||
|
||||
## Example: Persistent Volume Claim (PVC)
|
||||
## **9. Example: Persistent Volume Claim (PVC)**
|
||||
|
||||
```yaml
|
||||
apiVersion: v1
|
||||
@@ -165,19 +161,14 @@ spec:
|
||||
requests:
|
||||
storage: 64Mi
|
||||
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
|
||||
apiVersion: v1
|
||||
@@ -199,7 +190,7 @@ spec:
|
||||
server: 192.168.6.160
|
||||
```
|
||||
|
||||
### Persistent Volume Claim (PVC)
|
||||
### **Persistent Volume Claim (PVC)**
|
||||
|
||||
```yaml
|
||||
apiVersion: v1
|
||||
@@ -219,7 +210,7 @@ spec:
|
||||
|
||||
---
|
||||
|
||||
## Static StorageClass for Pre-Provisioned Volumes
|
||||
## **11. Example: Static StorageClass**
|
||||
|
||||
```yaml
|
||||
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.
|
||||
2. **Set `volumeBindingMode: WaitForFirstConsumer`** in StorageClasses for node-aware provisioning.
|
||||
3. **Avoid using `hostPath` in multi-node clusters**; use NFS or CSI drivers.
|
||||
4. **Check events** for troubleshooting PV/PVC binding:
|
||||
```yaml
|
||||
apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
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
|
||||
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.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user