使用共享资源
使用 Shared Resources CSI Driver Operator
摘要
第 1 章 共享资源 CSI Driver Operator 复制链接链接已复制到粘贴板!
传统上,存储供应商已将存储驱动程序作为 Kubernetes 的一部分提供。随着容器存储接口(CSI)的实现,第三方供应商可以使用标准接口来提供存储插件,而无需更改核心 Kubernetes 代码。CSI Operators 为构建的用户提供存储选项,如卷快照,它们无法通过 in-tree 卷插件实现。
Shared Resource CSI 驱动程序是一个特殊的 CSI 驱动程序,它允许集群管理员使用 csi 卷类型在命名空间间安全地共享 Secret 和 ConfigMap 对象。共享资源 CSI 驱动程序置备内联临时卷,启用 pod 和 OpenShift Container Platform 构建使用这些共享资源。
共享资源 CSI 驱动程序支持以下自定义资源类型:
-
用于共享
Secret对象的SharedSecret自定义资源 -
共享
ConfigMap对象的SharedConfigMap自定义资源
1.1. 在命名空间间共享 Secret 对象 复制链接链接已复制到粘贴板!
Secret 对象用于存储和管理敏感信息,如密码、OAuth 令牌和 SSH 密钥。您可以使用 SharedSecret 自定义资源(CR)在集群中的不同命名空间中安全地共享 Secret 对象,而无需手动复制它。共享 Secret 对象为敏感数据提供单个源,并确保授权的命名空间有权访问它。
1.1.1. 创建 SharedSecret 自定义资源 复制链接链接已复制到粘贴板!
您可以创建一个 SharedSecret 自定义资源(CR)在命名空间间共享 Secret 对象。
先决条件
-
您已创建了要在其他命名空间中共享的
Secret对象。要创建Secret对象,请参阅"创建 Secret"。 - 已订阅集群,并通过 Insights Operator 同步授权密钥。
您必须具有执行以下操作的权限:
-
在集群范围的级别上创建
sharedsecrets.sharedresource.openshift.ioCR。 -
为
SharedSecretCR 创建ClusterRole对象。 -
为 Shared Resource CSI 驱动程序创建
Role和RoleBinding对象。 - 管理集群中命名空间中的角色和角色绑定,以控制用户。
-
管理角色和角色绑定,以便 pod 指定的服务帐户可以使用
SharedSecretCR 挂载引用Secret对象的 CSI 卷。 -
访问包含您要共享的
Secret对象的命名空间。
-
在集群范围的级别上创建
流程
创建
Role和RoleBinding对象,为 Shared Resource CSI 驱动程序授予使用以下示例配置访问SharedSecret对象的权限:Role对象示例apiVersion: rbac.authorization.k8s.io/v1 kind: Role metadata: name: share-etc-pki-entitlement namespace: openshift-config-managed rules: - apiGroups: - "" resources: - secrets resourceNames: - etc-pki-entitlement verbs: - get - list - watchapiVersion: rbac.authorization.k8s.io/v1 kind: Role metadata: name: share-etc-pki-entitlement1 namespace: openshift-config-managed rules: - apiGroups: - "" resources: - secrets resourceNames: - etc-pki-entitlement verbs: - get - list - watchCopy to Clipboard Copied! Toggle word wrap Toggle overflow - 1
RoleCR 的名称。
RoleBinding对象示例apiVersion: rbac.authorization.k8s.io/v1 kind: RoleBinding metadata: name: share-etc-pki-entitlement namespace: openshift-config-managed roleRef: apiGroup: rbac.authorization.k8s.io kind: Role name: share-etc-pki-entitlement subjects: - kind: ServiceAccount name: csi-driver-shared-resource namespace: openshift-buildsapiVersion: rbac.authorization.k8s.io/v1 kind: RoleBinding metadata: name: share-etc-pki-entitlement1 namespace: openshift-config-managed roleRef: apiGroup: rbac.authorization.k8s.io kind: Role name: share-etc-pki-entitlement2 subjects: - kind: ServiceAccount name: csi-driver-shared-resource3 namespace: openshift-buildsCopy to Clipboard Copied! Toggle word wrap Toggle overflow 使用以下示例配置创建
SharedSecretCR:apiVersion: sharedresource.openshift.io/v1alpha1 kind: SharedSecret metadata: name: shared-test-secret spec: secretRef: name: test-secret namespace: <name_of_the_source_namespace>apiVersion: sharedresource.openshift.io/v1alpha1 kind: SharedSecret metadata: name: shared-test-secret1 spec: secretRef: name: test-secret namespace: <name_of_the_source_namespace>Copy to Clipboard Copied! Toggle word wrap Toggle overflow - 1
- 将
<name_of_the_source_namespace> 替换为SharedSecretCR 的名称。
使用以下示例配置,创建一个
ClusterRole对象来授予 RBAC 权限以使用引用的共享资源:apiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRole metadata: name: use-shared-test-secret rules: - apiGroups: - sharedresource.openshift.io resources: - sharedsecrets resourceNames: - shared-test-secret verbs: - useapiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRole metadata: name: use-shared-test-secret1 rules: - apiGroups: - sharedresource.openshift.io resources: - sharedsecrets resourceNames: - shared-test-secret verbs: - useCopy to Clipboard Copied! Toggle word wrap Toggle overflow - 1
ClusterRoleCR 的名称。
1.1.2. 在 pod 中使用 SharedSecret 自定义资源 复制链接链接已复制到粘贴板!
要从 pod 访问 SharedSecret 自定义资源(CR),您可以为服务帐户授予基于角色的访问控制(RBAC)权限。
先决条件
-
您已为您要在集群中的命名空间之间共享的
Secret对象创建一个SharedSecretCR 实例。 您必须具有执行以下操作的权限:
-
运行
oc adm policy who-can use <sharedsecret_identifier> 命令检查服务帐户是否可以使用SharedSecretCR,并且服务帐户列在命名空间中。 -
确认 pod 的服务帐户可以使用
csi卷。如果以用户身份创建 pod,请确认您可以使用csi卷。
-
运行
如果您无法完成最后的两个先决条件,集群管理员可以建立必要的 RBAC 权限,以便您可以启用服务帐户使用 SharedSecret CR。
流程
在与角色关联的集群中创建
RoleBinding对象,并为服务帐户授予使用共享资源的权限。请参见以下示例配置:apiVersion: rbac.authorization.k8s.io/v1 kind: RoleBinding metadata: name: use-shared-secret namespace: app-namespace roleRef: apiGroup: rbac.authorization.k8s.io kind: ClusterRole name: use-shared-secret subjects: - kind: ServiceAccount name: <service_account_name>apiVersion: rbac.authorization.k8s.io/v1 kind: RoleBinding metadata: name: use-shared-secret1 namespace: app-namespace roleRef: apiGroup: rbac.authorization.k8s.io kind: ClusterRole name: use-shared-secret subjects: - kind: ServiceAccount name: <service_account_name>2 Copy to Clipboard Copied! Toggle word wrap Toggle overflow 将共享资源
csi驱动程序挂载到 pod 或接受csi卷的任何其他资源中。请参见以下示例配置:apiVersion: v1 kind: Pod metadata: name: example-shared-secret namespace: <app_namespace> spec: ... serviceAccountName: default volumes: - name: shared-secret csi: readOnly: true driver: csi.sharedresource.openshift.io volumeAttributes: sharedSecret: shared-test-secretapiVersion: v1 kind: Pod metadata: name: example-shared-secret namespace: <app_namespace>1 spec: ... serviceAccountName: default volumes: - name: shared-secret csi: readOnly: true driver: csi.sharedresource.openshift.io volumeAttributes: sharedSecret: shared-test-secret2 Copy to Clipboard Copied! Toggle word wrap Toggle overflow 重要如果
sharedSecret属性的值与SharedSecret实例的名称不匹配,则 pod 无法启动。
1.2. 在命名空间间共享 ConfigMap 对象 复制链接链接已复制到粘贴板!
ConfigMap 对象存储非敏感配置数据,如应用程序 URL 或功能标记。SharedConfigMap 自定义资源(CR)可让您在没有手动重复的情况下,在集群中的多个命名空间中安全地共享 ConfigMap 对象。通过共享 ConfigMap 对象,您可以建立单个配置数据源并确保集群的一致性。
1.2.1. 创建 SharedConfigMap 自定义资源 复制链接链接已复制到粘贴板!
您可以创建 SharedConfigMap 自定义资源(CR)实例,以在命名空间间共享 ConfigMap 对象。
先决条件
-
您已创建了要在其他命名空间中共享的
ConfigMap对象。要创建ConfigMap对象,请参阅"添加输入 secret 和 configmaps"。 您必须具有执行以下操作的权限:
-
在集群范围的级别上创建
sharedconfigmaps.sharedresource.openshift.ioCR。 -
为
SharedConfigMapCR 创建ClusterRole对象。 -
为 Shared Resource Container Storage Interface (CSI)驱动程序创建
Role和RoleBinding对象。 - 管理集群中命名空间中的角色和角色绑定,以控制哪些用户可以获取、列出和监视实例。
-
管理角色和角色绑定,以便 pod 指定的服务帐户可以使用
SharedConfigMapCR 挂载引用ConfigMap对象的csi卷。 -
访问包含您要共享的
ConfigMap对象的命名空间。
-
在集群范围的级别上创建
流程
创建
Role和RoleBinding对象,为 Shared Resource CSI Driver 授予访问ConfigMap对象的权限。请参见以下示例配置:Role对象示例apiVersion: rbac.authorization.k8s.io/v1 kind: Role metadata: name: shared-test-config namespace: <name_of_the_source_namespace> rules: - apiGroups: [""] resources: ["configmaps"] resourceNames: ["shared-config"] verbs: ["get", "list", "watch"]apiVersion: rbac.authorization.k8s.io/v1 kind: Role metadata: name: shared-test-config namespace: <name_of_the_source_namespace>1 rules: - apiGroups: [""] resources: ["configmaps"] resourceNames: ["shared-config"] verbs: ["get", "list", "watch"]Copy to Clipboard Copied! Toggle word wrap Toggle overflow - 1
- 将
<name_of_the_source_namespace> 替换为源命名空间的名称。
RoleBinding对象示例apiVersion: rbac.authorization.k8s.io/v1 kind: RoleBinding metadata: name: shared-test-config namespace: <name_of_the_source_namespace> roleRef: apiGroup: rbac.authorization.k8s.io kind: Role name: shared-test-config subjects: - kind: ServiceAccount name: csi-driver-shared-resource namespace: openshift-buildsapiVersion: rbac.authorization.k8s.io/v1 kind: RoleBinding metadata: name: shared-test-config namespace: <name_of_the_source_namespace>1 roleRef: apiGroup: rbac.authorization.k8s.io kind: Role name: shared-test-config subjects:2 - kind: ServiceAccount name: csi-driver-shared-resource namespace: openshift-buildsCopy to Clipboard Copied! Toggle word wrap Toggle overflow 为您要在集群中的命名空间之间共享的
ConfigMap对象创建SharedConfigMapCR。以下示例显示了示例配置:apiVersion: sharedresource.openshift.io/v1alpha1 kind: SharedConfigMap metadata: name: share-test-config spec: configMapRef: name: shared-config namespace: <name_of_the_source_namespace>apiVersion: sharedresource.openshift.io/v1alpha1 kind: SharedConfigMap metadata: name: share-test-config1 spec: configMapRef: name: shared-config namespace: <name_of_the_source_namespace>2 Copy to Clipboard Copied! Toggle word wrap Toggle overflow 创建一个
ClusterRoleCR 实例,其授予基于角色的访问控制(RBAC)权限,以使用引用的共享资源。以下示例显示了示例配置:apiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRole metadata: name: <cluster_role_name> rules: - apiGroups: - sharedresource.openshift.io resources: - sharedconfigmaps resourceNames: - share-test-config verbs: - useapiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRole metadata: name: <cluster_role_name>1 rules: - apiGroups: - sharedresource.openshift.io resources: - sharedconfigmaps resourceNames: - share-test-config2 verbs: - useCopy to Clipboard Copied! Toggle word wrap Toggle overflow
1.2.2. 在 pod 中使用 SharedConfigMap 自定义资源 复制链接链接已复制到粘贴板!
要从 pod 访问 SharedConfigMap 自定义资源(CR),您必须为关联的服务帐户授予所需的基于角色的访问控制(RBAC)权限,以使用该 SharedConfigMap CR。
先决条件
-
为您要在集群中的命名空间之间共享的配置映射创建
SharedConfigMapCR 实例。 您必须具有执行以下操作的权限:
-
运行
oc get sharedconfigmaps命令,以获取SharedConfigMapCR 实例的列表。 -
运行
oc adm policy who-can use <sharedsecret_identifier> 命令检查服务帐户是否可以使用SharedSecretCR,并且服务帐户列在命名空间中。 -
运行
oc describe clusterrole <roleName> 命令,以确认 pod 的服务帐户可以使用csi卷。如果以用户身份创建 pod,请确认您可以使用csi卷。
-
运行
如果您无法完成最后两个先决条件,集群管理员可以授予 RBAC 权限,使服务帐户能够使用 SharedConfigMap CR。
流程
创建与角色关联的
RoleBinding对象,并为服务帐户授予使用共享资源的权限。apiVersion: rbac.authorization.k8s.io/v1 kind: RoleBinding metadata: name: use-shared-config namespace: <app_namespace> roleRef: apiGroup: rbac.authorization.k8s.io kind: ClusterRole name: use-shared-config subjects: - kind: ServiceAccount name: <service_account_name>apiVersion: rbac.authorization.k8s.io/v1 kind: RoleBinding metadata: name: use-shared-config namespace: <app_namespace>1 roleRef: apiGroup: rbac.authorization.k8s.io kind: ClusterRole name: use-shared-config subjects: - kind: ServiceAccount name: <service_account_name>2 Copy to Clipboard Copied! Toggle word wrap Toggle overflow 将 Shared Resource Container Storage Interface (CSI)驱动程序挂载到 pod 或接受
csi卷的任何其他资源中。apiVersion: v1 kind: Pod metadata: name: example-shared-config namespace: <app_namespace> spec: ... serviceAccountName: default volumes: - name: shared-config csi: readOnly: true driver: csi.sharedresource.openshift.io volumeAttributes: sharedConfigMap: share-test-configapiVersion: v1 kind: Pod metadata: name: example-shared-config namespace: <app_namespace>1 spec: ... serviceAccountName: default volumes: - name: shared-config csi: readOnly: true driver: csi.sharedresource.openshift.io volumeAttributes: sharedConfigMap: share-test-config2 Copy to Clipboard Copied! Toggle word wrap Toggle overflow 重要如果
sharedConfigMap属性的值与sharedConfigMap实例的名称不匹配,则 pod 无法启动。
1.3. 使用 RHEL 权利构建镜像 复制链接链接已复制到粘贴板!
如果您有 Red Hat Enterprise Linux (RHEL)授权,Insights Operator 会使用简单内容访问(SCA)功能自动管理权利密钥。使用 SCA 时,RHEL 系统将访问订阅内容,而无需手动管理授权密钥。Insights Operator 导入 SCA 授权密钥,并将其存储在 openshift-config-managed 命名空间中的名为 etc-pki-entitlement 的 secret 中。
在以前的版本中,集群管理员会将 etc-pki-entitlement secret 手动复制到所需的命名空间中。从 OpenShift Container Platform 4.10 及之后的版本开始,Red Hat OpenShift 的构建可以使用 Shared Resource Container Storage Interface (CSI) Driver Operator 从 openshift-config-managed 命名空间中共享 etc-pki-entitlement secret。
1.3.1. 在命名空间间共享 RHEL 权利 复制链接链接已复制到粘贴板!
您可以使用 SharedSecret 对象安全地共享并同步跨命名空间中的集群的 RHEL 授权密钥。
先决条件
您必须具有执行以下操作的权限:
-
创建
SharedSecret对象。 - 创建构建配置和启动构建。
-
运行
oc get sharedsecrets命令,以发现哪些SharedSecret自定义资源(CR)实例可用。 -
运行
oc adm policy who-can use <sharedsecret_identifier> 命令检查服务帐户是否可以使用SharedSecretCR,并且服务帐户列在命名空间中。
-
创建
如果您无法完成最后的两个先决条件,集群管理员可以建立必要的 RBAC 权限,以便您可以授予服务帐户以使用 SharedSecret CR。
流程
运行以下命令,使用集群的授权 secret 创建
SharedSecret对象实例:oc apply -f -<<EOF apiVersion: sharedresource.openshift.io/v1alpha1 kind: SharedSecret metadata: name: etc-pki-entitlement spec: secretRef: name: etc-pki-entitlement namespace: openshift-config-managed EOF$ oc apply -f -<<EOF apiVersion: sharedresource.openshift.io/v1alpha1 kind: SharedSecret metadata: name: etc-pki-entitlement spec: secretRef: name: etc-pki-entitlement namespace: openshift-config-managed EOFCopy to Clipboard Copied! Toggle word wrap Toggle overflow 创建
ClusterRole对象,以授予使用以下示例配置访问SharedSecret对象的权限:apiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRole metadata: name: use-share-etc-pki-entitlement rules: - apiGroups: - sharedresource.openshift.io resources: - sharedsecrets resourceNames: - etc-pki-entitlement verbs: - useapiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRole metadata: name: use-share-etc-pki-entitlement1 rules: - apiGroups: - sharedresource.openshift.io resources: - sharedsecrets resourceNames: - etc-pki-entitlement verbs: - useCopy to Clipboard Copied! Toggle word wrap Toggle overflow - 1
ClusterRoleCR 的名称。
创建
Role和RoleBinding对象,为 Shared Resource CSI 驱动程序授予访问SharedSecret对象的权限:Role对象示例apiVersion: rbac.authorization.k8s.io/v1 kind: Role metadata: name: share-etc-pki-entitlement namespace: openshift-config-managed rules: - apiGroups: - "" resources: - secrets resourceNames: - etc-pki-entitlement verbs: - get - list - watchapiVersion: rbac.authorization.k8s.io/v1 kind: Role metadata: name: share-etc-pki-entitlement1 namespace: openshift-config-managed rules: - apiGroups: - "" resources: - secrets resourceNames: - etc-pki-entitlement verbs: - get - list - watchCopy to Clipboard Copied! Toggle word wrap Toggle overflow - 1
RoleCR 的名称。
RoleBinding对象示例apiVersion: rbac.authorization.k8s.io/v1 kind: RoleBinding metadata: name: share-etc-pki-entitlement namespace: openshift-config-managed roleRef: apiGroup: rbac.authorization.k8s.io kind: Role name: share-etc-pki-entitlement subjects: - kind: ServiceAccount name: csi-driver-shared-resource namespace: openshift-buildsapiVersion: rbac.authorization.k8s.io/v1 kind: RoleBinding metadata: name: share-etc-pki-entitlement1 namespace: openshift-config-managed roleRef: apiGroup: rbac.authorization.k8s.io kind: Role name: share-etc-pki-entitlement subjects: - kind: ServiceAccount name: csi-driver-shared-resource namespace: openshift-builds2 Copy to Clipboard Copied! Toggle word wrap Toggle overflow 为运行构建的命名空间中
构建器和管道服务帐户创建RoleBinding对象:apiVersion: rbac.authorization.k8s.io/v1 kind: RoleBinding metadata: name: use-share-etc-pki-entitlement roleRef: apiGroup: rbac.authorization.k8s.io kind: ClusterRole name: use-share-etc-pki-entitlement subjects: - kind: ServiceAccount name: pipeline - kind: ServiceAccount name: builderapiVersion: rbac.authorization.k8s.io/v1 kind: RoleBinding metadata: name: use-share-etc-pki-entitlement1 roleRef: apiGroup: rbac.authorization.k8s.io kind: ClusterRole name: use-share-etc-pki-entitlement subjects: - kind: ServiceAccount name: pipeline - kind: ServiceAccount name: builderCopy to Clipboard Copied! Toggle word wrap Toggle overflow - 1
构建器和管道服务帐户的RoleBindingCR 的名称。
注意使用
SharedSecret对象的服务帐户由 OpenShift 控制器创建和管理。使用
buildah构建策略挂载SharedSecret对象。请参见以下示例:oc apply -f -<<EOF apiVersion: shipwright.io/v1beta1 kind: Build metadata: name: buildah-rhel spec: source: type: Git git: url: https://github.com/redhat-openshift-builds/samples contextDir: buildah-build strategy: name: buildah kind: ClusterBuildStrategy paramValues: - name: dockerfile value: DockerFile volumes: - csi: driver: csi.sharedresource.openshift.io readOnly: true volumeAttributes: sharedSecret: <sharedsecret_object_name> name: etc-pki-entitlement output: image: <output_image_location> EOF$ oc apply -f -<<EOF apiVersion: shipwright.io/v1beta1 kind: Build metadata: name: buildah-rhel spec: source: type: Git git: url: https://github.com/redhat-openshift-builds/samples contextDir: buildah-build strategy: name: buildah kind: ClusterBuildStrategy paramValues: - name: dockerfile value: DockerFile volumes: - csi: driver: csi.sharedresource.openshift.io readOnly: true1 volumeAttributes: sharedSecret: <sharedsecret_object_name>2 name: etc-pki-entitlement output: image: <output_image_location>3 EOFCopy to Clipboard Copied! Toggle word wrap Toggle overflow
Legal Notice
复制链接链接已复制到粘贴板!
Copyright © 2025 Red Hat
OpenShift documentation is licensed under the Apache License 2.0 (https://www.apache.org/licenses/LICENSE-2.0).
Modified versions must remove all Red Hat trademarks.
Portions adapted from https://github.com/kubernetes-incubator/service-catalog/ with modifications by Red Hat.
Red Hat, Red Hat Enterprise Linux, the Red Hat logo, the Shadowman logo, JBoss, OpenShift, Fedora, the Infinity logo, and RHCE are trademarks of Red Hat, Inc., registered in the United States and other countries.
Linux® is the registered trademark of Linus Torvalds in the United States and other countries.
Java® is a registered trademark of Oracle and/or its affiliates.
XFS® is a trademark of Silicon Graphics International Corp. or its subsidiaries in the United States and/or other countries.
MySQL® is a registered trademark of MySQL AB in the United States, the European Union and other countries.
Node.js® is an official trademark of Joyent. Red Hat Software Collections is not formally related to or endorsed by the official Joyent Node.js open source or commercial project.
The OpenStack® Word Mark and OpenStack logo are either registered trademarks/service marks or trademarks/service marks of the OpenStack Foundation, in the United States and other countries and are used with the OpenStack Foundation’s permission. We are not affiliated with, endorsed or sponsored by the OpenStack Foundation, or the OpenStack community.
All other trademarks are the property of their respective owners.