update pv & pvc doc
This commit is contained in:
@@ -1,77 +1,95 @@
|
|||||||
# 🌐 Kubernetes Persistent Volumes (PV) Cheat Sheet
|
# Kubernetes Persistent Volumes (PV) Cheat Sheet
|
||||||
|
|
||||||
## 📦 What is a Persistent Volume (PV)?
|
## What is a Persistent Volume (PV)?
|
||||||
|
|
||||||
A **Persistent Volume (PV)** is a piece of storage in a Kubernetes cluster that can be:
|
A **Persistent Volume (PV)** is a piece of storage in a Kubernetes cluster that can be:
|
||||||
|
|
||||||
- **Pre-provisioned** by an administrator, or
|
* **Pre-provisioned** by an administrator, or
|
||||||
- **Dynamically provisioned** using a **StorageClass**.
|
* **Dynamically provisioned** using a **StorageClass**.
|
||||||
|
|
||||||
> PVs allow data to **persist beyond the lifecycle of individual Pods**.
|
PVs allow data to **persist beyond the lifecycle of individual Pods**.
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## 📁 PV Storage Options
|
## PV Storage Options
|
||||||
|
|
||||||
### 1. **HostPath**
|
### 1. HostPath
|
||||||
- Mounts a file or directory from the host node’s filesystem into a Pod.
|
|
||||||
- Only suitable for **single-node testing or development** environments.
|
|
||||||
|
|
||||||
### 2. **Persistent Volume (PV) & Persistent Volume Claim (PVC)**
|
* Mounts a file or directory from the host node’s filesystem into a Pod.
|
||||||
- **PV**: Represents the actual physical or virtual storage resource.
|
* Suitable **only for local development or single-node clusters** such as Minikube or Kind.
|
||||||
- **PVC**: A user's request for specific storage resources and access modes.
|
* Not recommended for production workloads.
|
||||||
|
|
||||||
|
### 2. NFS
|
||||||
|
|
||||||
|
* Network File System; allows multiple Pods and nodes to share storage.
|
||||||
|
* Recommended for shared or distributed environments.
|
||||||
|
|
||||||
|
### 3. Cloud Volumes
|
||||||
|
|
||||||
|
* **AWS:** `awsElasticBlockStore`
|
||||||
|
* **GCP:** `gcePersistentDisk`
|
||||||
|
* **Azure:** `azureDisk` or `azureFile`
|
||||||
|
* **CSI Drivers:** Preferred modern approach for all major clouds and on-prem solutions.
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## 🧱 Kubernetes Storage Architecture
|
## Kubernetes Storage Architecture
|
||||||
|
|
||||||
1. **Persistent Volume (PV)** – The actual storage unit, managed by the admin or provisioned dynamically.
|
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’s request for a certain amount and type of storage.
|
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).
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## 🔄 PV Lifecycle Phases
|
## PV Lifecycle Phases
|
||||||
|
|
||||||
| **State** | **Description** |
|
| State | Description |
|
||||||
|---------------|---------------------------------------------|
|
| ------------- | -------------------------------------------------------- |
|
||||||
| Provisioning | PV is being created or initialized. |
|
| **Available** | PV is ready to be bound. |
|
||||||
| Binding | PV is bound to a PVC. |
|
| **Bound** | PV is bound to a PVC. |
|
||||||
| Using | PV is in use by a Pod. |
|
| **Released** | PVC was deleted; PV is unbound but data may still exist. |
|
||||||
| Releasing | PVC is deleted; PV becomes unbound. |
|
| **Failed** | Automatic cleanup failed. |
|
||||||
| Reclaiming | Based on reclaim policy: |
|
|
||||||
| | - `Delete`: Remove the storage. |
|
### Reclaim Policies:
|
||||||
| | - `Recycle`: Basic scrub (deprecated). |
|
|
||||||
| | - `Retain`: Manual cleanup required. |
|
* **Delete:** Removes the underlying storage resource.
|
||||||
|
* **Retain:** Keeps data for manual recovery.
|
||||||
|
* **Recycle:** Deprecated (previously used to scrub the volume).
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## 🔒 PV Access Modes
|
## PV Access Modes
|
||||||
|
|
||||||
| **Mode** | **Description** |
|
| Mode | Description |
|
||||||
|------------|-------------------------------------------------|
|
| ------------------------- | -------------------------------------------------------------------- |
|
||||||
| `RWO` | **ReadWriteOnce** – One node can read/write. |
|
| `ReadWriteOnce` (RWO) | One node can read/write. |
|
||||||
| `ROX` | **ReadOnlyMany** – Multiple nodes can read. |
|
| `ReadOnlyMany` (ROX) | Many nodes can read. |
|
||||||
| `RWX` | **ReadWriteMany** – Multiple nodes can read/write. |
|
| `ReadWriteMany` (RWX) | Many nodes can read/write. |
|
||||||
| `RWOP` | **ReadWriteOncePod** – Only one Pod can mount it with read/write access. |
|
| `ReadWriteOncePod` (RWOP) | Only one Pod can mount it with read/write access (Kubernetes ≥1.22). |
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## 🛠️ CLI Commands to Manage PVs & PVCs
|
## CLI Commands to Manage PVs & PVCs
|
||||||
|
|
||||||
```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
|
||||||
kubectl get pvc
|
kubectl get pvc -A
|
||||||
|
|
||||||
# Edit a PVC
|
# Describe a specific PV or PVC
|
||||||
kubectl edit pvc -n <namespace> <pvc-name>
|
kubectl describe pv <pv-name>
|
||||||
|
kubectl describe pvc <pvc-name> -n <namespace>
|
||||||
|
|
||||||
|
# Delete a PV or PVC
|
||||||
|
kubectl delete pv <pv-name>
|
||||||
|
kubectl delete pvc <pvc-name> -n <namespace>
|
||||||
```
|
```
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## 🚀 Example: Deployment with `hostPath` Volume
|
## Example: Deployment with `hostPath` Volume
|
||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
apiVersion: apps/v1
|
apiVersion: apps/v1
|
||||||
@@ -83,11 +101,11 @@ spec:
|
|||||||
replicas: 3
|
replicas: 3
|
||||||
selector:
|
selector:
|
||||||
matchLabels:
|
matchLabels:
|
||||||
name: nginx
|
app: nginx
|
||||||
template:
|
template:
|
||||||
metadata:
|
metadata:
|
||||||
labels:
|
labels:
|
||||||
name: nginx
|
app: nginx
|
||||||
spec:
|
spec:
|
||||||
containers:
|
containers:
|
||||||
- name: nginx
|
- name: nginx
|
||||||
@@ -104,18 +122,15 @@ spec:
|
|||||||
type: DirectoryOrCreate
|
type: DirectoryOrCreate
|
||||||
```
|
```
|
||||||
|
|
||||||
### Valid `hostPath` Types:
|
**Valid `hostPath` Types:**
|
||||||
- `DirectoryOrCreate`
|
`DirectoryOrCreate`, `Directory`, `FileOrCreate`, `File`, `Socket`, `CharDevice`, `BlockDevice`
|
||||||
- `Directory`
|
|
||||||
- `FileOrCreate`
|
|
||||||
- `File`
|
|
||||||
- `Socket`
|
|
||||||
- `CharDevice`
|
|
||||||
- `BlockDevice`
|
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## 📄 Example: Static Persistent Volume (PV)
|
## 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
|
||||||
@@ -128,11 +143,14 @@ spec:
|
|||||||
accessModes:
|
accessModes:
|
||||||
- ReadWriteMany
|
- ReadWriteMany
|
||||||
persistentVolumeReclaimPolicy: Retain
|
persistentVolumeReclaimPolicy: Retain
|
||||||
|
storageClassName: manual
|
||||||
|
hostPath:
|
||||||
|
path: /mnt/data/pv001
|
||||||
```
|
```
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## 📄 Example: Persistent Volume Claim (PVC)
|
## Example: Persistent Volume Claim (PVC)
|
||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
apiVersion: v1
|
apiVersion: v1
|
||||||
@@ -146,16 +164,18 @@ spec:
|
|||||||
resources:
|
resources:
|
||||||
requests:
|
requests:
|
||||||
storage: 64Mi
|
storage: 64Mi
|
||||||
|
storageClassName: manual
|
||||||
```
|
```
|
||||||
|
|
||||||
> ✅ To bind a PVC to a specific PV, add `volumeName: <pv-name>` in the PVC spec:
|
To bind this PVC to a specific PV manually:
|
||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
volumeName: pv001
|
volumeName: pv001
|
||||||
```
|
```
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## 🌐 NFS-Based Persistent Volume
|
## NFS-Based Persistent Volume
|
||||||
|
|
||||||
### Persistent Volume (PV)
|
### Persistent Volume (PV)
|
||||||
|
|
||||||
@@ -199,7 +219,7 @@ spec:
|
|||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## 🏗️ Static StorageClass for Pre-Provisioned Volumes
|
## Static StorageClass for Pre-Provisioned Volumes
|
||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
apiVersion: storage.k8s.io/v1
|
apiVersion: storage.k8s.io/v1
|
||||||
@@ -207,4 +227,21 @@ kind: StorageClass
|
|||||||
metadata:
|
metadata:
|
||||||
name: nginx-files
|
name: nginx-files
|
||||||
provisioner: kubernetes.io/no-provisioner
|
provisioner: kubernetes.io/no-provisioner
|
||||||
|
volumeBindingMode: WaitForFirstConsumer
|
||||||
|
reclaimPolicy: Retain
|
||||||
```
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Recommended Additions
|
||||||
|
|
||||||
|
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:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
kubectl describe pvc <pvc-name> -n <namespace>
|
||||||
|
```
|
||||||
|
5. **Automate cleanup** with proper reclaim policies or storage lifecycle controllers.
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user