Wiki LogoWiki - The Power of Many

Kubernetes 存储: CSI 标准与持久化卷

深入理解容器存储接口 (CSI), PV/PVC 生命周期管理, StorageClass 动态供给及卷快照机制.

在容器化的无状态世界里, 如何让数据持久化? Kubernetes 通过 CSI (Container Storage Interface) 建立了一套标准, 允许不同的存储供应商 (AWS EBS, Ceph, NFS) 无缝接入集群.


1. 存储抽象层

1.1 核心对象

对象角色描述
PV管理员实际存储资源的抽象
PVC用户对存储的需求声明
StorageClass策略定义动态供给策略

2. PV 与 PVC

2.1 PV (Persistent Volume)

apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv-nfs
spec:
  capacity:
    storage: 100Gi
  accessModes:
  - ReadWriteMany
  persistentVolumeReclaimPolicy: Retain
  storageClassName: nfs
  nfs:
    server: nfs.example.com
    path: /exports/data
  mountOptions:
  - hard
  - nfsvers=4.1

2.2 PVC (Persistent Volume Claim)

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: data-pvc
spec:
  accessModes:
  - ReadWriteOnce
  resources:
    requests:
      storage: 10Gi
  storageClassName: fast-ssd
  selector:
    matchLabels:
      environment: production

2.3 绑定机制

匹配条件:

  • 容量 >= 请求
  • 访问模式包含请求的模式
  • StorageClass 匹配
  • Selector 匹配 (如果指定)

2.4 访问模式

模式缩写描述典型存储
ReadWriteOnceRWO单节点读写Block (EBS, Azure Disk)
ReadOnlyManyROX多节点只读NFS, CephFS
ReadWriteManyRWX多节点读写NFS, CephFS, GlusterFS
ReadWriteOncePodRWOP单 Pod 读写CSI 1.5+

2.5 回收策略

策略行为
RetainPVC 删除后保留 PV 和数据
DeletePVC 删除后删除 PV 和底层存储
Recycle已废弃, 使用动态供给替代

3. StorageClass

3.1 定义

apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: fast-ssd
  annotations:
    storageclass.kubernetes.io/is-default-class: "true"
provisioner: ebs.csi.aws.com
parameters:
  type: gp3
  iops: "3000"
  throughput: "125"
  encrypted: "true"
reclaimPolicy: Delete
allowVolumeExpansion: true
volumeBindingMode: WaitForFirstConsumer

3.2 关键参数

参数描述
provisionerCSI 驱动名称
parameters存储后端特定参数
reclaimPolicy回收策略
allowVolumeExpansion是否允许扩容
volumeBindingMode绑定时机

3.3 绑定模式

模式行为
ImmediatePVC 创建后立即绑定
WaitForFirstConsumer等待 Pod 调度后再绑定 (拓扑感知)

4. CSI 架构

4.1 CSI 组件

组件位置职责
Controller PluginDeployment创建/删除卷, 快照
Node PluginDaemonSet挂载/卸载卷
external-provisionerSidecar监听 PVC, 调用 CreateVolume
external-attacherSidecar监听 VolumeAttachment, 调用 ControllerPublish
external-snapshotterSidecar处理卷快照
node-driver-registrarSidecar向 Kubelet 注册 CSI 驱动

4.2 CSI RPC 接口

服务方法
IdentityGetPluginInfo, Probe, GetPluginCapabilities
ControllerCreateVolume, DeleteVolume, ControllerPublishVolume
NodeNodeStageVolume, NodePublishVolume, NodeGetInfo

5. 卷操作

5.1 挂载流程

5.2 卷扩容

# 修改 PVC
spec:
  resources:
    requests:
      storage: 20Gi  # 从 10Gi 扩容到 20Gi

扩容流程:

  1. 更新 PVC 的 storage 请求
  2. CSI 控制器扩容底层卷
  3. Kubelet 扩展文件系统 (在线或离线)

6. 卷快照 (VolumeSnapshot)

6.1 VolumeSnapshotClass

apiVersion: snapshot.storage.k8s.io/v1
kind: VolumeSnapshotClass
metadata:
  name: csi-aws-snapclass
driver: ebs.csi.aws.com
deletionPolicy: Delete
parameters:
  type: snap

6.2 创建快照

apiVersion: snapshot.storage.k8s.io/v1
kind: VolumeSnapshot
metadata:
  name: data-snapshot
spec:
  volumeSnapshotClassName: csi-aws-snapclass
  source:
    persistentVolumeClaimName: data-pvc

6.3 从快照恢复

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: restored-pvc
spec:
  dataSource:
    name: data-snapshot
    kind: VolumeSnapshot
    apiGroup: snapshot.storage.k8s.io
  accessModes:
  - ReadWriteOnce
  resources:
    requests:
      storage: 10Gi

7. 本地存储

7.1 Local PV

apiVersion: v1
kind: PersistentVolume
metadata:
  name: local-pv
spec:
  capacity:
    storage: 100Gi
  accessModes:
  - ReadWriteOnce
  persistentVolumeReclaimPolicy: Retain
  storageClassName: local-storage
  local:
    path: /mnt/disks/ssd1
  nodeAffinity:
    required:
      nodeSelectorTerms:
      - matchExpressions:
        - key: kubernetes.io/hostname
          operator: In
          values:
          - node-1

7.2 Local StorageClass

apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: local-storage
provisioner: kubernetes.io/no-provisioner
volumeBindingMode: WaitForFirstConsumer

7.3 本地 vs 网络存储

特性本地存储网络存储
性能极高 (NVMe)受网络限制
持久性节点故障即丢失跨节点复制
调度固定节点灵活迁移
适用场景高性能 DB通用应用

8. 常见 CSI 驱动

驱动存储类型特点
ebs.csi.aws.comAWS EBS块存储, gp3/io2
pd.csi.storage.gke.ioGCP PDGCE 持久磁盘
disk.csi.azure.comAzure DiskAzure 托管磁盘
rook-ceph.rbd.csi.ceph.comCeph RBD分布式块存储
nfs.csi.k8s.ioNFS网络文件系统

9. 调试与排查

# 查看 PV/PVC 状态
kubectl get pv,pvc -A

# 查看 StorageClass
kubectl get storageclass

# 查看 VolumeAttachment
kubectl get volumeattachments

# CSI 驱动 Pod 日志
kubectl logs -n kube-system -l app=ebs-csi-controller

# 挂载点排查
kubectl describe pod <name> | grep -A 10 Volumes

总结: CSI 标准化了存储的操作接口. 在选型时, 应根据业务对 IOPS, 延迟以及高可用的权重进行平衡.

On this page