持久卷 #
一、持久卷概述 #
持久卷(Persistent Volume,PV)是集群级别的存储资源,持久卷声明(Persistent Volume Claim,PVC)是用户对存储的请求。
1.1 PV和PVC关系 #
text
PV和PVC关系
│
├── PV (PersistentVolume)
│ └── 集群级存储资源
│
├── PVC (PersistentVolumeClaim)
│ └── 命名空间级存储请求
│
└── 绑定关系
└── PVC绑定到满足条件的PV
1.2 存储生命周期 #
text
存储生命周期
│
├── Provisioning(供给)
│ ├── 静态供给
│ └── 动态供给
│
├── Binding(绑定)
│ └── PVC绑定PV
│
├── Using(使用)
│ └── Pod通过PVC使用存储
│
└── Reclaiming(回收)
├── Retain(保留)
├── Delete(删除)
└── Recycle(回收,已废弃)
二、PersistentVolume #
2.1 创建PV #
yaml
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv-nfs
spec:
capacity:
storage: 5Gi
volumeMode: Filesystem
accessModes:
- ReadWriteOnce
- ReadOnlyMany
persistentVolumeReclaimPolicy: Retain
storageClassName: nfs
nfs:
server: 192.168.1.100
path: /data/pv1
2.2 访问模式 #
| 模式 | 缩写 | 说明 |
|---|---|---|
| ReadWriteOnce | RWO | 单节点读写 |
| ReadOnlyMany | ROX | 多节点只读 |
| ReadWriteMany | RWX | 多节点读写 |
| ReadWriteOncePod | RWOP | 单Pod读写 |
2.3 回收策略 #
| 策略 | 说明 |
|---|---|
| Retain | 保留数据,需手动清理 |
| Delete | 删除PV和底层存储 |
| Recycle | 清空数据(已废弃) |
2.4 PV状态 #
| 状态 | 说明 |
|---|---|
| Available | 可用 |
| Bound | 已绑定 |
| Released | 已释放 |
| Failed | 失败 |
2.5 不同存储类型PV #
yaml
# NFS PV
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv-nfs
spec:
capacity:
storage: 5Gi
accessModes:
- ReadWriteMany
nfs:
server: 192.168.1.100
path: /data/pv
---
# HostPath PV
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv-hostpath
spec:
capacity:
storage: 10Gi
accessModes:
- ReadWriteOnce
hostPath:
path: /data/pv
type: DirectoryOrCreate
---
# iSCSI PV
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv-iscsi
spec:
capacity:
storage: 20Gi
accessModes:
- ReadWriteOnce
iscsi:
targetPortal: 192.168.1.200:3260
iqn: iqn.2001-04.com.example:storage.target00
lun: 0
fsType: ext4
三、PersistentVolumeClaim #
3.1 创建PVC #
yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: pvc-demo
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 5Gi
storageClassName: nfs
bash
# 创建PVC
kubectl apply -f pvc.yaml
# 查看PVC
kubectl get pvc
# 输出示例
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
pvc-demo Bound pv-nfs 5Gi RWO nfs 10s
3.2 PVC状态 #
| 状态 | 说明 |
|---|---|
| Pending | 等待绑定 |
| Bound | 已绑定 |
| Lost | 丢失 |
3.3 选择器匹配 #
yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: pvc-selector
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 5Gi
selector:
matchLabels:
environment: production
matchExpressions:
- key: tier
operator: In
values:
- database
四、使用PVC #
4.1 在Pod中使用 #
yaml
apiVersion: v1
kind: Pod
metadata:
name: pvc-pod
spec:
containers:
- name: nginx
image: nginx
volumeMounts:
- name: data
mountPath: /usr/share/nginx/html
volumes:
- name: data
persistentVolumeClaim:
claimName: pvc-demo
4.2 在Deployment中使用 #
yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 1
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
volumeMounts:
- name: data
mountPath: /usr/share/nginx/html
volumes:
- name: data
persistentVolumeClaim:
claimName: pvc-demo
4.3 在StatefulSet中使用 #
yaml
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: web
spec:
serviceName: web
replicas: 3
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
containers:
- name: nginx
image: nginx
volumeMounts:
- name: www
mountPath: /usr/share/nginx/html
volumeClaimTemplates:
- metadata:
name: www
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
五、绑定机制 #
5.1 绑定过程 #
text
PVC绑定过程
│
├── 1. 用户创建PVC
│
├── 2. 控制器查找匹配的PV
│ ├── 容量满足
│ ├── 访问模式匹配
│ └── StorageClass匹配
│
├── 3. 绑定PV到PVC
│
└── 4. 更新PV和PVC状态
5.2 绑定条件 #
| 条件 | 说明 |
|---|---|
| 容量 | PV容量 >= PVC请求容量 |
| 访问模式 | PV支持PVC的访问模式 |
| StorageClass | 相同的StorageClass |
| 选择器 | 满足标签选择器 |
六、PV管理 #
6.1 查看PV #
bash
# 查看PV列表
kubectl get pv
# 查看PV详情
kubectl describe pv <pv-name>
# 查看PV YAML
kubectl get pv <pv-name> -o yaml
6.2 查看PVC #
bash
# 查看PVC列表
kubectl get pvc
# 查看PVC详情
kubectl describe pvc <pvc-name>
# 查看特定命名空间
kubectl get pvc -n <namespace>
6.3 删除PVC #
bash
# 删除PVC
kubectl delete pvc <pvc-name>
# 删除后PV状态变为Released
# 根据回收策略处理数据
七、完整示例 #
7.1 静态供给完整示例 #
yaml
# PV定义
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv-web
labels:
app: web
environment: production
spec:
capacity:
storage: 10Gi
volumeMode: Filesystem
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Retain
storageClassName: manual
nfs:
server: 192.168.1.100
path: /data/web
---
# PVC定义
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: pvc-web
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10Gi
storageClassName: manual
selector:
matchLabels:
app: web
---
# Pod使用PVC
apiVersion: v1
kind: Pod
metadata:
name: web-pod
spec:
containers:
- name: nginx
image: nginx
volumeMounts:
- name: data
mountPath: /usr/share/nginx/html
volumes:
- name: data
persistentVolumeClaim:
claimName: pvc-web
八、故障排查 #
8.1 常见问题 #
bash
# 查看PVC状态
kubectl describe pvc <pvc-name>
# 查看PV状态
kubectl describe pv <pv-name>
# 查看事件
kubectl get events --field-selector involvedObject.name=<pvc-name>
8.2 问题诊断 #
| 问题 | 原因 | 解决方案 |
|---|---|---|
| PVC一直Pending | 无匹配PV | 创建匹配的PV |
| 绑定失败 | 条件不满足 | 检查容量和访问模式 |
| PV不可用 | 存储故障 | 检查存储后端 |
九、最佳实践 #
9.1 命名规范 #
text
命名建议
│
├── PV命名:pv-<storage-type>-<purpose>
│ └── 示例:pv-nfs-web
│
└── PVC命名:pvc-<app-name>-<purpose>
└── 示例:pvc-nginx-data
9.2 容量规划 #
yaml
# 合理设置容量
resources:
requests:
storage: 10Gi
# PV容量略大于PVC请求
capacity:
storage: 12Gi
9.3 安全配置 #
yaml
# 只读挂载
volumeMounts:
- name: data
mountPath: /data
readOnly: true
十、总结 #
10.1 核心要点 #
| 概念 | 说明 |
|---|---|
| PV | 集群级存储资源 |
| PVC | 命名空间级存储请求 |
| 绑定 | PVC匹配并绑定PV |
| 回收策略 | Retain/Delete |
10.2 下一步 #
掌握了PV和PVC后,让我们学习 存储类,了解动态存储供给机制。
最后更新:2026-03-28