CSI

https://github.com/container-storage-interface/spec


卷Volume

和docker中的一样。

volume支持的卷类型有: awsEBS, azureDisk, azureFile, gcePD, secret, configMap, emptyDir, hostPath, local, nfs等.

apiVersion: v1
kind: Pod
metadata:
  name: test-ebs
spec:
  containers:
  - image: k8s.gcr.io/test-webserver
    name: test-container
    volumeMounts:
    - mountPath: /test-ebs
      name: test-volume
  volumes:
  - name: test-volume
    # 此 AWS EBS 卷必须已经存在
    awsElasticBlockStore:
      volumeID: "<volume-id>"
      fsType: ext4

AWS的EBS和EFS需要安装驱动:

https://github.com/kubernetes-sigs/aws-ebs-csi-driver https://github.com/kubernetes-sigs/aws-efs-csi-driver


存储类StorageClass

storageclass没有namespace.

每个存储类包含provisioner, parameters和reclaimPolicy.

内置provisioner的卷插件:

  • awsEBS
  • azureFile
  • azureDisk
  • gcePD
  • openstack cinder

没有provisioner的卷类型可以使用外部插件或者自己开发.

https://github.com/kubernetes-sigs/sig-storage-lib-external-provisioner

awsEBS:

kind: StorageClass
apiVersion: storage.k8s.io/v1
metadata:
  name: sourcegraph
labels:
  deploy: sourcegraph
# provisioner: ebs.csi.aws.com
provisioner: kubernetes.io/aws-ebs
parameters:
  type: gp2 # This configures SSDs (default).
  fsType: ext4 # (default)
reclaimPolicy: Retain
volumeBindingMode: WaitForFirstConsumer
allowVolumeExpansion: true

nfs:

kubernetes不包含nfs驱动,需要使用外部驱动创建nfs存储类.

apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: example-nfs
provisioner: example.com/external-nfs
parameters:
  server: nfs-server.example.com
  path: /share
  readOnly: false

local:

本地卷不支持动态制备.

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

持久卷PV

pv没有namespace, 用来指定具体的存储资源。有静态和动态两种方式,最终需要绑定到pvc上。

pv的回收策略ReclaimPolicy:

  • Retained保留
  • Deleted删除

pv的卷绑定模式volumeBindingMode:

  • WaitForFirstConsumer
  • Immediate

卷模式volumeMode:

  • Filesystem(默认)
  • Block

访问模式accessMode:

  • RWO: ReadWriteOnce
  • ROX: ReadOnlyMany
  • RWX: ReadWriteMany
  • RWOP: ReadWriteOncePod

卷的阶段:

  • Avaliable
  • Bound
  • Released
  • Failed

static volume provisioning

静态pvc和pv的绑定通过storageClassName, accessMode和capacity来判断。

pv中的capacity必须大于等于pvc。

apiVersion: v1
kind: PersistentVolume
metadata:
  name: nas-csi-pv
  labels:
    app: demo
spec:
  storageClassName: 
  persistentVolumeReclaimPolicy: Retained/Recycled
  accessModes:
  - ReadWriteMany
  capacity:
    storage: 5Gi
  hostPath:
    path: "/home/path"
  csi:
    driver: ...

dynamic volume provisioning

动态pv需要storageclass, 由StorageClass动态的创建PV, 不需要手动创建pv,只需要在pvc中指定storageclass即可.

kind: StorageClass
apiVersion: storage.k8s.io/v1
metadata:
  name: sourcegraph
labels:
  deploy: sourcegraph
provisioner: kubernetes.io/aws-ebs
parameters:
  type: gp2 
reclaimPolicy: Retain
volumeBindingMode: WaitForFirstConsumer
allowVolumeExpansion: true

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pgsql
  labels:
    deploy: sourcegraph
    sourcegraph-resource-requires: no-cluster-admin
    app.kubernetes.io/component: pgsql
spec:
  // 通过storageClassName自动给pvc创建pv
  storageClassName: sourcegraph
  accessModes:
  - ReadWriteOnce
  resources:
    requests:
    storage: 200Gi

PVC

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: nsa-pvc
  namespace: test
labels:
  app: demo
spec:
  storageClassName: 
  accessModes:
  - ReadWriteMany
  resources:
    requests:
      storage: 5Gi 
  // 通过selector让PVC使用指定的PV。
  selector:
    app: demo

VolumeSnapshotClass

VolumeSnapshot

VS是对资源的请求.

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

VolumeSnapshotContent

VSC实际中资源管理.

apiVersion: snapshot.storage.k8s.io/v1
kind: VolumeSnapshotContent
metadata:
  name: snapcontent-72d9a349-aacd-42d2-a240-d775650d2455
spec:
  deletionPolicy: Delete
  driver: hostpath.csi.k8s.io
  source:
    volumeHandle: ee0cfb94-f8d4-11e9-b2d8-0242ac110002
  volumeSnapshotClassName: csi-hostpath-snapclass
  volumeSnapshotRef:
    name: new-snapshot-test
    namespace: default
    uid: 72d9a349-aacd-42d2-a240-d775650d2455