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. 创建卷填充器 复制链接链接已复制到粘贴板!
创建和使用卷填充器:
- 为卷填充器创建自定义资源定义(CRD)。
- 使用卷填充程序创建预先填充的卷。
6.6.2.1. 为卷填充器创建 CRD 复制链接链接已复制到粘贴板!
以下流程解释了如何为卷填充器创建示例"hello, world"自定义资源定义(CRD)。
然后,用户可以创建此 CRD 的实例来填充持久性卷声明(PVC)。
先决条件
- 访问 OpenShift Container Platform Web 控制台。
- 使用 cluster-admin 权限访问集群。
流程
使用以下示例 YAML 文件,为填充器和相关资源的逻辑分组和操作创建命名空间:
命名空间 YAML 文件示例
apiVersion: v1 kind: Namespace metadata: name: hello使用以下 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通过创建
ServiceAccount、ClusterRole、ClusterRoleBindering和Deployment来部署控制器,以运行实施填充的逻辑:使用以下示例 YAML 文件为填充器创建服务帐户:
服务帐户 YAML 文件示例
apiVersion: v1 kind: ServiceAccount metadata: name: hello-account namespace: hello1 - 1
- 引用之前创建的命名空间。
使用以下示例 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]使用以下 YAML 文件示例创建集群角色绑定:
集群角色绑定 YAML 文件示例
kind: ClusterRoleBinding apiVersion: rbac.authorization.k8s.io/v1 metadata: name: hello-binding1 subjects: - kind: ServiceAccount name: hello-account2 namespace: hello3 roleRef: kind: ClusterRole name: hello-role4 apiGroup: rbac.authorization.k8s.io使用以下示例 YAML 文件为填充器创建 Deployment:
部署 YAML 文件示例
kind: Deployment apiVersion: apps/v1 metadata: name: hello-populator namespace: hello1 spec: selector: matchLabels: app: hello template: metadata: labels: app: hello spec: serviceAccount: hello-account2 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
创建一个卷填充器,使用以下示例 YAML 文件将
kind:Hello资源注册为卷的有效数据源:卷填充器 YAML 文件示例
kind: VolumePopulator apiVersion: populator.storage.k8s.io/v1beta1 metadata: name: hello-populator1 sourceKind: group: hello.example.com kind: Hello- 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 不附带任何卷填充器。您必须创建自己的卷填充器。
流程
运行以下命令,创建
HelloCRD 的自定义资源(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创建一个引用 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-hello2 volumeMode: Filesystem
验证
几分钟后,运行以下命令来确保 PVC 已创建并处于
Bound状态:$ oc get pvc example-pvc -n hello1 - 1
- 在本例中,PVC 的名称为
example-pvc。
输出示例
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS VOLUMEATTRIBUTESCLASS AGE example-pvc Bound my-pv 10Mi ReadWriteOnce gp3-csi <unset> 14s创建一个从 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.txt1 volumeMounts: - name: vol mountPath: /mnt restartPolicy: Never volumes: - name: vol persistentVolumeClaim: claimName: example-pvc2 运行以下命令来启动作业:
$ oc run example-job --image=busybox --command -- sleep 30 --restart=OnFailure输出示例
pod/example-job created运行以下命令,等待作业及其所有依赖项完成:
$ oc wait --for=condition=Complete pod/example-job运行以下命令,验证作业收集的内容:
$ oc logs job/example-job预期输出
Hello, world!
6.6.2.3. 卸载卷填充器 复制链接链接已复制到粘贴板!
以下流程解释了如何卸载卷填充器。
先决条件
- 访问 OpenShift Container Platform Web 控制台。
- 使用 cluster-admin 权限访问集群。
流程
要卸载卷填充器,请按照以下过程中安装的所有对象以相反的顺序删除:
- 使用卷填充器创建预先填充的卷。
为卷填充器创建 CRD 部分。
务必删除
VolumePopulator实例。