6.6. 卷填充器


卷 Populators 在动态置备过程中启用将数据自动预载入到卷中,而不是置备空卷。

6.6.1. 卷填充器概述

在 OpenShift Container Platform 版本 4.12 到 4.19 中,持久性卷声明(PVC) spec 中的 dataSource 字段提供卷填充器功能。但是,它只使用 PVC 和快照作为填充卷的数据源。

从 OpenShift Container Platform 版本 4.20 开始,使用 dataSourceRef 字段。使用 dataSourceRef 字段,您可以使用任何适当的自定义资源(CR)作为数据源来预填充新卷。

注意

使用 dataSource 字段的卷填充功能可能会在以后的版本中被弃用。如果您使用此字段创建了任何卷填充器,请考虑重新创建卷填充器以使用 dataSourceRef 字段以避免将来的问题。

卷填充功能会被默认启用,OpenShift Container Platform 包含已安装的 volume-data-source-validator 控制器。但是,OpenShift Container Platform 不附带任何卷填充器。

6.6.2. 创建卷填充器

创建和使用卷填充器:

  1. 为卷填充器创建自定义资源定义(CRD)。
  2. 使用卷填充程序创建预先填充的卷。

6.6.2.1. 为卷填充器创建 CRD

以下流程解释了如何为卷填充器创建示例"hello, world"自定义资源定义(CRD)。

然后,用户可以创建此 CRD 的实例来填充持久性卷声明(PVC)。

先决条件

  • 访问 OpenShift Container Platform Web 控制台。
  • 使用 cluster-admin 权限访问集群。

流程

  1. 使用以下示例 YAML 文件,为填充器和相关资源的逻辑分组和操作创建命名空间:

    命名空间 YAML 文件示例

    apiVersion: v1
    kind: Namespace
    metadata:
      name: hello
    Copy to Clipboard Toggle word wrap

  2. 使用以下 YAML 文件示例为数据源创建 CRD:

    CRD YAML 文件示例

    apiVersion: apiextensions.k8s.io/v1
    kind: CustomResourceDefinition
    metadata:
      name: hellos.hello.example.com
    spec:
      group: hello.example.com
      names:
        kind: Hello
        listKind: HelloList
        plural: hellos
        singular: hello
      scope: Namespaced
      versions:
      - name: v1alpha1
        schema:
          openAPIV3Schema:
            description: Hello is a specification for a Hello resource
            properties:
              apiVersion:
                description: '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'
                type: string
              kind:
                description: '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'
                type: string
              spec:
                description: HelloSpec is the spec for a Hello resource
                properties:
                  fileContents:
                    type: string
                  fileName:
                    type: string
                required:
                - fileContents
                - fileName
                type: object
            required:
            - spec
            type: object
        served: true
        storage: true
    Copy to Clipboard Toggle word wrap

  3. 通过创建 ServiceAccountClusterRoleClusterRoleBinderingDeployment 来部署控制器,以运行实施填充的逻辑:

    1. 使用以下示例 YAML 文件为填充器创建服务帐户:

      服务帐户 YAML 文件示例

      apiVersion: v1
      kind: ServiceAccount
      metadata:
        name: hello-account
        namespace: hello 
      1
      Copy to Clipboard Toggle word wrap

      1
      引用之前创建的命名空间。
    2. 使用以下示例 YAML 文件为填充器创建集群角色:

      集群角色 YAML 文件示例

      kind: ClusterRole
      apiVersion: rbac.authorization.k8s.io/v1
      metadata:
        name: hello-role
      rules:
        - apiGroups: [hello.example.com]
          resources: [hellos]
          verbs: [get, list, watch]
      Copy to Clipboard Toggle word wrap

    3. 使用以下 YAML 文件示例创建集群角色绑定:

      集群角色绑定 YAML 文件示例

      kind: ClusterRoleBinding
      apiVersion: rbac.authorization.k8s.io/v1
      metadata:
        name: hello-binding 
      1
      
      subjects:
        - kind: ServiceAccount
          name: hello-account 
      2
      
          namespace: hello 
      3
      
      roleRef:
        kind: ClusterRole
        name: hello-role 
      4
      
        apiGroup: rbac.authorization.k8s.io
      Copy to Clipboard Toggle word wrap

      1
      角色绑定名称。
      2
      引用您之前创建的服务帐户的名称。
      3
      引用您之前创建的服务帐户的命名空间名称。
      4
      引用您之前创建的集群角色。
    4. 使用以下示例 YAML 文件为填充器创建 Deployment:

      部署 YAML 文件示例

      kind: Deployment
      apiVersion: apps/v1
      metadata:
        name: hello-populator
        namespace: hello 
      1
      
      spec:
        selector:
          matchLabels:
            app: hello
        template:
          metadata:
            labels:
              app: hello
          spec:
            serviceAccount: hello-account 
      2
      
            containers:
              - name: hello
                image: registry.k8s.io/sig-storage/hello-populator:v1.0.1
                imagePullPolicy: IfNotPresent
                args:
                  - --mode=controller
                  - --image-name=registry.k8s.io/sig-storage/hello-populator:v1.0.1
                  - --http-endpoint=:8080
                ports:
                  - containerPort: 8080
                    name: http-endpoint
                    protocol: TCP
      Copy to Clipboard Toggle word wrap

      1
      引用之前创建的命名空间。
      2
      引用您之前创建的服务帐户。
  4. 创建一个卷填充器,使用以下示例 YAML 文件将 kind:Hello 资源注册为卷的有效数据源:

    卷填充器 YAML 文件示例

    kind: VolumePopulator
    apiVersion: populator.storage.k8s.io/v1beta1
    metadata:
      name: hello-populator 
    1
    
    sourceKind:
      group: hello.example.com
      kind: Hello
    Copy to Clipboard Toggle word wrap

    1
    卷填充器名称。

    使用未注册填充器的 PVC 生成事件:"此 PVC 的数据源与任何注册的 VolumePopulator" 不匹配,表示 PVC 可能没有被置备,因为您使用未知(未注册)填充器。

后续步骤

  • 现在,您可以创建此 CRD 的 CR 实例来填充 PVC。

有关如何使用卷填充 器预填充卷的详情,请参考使用卷填充器创建预策略卷

6.6.2.2. 使用卷填充器创建预先填充的卷

以下流程解释了如何使用前面创建的 hellos.hello.example.com 自定义资源定义(CRD)创建预先填充的持久性卷声明(PVC)。

在本例中,您要创建一个名为 "example.txt" 的文件,该文件在卷的根目录中包含字符串 "Hello, world!"。对于真实实施,您需要创建自己的卷填充器。

先决条件

  • 登陆到一个正在运行的 OpenShift Container Platform 集群。
  • 卷填充器有一个现有的自定义资源定义(CRD)。
  • OpenShift Container Platform 不附带任何卷填充器。您必须 创建自己的卷填充器。

流程

  1. 运行以下命令,创建 Hello CRD 的自定义资源(CR)实例,其文本 "Hello, World!" 传递为 fileContents 参数:

    $ oc apply -f  - <<EOF
    apiVersion: hello.example.com/v1alpha1
    kind: Hello
    metadata:
      name: example-hello
    spec:
      fileName: example.txt
      fileContents: Hello, world!
    EOF
    Copy to Clipboard Toggle word wrap
  2. 创建一个引用 Hello CR 的 PVC,类似于以下示例文件:

    PVC YAML 文件示例

    apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
      name: example-pvc
    spec:
      accessModes:
      - ReadWriteOnce
      resources:
        requests:
          storage: 10Mi
      dataSourceRef: 
    1
    
        apiGroup: hello.example.com
        kind: Hello
        name: example-hello 
    2
    
      volumeMode: Filesystem
    Copy to Clipboard Toggle word wrap

    1
    dataSourceRef 字段指定 PVC 的数据源。
    2
    您用作数据源的 CR 的名称。在本例中,'example-hello'。

验证

  1. 几分钟后,运行以下命令来确保 PVC 已创建并处于 Bound 状态:

    $ oc get pvc example-pvc -n hello 
    1
    Copy to Clipboard Toggle word wrap
    1
    在本例中,PVC 的名称为 example-pvc

    输出示例

    NAME          STATUS    VOLUME        CAPACITY   ACCESS MODES   STORAGECLASS   VOLUMEATTRIBUTESCLASS   AGE
    example-pvc   Bound     my-pv         10Mi       ReadWriteOnce  gp3-csi        <unset>                 14s
    Copy to Clipboard Toggle word wrap

  2. 创建一个从 PVC 读取的作业,以使用以下示例文件验证数据源信息是否已应用:

    作业 YAML 文件示例

    apiVersion: batch/v1
    kind: Job
    metadata:
      name: example-job
    spec:
      template:
        spec:
          containers:
            - name: example-container
              image: busybox:latest
              command:
                - cat
                - /mnt/example.txt 
    1
    
              volumeMounts:
                - name: vol
                  mountPath: /mnt
          restartPolicy: Never
          volumes:
            - name: vol
              persistentVolumeClaim:
                claimName: example-pvc 
    2
    Copy to Clipboard Toggle word wrap

    1
    带有 "Hello, world!" 文本的文件的位置和名称。
    2
    您在第 2 步中创建的 PVC 名称。在本例中,example-pvc
  3. 运行以下命令来启动作业:

    $ oc run example-job --image=busybox --command -- sleep 30 --restart=OnFailure
    Copy to Clipboard Toggle word wrap

    输出示例

    pod/example-job created
    Copy to Clipboard Toggle word wrap

  4. 运行以下命令,等待作业及其所有依赖项完成:

    $ oc wait --for=condition=Complete pod/example-job
    Copy to Clipboard Toggle word wrap
  5. 运行以下命令,验证作业收集的内容:

    $ oc logs job/example-job
    Copy to Clipboard Toggle word wrap

    预期输出

    Hello, world!
    Copy to Clipboard Toggle word wrap

6.6.2.3. 卸载卷填充器

以下流程解释了如何卸载卷填充器。

先决条件

  • 访问 OpenShift Container Platform Web 控制台。
  • 使用 cluster-admin 权限访问集群。

流程

要卸载卷填充器,请按照以下过程中安装的所有对象以相反的顺序删除:

  1. 使用卷 填充器 创建预填充卷.
  2. 为卷填充器创建 CRD 部分.

    务必删除 VolumePopulator 实例。

返回顶部
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。 了解我们当前的更新.

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

Theme

© 2025 Red Hat