Kubernetes——存储类(StorageClass)

存储类(StorageClass)

  存储类(storage class)是 Kubernetes 资源类型的一种,它是由管理员为管理 PV 之便而按需创建的类别(逻辑组),例如可按存储系统的性能高低分类,或者根据其综合服务质量级别进行分类、依照备份策略分类,甚至直接按管理员自定义的标准进行分类等。

  Kubernetes 自身无法理解 "类别" 到底意味着什么,它仅仅是将这些当作 PV 的特性描述。

  存储类(StorageClass)的优势之一是支持 PV 的动态创建。用户用到持久性存储时,需要通过创建 PVC 来绑定匹配的 PV,此类操作需求量较大,或者当管理员手动创建的 PV 无法满足 PVC 的所有需求时,系统按 PVC 的需求标准动态创建适配的 PV 会为存储管理带来极大的灵活性。

一、StorageClass Spec

  存储类对象的名称至关重要,它是用户调用的标识。创建存储类对象时,除了名称之外,还需要为其定义三个关键字段:

[root@mh-k8s-master-247-10 ~]# kubectl explain storageclass
KIND:     StorageClass
VERSION:  storage.k8s.io/v1

DESCRIPTION:
     StorageClass describes the parameters for a class of storage for which
     PersistentVolumes can be dynamically provisioned. StorageClasses are
     non-namespaced; the name of the storage class according to etcd is in
     ObjectMeta.Name.

FIELDS:
   allowVolumeExpansion	<boolean>
     AllowVolumeExpansion shows whether the storage class allow volume expand

   allowedTopologies	<[]Object>
     Restrict the node topologies where volumes can be dynamically provisioned.
     Each volume plugin defines its own supported topology specifications. An
     empty TopologySelectorTerm list means there is no topology restriction.
     This field is only honored by servers that enable the VolumeScheduling
     feature.

   apiVersion	<string>
     APIVersion defines the versioned schema of this representation of an
     object. Servers should convert recognized schemas to the latest internal
     value, and may reject unrecognized values. More info:
     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources

   kind	<string>
     Kind is a string value representing the REST resource this object
     represents. Servers may infer this from the endpoint the client submits
     requests to. Cannot be updated. In CamelCase. More info:
     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds

   metadata	<Object>
     Standard object's metadata. More info:
     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata

   mountOptions	<[]string>
     Dynamically provisioned PersistentVolumes of this storage class are created
     with these mountOptions, e.g. ["ro", "soft"]. Not validated - mount of the
     PVs will simply fail if one is invalid.

   parameters	<map[string]string>
     Parameters holds the parameters for the provisioner that should create
     volumes of this storage class.

   provisioner	<string> -required-
     Provisioner indicates the type of the provisioner.

   reclaimPolicy	<string>
     Dynamically provisioned PersistentVolumes of this storage class are created
     with this reclaimPolicy. Defaults to Delete.

   volumeBindingMode	<string>
     VolumeBindingMode indicates how PersistentVolumeClaims should be
     provisioned and bound. When unset, VolumeBindingImmediate is used. This
     field is only honored by servers that enable the VolumeScheduling feature.

[root@mh-k8s-master-247-10 ~]# 
  • reclaimPolicy <string>: 为当前存储类动态创建的 PV 指定回收策略,可用值为 Delete(默认)和 Retain;不过,那些由管理员手工创建的 PV 的回收策略取决于它们自身的定义。
  • volumeBindingMode <string>: 定义如何为 PVC 完成供给和绑定,默认值为 "VolumeBindingImmediate";此选项仅在启用了存储卷调度功能时才能生效。
  • mountOptions <[]string>:由当前类动态创建的 PV 的挂载选项列表。使用这些安装选项,例如[“ro”,“soft”]。未验证的, 挂载 PV卷 将会无效。

  下面是一个定义在 glusterfs-storageclass.yaml 配置文件的资源清单,它定义了一个使用 Gluster 存储系统的存储类 glusterfs,并通过 annotations 字段将其定义为默认的存储类:

apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: gluster-vol-default
provisioner: kubernetes.io/glusterfs
parameters:
  resturl: "http://192.168.10.100:8080"
  restuser: ""
  secretNamespace: ""
  secretName: ""
allowVolumeExpansion: true

二、动态 PV 供给

  动态 PV 供给的启用,需要事先由管理员创建至少一个存储类,不同的 Provisoner 的创建方法各有不同,另外,并非所有的存储卷插件都由 Kubernetes 内建支持 PV 动态供给功能。

  下面的资源清单定义在 pvc-glusterfs-dynamic-0001.yaml 配置文件中,它将从 glusterfs 存储类中申请适应 5GB 的存储空间。

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pvc-gluser-dynamic-0001
  annotations:
    volume.beta.kubernetes.io/storage-class: glusterfs
spec:
  # storageClassName: "glusterfs"
  accessModes:
  - ReadWriteOnce
  resources:
    requests:
	  storage: 5Gi

  目前,在 PVC 的定义中指定使用的存储类资源的方式共有两种:一种是使用 spec.storageClassName 字段,另一种是使用 "volume.beta.kubernetes.io/storage-class" 注解信息。不过,建议仅使用一种方式,以免两者设置为不同的值时会出现配置错误。接下来创建定义的 PVC,并检查其绑定状态:

  通过如下命令输出的 PVC 资源的描述信息可以看到 PVC 存储卷创建和绑定情况,绑定的 PV 资源由 persistentvolume-controller 控制器动态提供。

kubectl  describe pvc pvc-gluster-dynamic-0001

  任何支持 PV 动态供给的存储系统都可以在定义为存储类后由 PVC 动态申请使用,这对于难以事先预估使用到的存储空间大小及存储卷数量的使用场景尤为有用,例如由 StatefuleSet 控制器管理 Pod 对象时,存储卷是必备资源,且随着规模的变动,存储卷的数量也会随着变动。

posted @ 2022-06-24 17:35  左扬  阅读(914)  评论(0编辑  收藏  举报
levels of contents