IT评测·应用市场-qidao123.com

标题: Kubernetes——持久存储卷 [打印本页]

作者: 张国伟    时间: 2022-8-9 14:45
标题: Kubernetes——持久存储卷
持久存储卷

  通过前面使用持久存储卷(Persistent Volume)的示例可知,Kubernetes 用户必须要清晰了解所用到的网络存储系统的访问细节才能完成存储卷相关的配置任务,例如:NFS存储卷的 server 和 path 字段的配置就依赖于 NFS 服务器地址和共享目录路径。这与 Kubernetes 的向用户和开发隐藏底层架构的目标有所偏离,对存储资源的使用最好也能像使用计算资源一样,用户和开发人员无须了解 Pod 资源究竟运行于哪个节点,也无须了解存储系统是什么设备以及位于何处。
  为此,Kubernetes 的 Persistent Volume 子系统在用户与管理员之间添加了一个抽象层,从而使得存储系统的使用和管理职能互相解耦。

  尽管 PVC 使得用户可以以抽象的方式访问存储资源,但很多时候还是会涉及 PV 的不少属性,例如,用于不同场景时设置的性能参数等。为此,集群管理员不得不通过多种方式提供多种不同的 PV 以满足用户不同的使用需求,两者衔接上的偏差必然会导致用户的需求无法全部及时有效抵得到满足。
  Kubernetes 自 1.4 版起引入了一个新的资源对象 StorageClass,可用于将存储资源定义为具有显著性的类别(Class)而不是具体的 PV,例如 "fast" "slow" 或 "glod" "silver" "bronze"等。用户通过 PVC 直接向意向的类别发出申请,匹配由管理员事先创建的 PV,或者由其按需为用户动态创建 PV,这样做甚至免去了需要事先创建 PV 的过程。
  PV 对存储系统的支持可通过其插件来实现,目前,Kubernetes 支持如下类型的插件(https://kubernetes.io/docs/concepts/storage/persistent-volumes#reclaiming)。
 一、创建 PV

  PersistentVolume Spec 字段定义如下:
  1. [root@mh-k8s-master-247-10 ~]# kubectl explain pv
  2. KIND:     PersistentVolume
  3. VERSION:  v1
  4. DESCRIPTION:
  5.      PersistentVolume (PV) is a storage resource provisioned by an
  6.      administrator. It is analogous to a node. More info:
  7.      https://kubernetes.io/docs/concepts/storage/persistent-volumes
  8. FIELDS:
  9.    apiVersion        <string>
  10.      APIVersion defines the versioned schema of this representation of an
  11.      object. Servers should convert recognized schemas to the latest internal
  12.      value, and may reject unrecognized values. More info:
  13.      https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources
  14.    kind        <string>
  15.      Kind is a string value representing the REST resource this object
  16.      represents. Servers may infer this from the endpoint the client submits
  17.      requests to. Cannot be updated. In CamelCase. More info:
  18.      https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds
  19.    metadata        <Object>
  20.      Standard object's metadata. More info:
  21.      https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata
  22.    spec        <Object>
  23.      Spec defines a specification of a persistent volume owned by the cluster.
  24.      Provisioned by an administrator. More info:
  25.      https://kubernetes.io/docs/concepts/storage/persistent-volumes#persistent-volumes
  26.    status        <Object>
  27.      Status represents the current information/status for the persistent volume.
  28.      Populated by the system. Read-only. More info:
  29.      https://kubernetes.io/docs/concepts/storage/persistent-volumes#persistent-volumes
  30. [root@mh-k8s-master-247-10 ~]#
复制代码
  主要支持以下几个通用字段,用于定义 PV 的容量、访问模式和回收策略:
重要提醒! 
  每个卷同一时刻只能以一种访问模式挂载,即使该卷能够支持多种访问模式。例如,一个 GCEPersistentDisk 卷可以被某节点以 ReadWriteOnce 模式挂载,或者被多个节点以 ReadOnlyMany 模式挂载,但不可以同时以两种模式挂载。 
各 PV 支持的访问模式(https://kubernetes.io/docs/concepts/storage/persistent-volumes#reclaiming)
卷插件ReadWriteOnceReadOnlyManyReadWriteManyReadWriteOncePodAWSElasticBlockStore✓---AzureFile✓✓✓-AzureDisk✓---CephFS✓✓✓-Cinder✓---CSI取决于驱动取决于驱动取决于驱动取决于驱动FC✓✓--FlexVolume✓✓取决于驱动-Flocker✓---GCEPersistentDisk✓✓--Glusterfs✓✓✓-HostPath✓---iSCSI✓✓--Quobyte✓✓✓-NFS✓✓✓-RBD✓✓--VsphereVolume✓-- (Pod 运行于同一节点上时可行)-PortworxVolume✓-✓-StorageOS✓---   下面的资源清单配置示例定义了一个使用 NFS 存储后端的 PV,空间大小为 10GB,支持多路的读写操作。待后端存储系统满足需求时,即可进行如下 PV 资源的创建:
  1. kind: PersistentVolume
  2. apiVersion: v1
  3. metadata:
  4.   name: pv-nfs-0001
  5.   labels:
  6.     release: stable
  7. spec:
  8.   capacity:
  9.     storage: 5Gi
  10.   volumeModel: Filesystem
  11.   accessModes:
  12.     - ReadWrieMany
  13.   persistentVolumeReclaimPolicy: Recycle
  14.   storageClassName: slow
  15.   mountOptions:
  16.     - hard
  17.         - nfservers=4.1
  18.   nfs:
  19.     path: "/webdata/htdocs"
  20.         server: nfs.xxx.com
复制代码
  使用资源的查看命令可列出 PV 资源的相关信息。创建完成的 PV 资源可能处于下列四种状态中的某一种,它们代表着 PV 资源生命周期中的各个阶段。
二、创建 PVC

  PersistentVolumeClaim 是存储卷类型的资源,它通过申请占用某个 PersistentVolume 而创建,它与 PV 是一对一的关系,用户无须关心其底层实现细节。申请时,用户需要指定目标空间的大小、访问模式、PV标签选择器和 StorageClass 等相关信息即可。PVC 字段的可嵌套字段如下:
  1. [root@mh-k8s-master-247-10 ~]# kubectl explain pvc
  2. KIND:     PersistentVolumeClaim
  3. VERSION:  v1
  4. DESCRIPTION:
  5.      PersistentVolumeClaim is a user's request for and claim to a persistent
  6.      volume
  7. FIELDS:
  8.    apiVersion        <string>
  9.      APIVersion defines the versioned schema of this representation of an
  10.      object. Servers should convert recognized schemas to the latest internal
  11.      value, and may reject unrecognized values. More info:
  12.      https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources
  13.    kind        <string>
  14.      Kind is a string value representing the REST resource this object
  15.      represents. Servers may infer this from the endpoint the client submits
  16.      requests to. Cannot be updated. In CamelCase. More info:
  17.      https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds
  18.    metadata        <Object>
  19.      Standard object's metadata. More info:
  20.      https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata
  21.    spec        <Object>
  22.      Spec defines the desired characteristics of a volume requested by a pod
  23.      author. More info:
  24.      https://kubernetes.io/docs/concepts/storage/persistent-volumes#persistentvolumeclaims
  25.    status        <Object>
  26.      Status represents the current information/status of a persistent volume
  27.      claim. Read-only. More info:
  28.      https://kubernetes.io/docs/concepts/storage/persistent-volumes#persistentvolumeclaims
  29. [root@mh-k8s-master-247-10 ~]#
复制代码
  spec 字段定义常见如下:
  1. [root@mh-k8s-master-247-10 ~]# kubectl explain pvc.spec
  2. KIND:     PersistentVolumeClaim
  3. VERSION:  v1
  4. RESOURCE: spec <Object>
  5. DESCRIPTION:
  6.      Spec defines the desired characteristics of a volume requested by a pod
  7.      author. More info:
  8.      https://kubernetes.io/docs/concepts/storage/persistent-volumes#persistentvolumeclaims
  9.      PersistentVolumeClaimSpec describes the common attributes of storage
  10.      devices and allows a Source for provider-specific attributes
  11. FIELDS:
  12.    accessModes        <[]string>
  13.      AccessModes contains the desired access modes the volume should have. More
  14.      info:
  15.      https://kubernetes.io/docs/concepts/storage/persistent-volumes#access-modes-1
  16.    dataSource        <Object>
  17.      This field can be used to specify either: * An existing VolumeSnapshot
  18.      object (snapshot.storage.k8s.io/VolumeSnapshot - Beta) * An existing PVC
  19.      (PersistentVolumeClaim) * An existing custom resource/object that
  20.      implements data population (Alpha) In order to use VolumeSnapshot object
  21.      types, the appropriate feature gate must be enabled
  22.      (VolumeSnapshotDataSource or AnyVolumeDataSource) If the provisioner or an
  23.      external controller can support the specified data source, it will create a
  24.      new volume based on the contents of the specified data source. If the
  25.      specified data source is not supported, the volume will not be created and
  26.      the failure will be reported as an event. In the future, we plan to support
  27.      more data source types and the behavior of the provisioner may change.
  28.    resources        <Object>
  29.      Resources represents the minimum resources the volume should have. More
  30.      info:
  31.      https://kubernetes.io/docs/concepts/storage/persistent-volumes#resources
  32.    selector        <Object>
  33.      A label query over volumes to consider for binding.
  34.    storageClassName        <string>
  35.      Name of the StorageClass required by the claim. More info:
  36.      https://kubernetes.io/docs/concepts/storage/persistent-volumes#class-1
  37.    volumeMode        <string>
  38.      volumeMode defines what type of volume is required by the claim. Value of
  39.      Filesystem is implied when not included in claim spec.
  40.    volumeName        <string>
  41.      VolumeName is the binding reference to the PersistentVolume backing this
  42.      claim.
  43. [root@mh-k8s-master-247-10 ~]#
复制代码
  下面的配置清单(pvc-nfs-0001.yaml 文件)定义了一个 PVC 资源示例,其选择 PV 的挑选机制是使用了标签选择器,适配的标签是 release:stable, 存储类为 slow,这会关联到前面创建的 PV 资源 pv-nfs-0001:
  1. kind: PersistentVolumeClaim
  2. apiVersion: v1
  3. metadata:
  4.   name: pvc-nfs-0001
  5.   labels:
  6.     release: "stable"
  7. spec:
  8.   accessModes:
  9.     - ReadWrieMany
  10.   volumeMode: Filesystem
  11.   resources:
  12.     requests:
  13.           storage: 5Gi
  14.   storageClassName: slow
  15.   selector:
  16.     matchLabels:
  17.           release: "stable"
复制代码
  PV是集群级别的资源,而 PVC 是隶属于名称空间的,因此,PVC 在绑定目标 PV 时不受名称空间的限制,但 Pod 引用 PVC时,则只能是属于同一名称空间中的资源。
三、在 Pod 中使用 PVC

  在 Pod 资源中调用 PVC 资源,只需要在定义 volumes 时使用 persistentVolumeClaims 字段嵌套指定两个字段即可,具体如下:
  1. [root@mh-k8s-master-247-10 ~]# kubectl explain pod.spec.volumes.persistentVolumeClaim
  2. KIND:     Pod
  3. VERSION:  v1
  4. RESOURCE: persistentVolumeClaim <Object>
  5. DESCRIPTION:
  6.      PersistentVolumeClaimVolumeSource represents a reference to a
  7.      PersistentVolumeClaim in the same namespace. More info:
  8.      https://kubernetes.io/docs/concepts/storage/persistent-volumes#persistentvolumeclaims
  9.      PersistentVolumeClaimVolumeSource references the user's PVC in the same
  10.      namespace. This volume finds the bound PV and mounts that volume for the
  11.      pod. A PersistentVolumeClaimVolumeSource is, essentially, a wrapper around
  12.      another type of volume that is owned by someone else (the system).
  13. FIELDS:
  14.    claimName        <string> -required-
  15.      ClaimName is the name of a PersistentVolumeClaim in the same namespace as
  16.      the pod using this volume. More info:
  17.      https://kubernetes.io/docs/concepts/storage/persistent-volumes#persistentvolumeclaims
  18.    readOnly        <boolean>
  19.      Will force the ReadOnly setting in VolumeMounts. Default false.
  20. [root@mh-k8s-master-247-10 ~]#
复制代码
  举个例子:
  1. apiVersion: v1
  2. kind: Pod
  3. metadata:
  4.   name: mypod
  5. spec:
  6.   containers:
  7.     - name: myfrontend
  8.       image: nginx
  9.       volumeMounts:
  10.       - mountPath: "/var/www/html"
  11.         name: mypd
  12.   volumes:
  13.     - name: mypd
  14.       persistentVolumeClaim:
  15.         claimName: myclaim
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!




欢迎光临 IT评测·应用市场-qidao123.com (https://dis.qidao123.com/) Powered by Discuz! X3.4