update kuber pv doc
This commit is contained in:
@@ -1,94 +1,71 @@
|
|||||||
# **Kubernetes Persistent Volumes (PV) – Technical Reference Guide**
|
# 🌐 Kubernetes: Persistent Volumes (PV) Cheat Sheet
|
||||||
|
|
||||||
## **1. Overview**
|
## 📦 What is a Persistent Volume (PV)?
|
||||||
|
|
||||||
A **Persistent Volume (PV)** is a storage resource in Kubernetes that allows data to persist beyond the lifecycle of Pods.
|
A **Persistent Volume** is a piece of storage in a Kubernetes cluster that has been provisioned by an administrator or dynamically provisioned using **StorageClasses**.
|
||||||
Persistent Volumes can be:
|
|
||||||
|
|
||||||
* **Pre-provisioned** by a cluster administrator, or
|
|
||||||
* **Dynamically provisioned** using a **StorageClass**.
|
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## **2. PV Storage Options**
|
## 📁 PV Storage Options
|
||||||
|
|
||||||
### **2.1 HostPath**
|
1. **HostPath**
|
||||||
|
|
||||||
* Mounts a directory or file from the host node’s filesystem into a Pod.
|
* Mounts a file or directory from the host node’s filesystem into your Pod.
|
||||||
* Suitable only for **local development** or **single-node clusters** (e.g., Minikube, Kind).
|
|
||||||
* **Not recommended** for production environments.
|
|
||||||
|
|
||||||
### **2.2 NFS**
|
2. **Using PV and PVC**
|
||||||
|
|
||||||
* Uses Network File System (NFS) to allow multiple Pods and nodes to share storage.
|
* **PV (Persistent Volume):** The actual storage resource.
|
||||||
* Recommended for **shared or distributed** setups.
|
* **PVC (Persistent Volume Claim):** A request for storage by a user.
|
||||||
|
|
||||||
### **2.3 Cloud Volumes**
|
|
||||||
|
|
||||||
* **AWS:** `awsElasticBlockStore`
|
|
||||||
* **GCP:** `gcePersistentDisk`
|
|
||||||
* **Azure:** `azureDisk` or `azureFile`
|
|
||||||
* **CSI Drivers:** Preferred for all modern cloud and on-prem environments.
|
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## **3. Kubernetes Storage Architecture**
|
## 🧱 Kubernetes Storage Layers
|
||||||
|
|
||||||
1. **Persistent Volume (PV):** Represents the actual storage resource, either statically created or dynamically provisioned.
|
1. **Persistent Volume (PV)**
|
||||||
2. **Persistent Volume Claim (PVC):** A user request for storage of specific size and access modes.
|
2. **Persistent Volume Claim (PVC)**
|
||||||
3. **StorageClass:** Defines dynamic provisioning behavior (provisioner, reclaim policy, parameters).
|
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## **4. PV Lifecycle Phases**
|
## 🔄 PV Lifecycle States
|
||||||
|
|
||||||
| **State** | **Description** |
|
| State | Description |
|
||||||
| ------------- | ------------------------------------------------ |
|
| ------------ | ---------------------------------------------------- |
|
||||||
| **Available** | PV is ready to be bound to a claim. |
|
| Provisioning | Kubernetes is preparing the PV. |
|
||||||
| **Bound** | PV is bound to a PVC. |
|
| Binding | The PV is bound to a PVC. |
|
||||||
| **Released** | PVC was deleted; PV is unbound but retains data. |
|
| Using | The bound PV is being used by a Pod. |
|
||||||
| **Failed** | Automatic cleanup failed. |
|
| Releasing | PVC is deleted; PV is no longer bound. |
|
||||||
|
| Reclaiming | PV is reclaimed using one of the following policies: |
|
||||||
### **Reclaim Policies**
|
| | • `Delete` – Remove the storage |
|
||||||
|
| | • `Recycle` – Basic scrub (deprecated) |
|
||||||
* **Delete:** Deletes the underlying storage resource.
|
| | • `Retain` – Manual reclaim |
|
||||||
* **Retain:** Keeps the data for manual recovery.
|
|
||||||
* **Recycle:** Deprecated (was used to scrub the volume).
|
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## **5. PV Access Modes**
|
## 🔒 PV Access Modes
|
||||||
|
|
||||||
| **Access Mode** | **Description** |
|
| Mode | Description |
|
||||||
| ------------------------- | ----------------------------------------------------------- |
|
| ------ | ------------------------------------------- |
|
||||||
| `ReadWriteOnce` (RWO) | Volume can be mounted as read-write by a single node. |
|
| `RWO` | **ReadWriteOnce** – One node can read/write |
|
||||||
| `ReadOnlyMany` (ROX) | Volume can be mounted read-only by multiple nodes. |
|
| `ROX` | **ReadOnlyMany** – Many nodes read-only |
|
||||||
| `ReadWriteMany` (RWX) | Volume can be mounted as read-write by multiple nodes. |
|
| `RWX` | **ReadWriteMany** – Many nodes read/write |
|
||||||
| `ReadWriteOncePod` (RWOP) | Volume can be mounted read-write by a single Pod. (≥ v1.22) |
|
| `RWOP` | **ReadWriteOncePod** – One Pod only |
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## **6. CLI Commands**
|
## 🛠️ Check PVs via CLI
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
# List all Persistent Volumes
|
|
||||||
kubectl get pv
|
kubectl get pv
|
||||||
|
|
||||||
# List all Persistent Volume Claims (across all namespaces)
|
|
||||||
kubectl get pvc -A
|
|
||||||
|
|
||||||
# Describe a specific PV or PVC
|
|
||||||
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>
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
```bash
|
||||||
|
kubectl get pvc
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## **7. Example: Deployment with HostPath Volume**
|
## 🚀 Example: Deployment using HostPath Volume
|
||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
apiVersion: apps/v1
|
apiVersion: apps/v1
|
||||||
@@ -96,15 +73,19 @@ kind: Deployment
|
|||||||
metadata:
|
metadata:
|
||||||
name: app-1
|
name: app-1
|
||||||
namespace: dev
|
namespace: dev
|
||||||
|
labels:
|
||||||
|
label1: test1
|
||||||
|
app.kubernetes.io/label2: test2
|
||||||
spec:
|
spec:
|
||||||
replicas: 3
|
replicas: 3
|
||||||
selector:
|
selector:
|
||||||
matchLabels:
|
matchLabels:
|
||||||
app: nginx
|
app.kubernetes.io/label2: test2
|
||||||
template:
|
template:
|
||||||
metadata:
|
metadata:
|
||||||
labels:
|
labels:
|
||||||
app: nginx
|
app.kubernetes.io/label2: test2
|
||||||
|
os: linux
|
||||||
spec:
|
spec:
|
||||||
containers:
|
containers:
|
||||||
- name: nginx
|
- name: nginx
|
||||||
@@ -113,166 +94,88 @@ spec:
|
|||||||
- containerPort: 80
|
- containerPort: 80
|
||||||
volumeMounts:
|
volumeMounts:
|
||||||
- name: nginx-log
|
- name: nginx-log
|
||||||
mountPath: /var/log/nginx
|
mountPath: /var/log/nginx
|
||||||
volumes:
|
volumes:
|
||||||
- name: nginx-log
|
- name: nginx-log
|
||||||
hostPath:
|
hostPath:
|
||||||
path: /root/nginx/logs
|
path: /root/nginx/logs
|
||||||
type: DirectoryOrCreate
|
|
||||||
```
|
```
|
||||||
|
|
||||||
**Valid `hostPath` types:**
|
|
||||||
`DirectoryOrCreate`, `Directory`, `FileOrCreate`, `File`, `Socket`, `CharDevice`, `BlockDevice`
|
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## **8. Example: Static Persistent Volume (PV)**
|
## 📂 Example: PersistentVolume with NFS
|
||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
apiVersion: v1
|
apiVersion: v1
|
||||||
kind: PersistentVolume
|
kind: PersistentVolume
|
||||||
metadata:
|
metadata:
|
||||||
name: pv001
|
name: pv1
|
||||||
spec:
|
spec:
|
||||||
capacity:
|
capacity:
|
||||||
storage: 128Mi
|
storage: 5Gi
|
||||||
accessModes:
|
volumeMode: Filesystem
|
||||||
|
accessModes:
|
||||||
- ReadWriteMany
|
- ReadWriteMany
|
||||||
persistentVolumeReclaimPolicy: Retain
|
persistentVolumeReclaimPolicy: Recycle
|
||||||
storageClassName: manual
|
storageClassName: custom-name
|
||||||
hostPath:
|
|
||||||
path: /mnt/data/pv001
|
|
||||||
```
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## **9. Example: Persistent Volume Claim (PVC)**
|
|
||||||
|
|
||||||
```yaml
|
|
||||||
apiVersion: v1
|
|
||||||
kind: PersistentVolumeClaim
|
|
||||||
metadata:
|
|
||||||
name: pvc-2
|
|
||||||
namespace: db
|
|
||||||
spec:
|
|
||||||
accessModes:
|
|
||||||
- ReadWriteOnce
|
|
||||||
resources:
|
|
||||||
requests:
|
|
||||||
storage: 64Mi
|
|
||||||
storageClassName: manual
|
|
||||||
volumeName: pv001
|
|
||||||
```
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## **10. Example: NFS-Based PV and PVC**
|
|
||||||
|
|
||||||
### **Persistent Volume (PV)**
|
|
||||||
|
|
||||||
```yaml
|
|
||||||
apiVersion: v1
|
|
||||||
kind: PersistentVolume
|
|
||||||
metadata:
|
|
||||||
name: pv-nfs-2
|
|
||||||
spec:
|
|
||||||
capacity:
|
|
||||||
storage: 128Mi
|
|
||||||
accessModes:
|
|
||||||
- ReadWriteMany
|
|
||||||
persistentVolumeReclaimPolicy: Retain
|
|
||||||
storageClassName: nginx-files
|
|
||||||
mountOptions:
|
mountOptions:
|
||||||
- hard
|
- hard
|
||||||
- nfsvers=4.2
|
- nfsvers=4.1
|
||||||
nfs:
|
nfs:
|
||||||
path: /root/Nginx_Files
|
path: /nfs/data
|
||||||
server: 192.168.6.160
|
server: 192.168.1.10
|
||||||
```
|
```
|
||||||
|
|
||||||
### **Persistent Volume Claim (PVC)**
|
|
||||||
|
|
||||||
```yaml
|
|
||||||
apiVersion: v1
|
|
||||||
kind: PersistentVolumeClaim
|
|
||||||
metadata:
|
|
||||||
name: pvc-1
|
|
||||||
namespace: db
|
|
||||||
spec:
|
|
||||||
volumeName: pv-nfs-2
|
|
||||||
accessModes:
|
|
||||||
- ReadWriteMany
|
|
||||||
resources:
|
|
||||||
requests:
|
|
||||||
storage: 64Mi
|
|
||||||
storageClassName: nginx-files
|
|
||||||
```
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## **11. Example: Static StorageClass**
|
|
||||||
|
|
||||||
```yaml
|
|
||||||
apiVersion: storage.k8s.io/v1
|
|
||||||
kind: StorageClass
|
|
||||||
metadata:
|
|
||||||
name: nginx-files
|
|
||||||
provisioner: kubernetes.io/no-provisioner
|
|
||||||
volumeBindingMode: WaitForFirstConsumer
|
|
||||||
reclaimPolicy: Retain
|
|
||||||
```
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## **12. Example: Nginx Deployment Using PVC**
|
|
||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
apiVersion: apps/v1
|
apiVersion: apps/v1
|
||||||
kind: Deployment
|
kind: Deployment
|
||||||
metadata:
|
metadata:
|
||||||
name: nginx
|
name: app-1
|
||||||
namespace: web-app
|
namespace: dev
|
||||||
|
labels:
|
||||||
|
label1: test1
|
||||||
|
app.kubernetes.io/label2: test2
|
||||||
spec:
|
spec:
|
||||||
replicas: 1
|
replicas: 3
|
||||||
selector:
|
selector:
|
||||||
matchLabels:
|
matchLabels:
|
||||||
app_type: nginx-web
|
app.kubernetes.io/label2: test2
|
||||||
template:
|
template:
|
||||||
metadata:
|
metadata:
|
||||||
labels:
|
labels:
|
||||||
app_type: nginx-web
|
app.kubernetes.io/label2: test2
|
||||||
|
os: linux
|
||||||
spec:
|
spec:
|
||||||
containers:
|
containers:
|
||||||
- name: nginx
|
- name: nginx
|
||||||
image: nginx:latest
|
image: nginx
|
||||||
ports:
|
ports:
|
||||||
- containerPort: 80
|
- containerPort: 80
|
||||||
volumeMounts:
|
volumeMounts:
|
||||||
- name: nginx-configs-volume
|
- name: nginx-log
|
||||||
mountPath: /etc/nginx/conf.d/default.conf
|
mountPath: /var/log/nginx
|
||||||
subPath: reverse_proxy.conf
|
volumes:
|
||||||
- name: nginx-logs-volume
|
- name: nginx-log
|
||||||
mountPath: /var/log/nginx
|
persisentVolumeClaim:
|
||||||
volumes:
|
claimName: pv1
|
||||||
- name: nginx-configs-volume
|
|
||||||
configMap:
|
|
||||||
name: nginx-configs
|
|
||||||
- name: nginx-logs-volume
|
|
||||||
persistentVolumeClaim:
|
|
||||||
claimName: nginx-pvc
|
|
||||||
```
|
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## **13. Best Practices and Recommendations**
|
apiVersion: v1
|
||||||
|
kind: persisentVolumeClaim
|
||||||
1. **Always specify `storageClassName`** for predictable PV/PVC binding.
|
metadata:
|
||||||
2. **Use `volumeBindingMode: WaitForFirstConsumer`** to ensure node-aware provisioning.
|
name: pvc1
|
||||||
3. **Avoid `hostPath`** in multi-node or production clusters; use NFS or CSI drivers instead.
|
namespace: ns-test
|
||||||
4. **Monitor events** for troubleshooting binding issues:
|
spec:
|
||||||
|
accessModes:
|
||||||
```bash
|
- ReadWriteMany
|
||||||
kubectl describe pvc <pvc-name> -n <namespace>
|
resources:
|
||||||
```
|
requests:
|
||||||
5. **Implement proper reclaim policies** or automated cleanup mechanisms for storage lifecycle management.
|
storage: 1Gi
|
||||||
|
limits:
|
||||||
|
storage: 2Gi
|
||||||
|
storageClassName: custom-name
|
||||||
|
```
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user