使用共享资源
使用 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.io
CR。 -
为
SharedSecret
CR 创建ClusterRole
对象。 -
为 Shared Resource CSI 驱动程序创建
Role
和RoleBinding
对象。 - 管理集群中命名空间中的角色和角色绑定,以控制用户。
-
管理角色和角色绑定,以便 pod 指定的服务帐户可以使用
SharedSecret
CR 挂载引用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 - watch
apiVersion: rbac.authorization.k8s.io/v1 kind: Role metadata: name: share-etc-pki-entitlement
1 namespace: openshift-config-managed rules: - apiGroups: - "" resources: - secrets resourceNames: - etc-pki-entitlement verbs: - get - list - watch
Copy to Clipboard Copied! Toggle word wrap Toggle overflow - 1
Role
CR 的名称。
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-builds
apiVersion: rbac.authorization.k8s.io/v1 kind: RoleBinding metadata: name: share-etc-pki-entitlement
1 namespace: openshift-config-managed roleRef: apiGroup: rbac.authorization.k8s.io kind: Role name: share-etc-pki-entitlement
2 subjects: - kind: ServiceAccount name: csi-driver-shared-resource
3 namespace: openshift-builds
Copy to Clipboard Copied! Toggle word wrap Toggle overflow 使用以下示例配置创建
SharedSecret
CR: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-secret
1 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
> 替换为SharedSecret
CR 的名称。
使用以下示例配置,创建一个
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: - use
apiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRole metadata: name: use-shared-test-secret
1 rules: - apiGroups: - sharedresource.openshift.io resources: - sharedsecrets resourceNames: - shared-test-secret verbs: - use
Copy to Clipboard Copied! Toggle word wrap Toggle overflow - 1
ClusterRole
CR 的名称。
1.1.2. 在 pod 中使用 SharedSecret 自定义资源 复制链接链接已复制到粘贴板!
要从 pod 访问 SharedSecret
自定义资源(CR),您可以为服务帐户授予基于角色的访问控制(RBAC)权限。
先决条件
-
您已为您要在集群中的命名空间之间共享的
Secret
对象创建一个SharedSecret
CR 实例。 您必须具有执行以下操作的权限:
-
运行
oc adm policy who-can use <sharedsecret_identifier
> 命令检查服务帐户是否可以使用SharedSecret
CR,并且服务帐户列在命名空间中。 -
确认 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-secret
1 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-secret
apiVersion: 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-secret
2 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.io
CR。 -
为
SharedConfigMap
CR 创建ClusterRole
对象。 -
为 Shared Resource Container Storage Interface (CSI)驱动程序创建
Role
和RoleBinding
对象。 - 管理集群中命名空间中的角色和角色绑定,以控制哪些用户可以获取、列出和监视实例。
-
管理角色和角色绑定,以便 pod 指定的服务帐户可以使用
SharedConfigMap
CR 挂载引用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-builds
apiVersion: 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-builds
Copy to Clipboard Copied! Toggle word wrap Toggle overflow 为您要在集群中的命名空间之间共享的
ConfigMap
对象创建SharedConfigMap
CR。以下示例显示了示例配置: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-config
1 spec: configMapRef: name: shared-config namespace: <name_of_the_source_namespace>
2 Copy to Clipboard Copied! Toggle word wrap Toggle overflow 创建一个
ClusterRole
CR 实例,其授予基于角色的访问控制(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: - use
apiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRole metadata: name: <cluster_role_name>
1 rules: - apiGroups: - sharedresource.openshift.io resources: - sharedconfigmaps resourceNames: - share-test-config
2 verbs: - use
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
1.2.2. 在 pod 中使用 SharedConfigMap 自定义资源 复制链接链接已复制到粘贴板!
要从 pod 访问 SharedConfigMap
自定义资源(CR),您必须为关联的服务帐户授予所需的基于角色的访问控制(RBAC)权限,以使用该 SharedConfigMap
CR。
先决条件
-
为您要在集群中的命名空间之间共享的配置映射创建
SharedConfigMap
CR 实例。 您必须具有执行以下操作的权限:
-
运行
oc get sharedconfigmaps
命令,以获取SharedConfigMap
CR 实例的列表。 -
运行
oc adm policy who-can use <sharedsecret_identifier
> 命令检查服务帐户是否可以使用SharedSecret
CR,并且服务帐户列在命名空间中。 -
运行
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-config
apiVersion: 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-config
2 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
> 命令检查服务帐户是否可以使用SharedSecret
CR,并且服务帐户列在命名空间中。
-
创建
如果您无法完成最后的两个先决条件,集群管理员可以建立必要的 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 EOF
Copy 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: - use
apiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRole metadata: name: use-share-etc-pki-entitlement
1 rules: - apiGroups: - sharedresource.openshift.io resources: - sharedsecrets resourceNames: - etc-pki-entitlement verbs: - use
Copy to Clipboard Copied! Toggle word wrap Toggle overflow - 1
ClusterRole
CR 的名称。
创建
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 - watch
apiVersion: rbac.authorization.k8s.io/v1 kind: Role metadata: name: share-etc-pki-entitlement
1 namespace: openshift-config-managed rules: - apiGroups: - "" resources: - secrets resourceNames: - etc-pki-entitlement verbs: - get - list - watch
Copy to Clipboard Copied! Toggle word wrap Toggle overflow - 1
Role
CR 的名称。
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-builds
apiVersion: rbac.authorization.k8s.io/v1 kind: RoleBinding metadata: name: share-etc-pki-entitlement
1 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-builds
2 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: builder
apiVersion: rbac.authorization.k8s.io/v1 kind: RoleBinding metadata: name: use-share-etc-pki-entitlement
1 roleRef: apiGroup: rbac.authorization.k8s.io kind: ClusterRole name: use-share-etc-pki-entitlement subjects: - kind: ServiceAccount name: pipeline - kind: ServiceAccount name: builder
Copy to Clipboard Copied! Toggle word wrap Toggle overflow - 1
构建器
和管道
服务帐户的RoleBinding
CR 的名称。
注意使用
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: true
1 volumeAttributes: sharedSecret: <sharedsecret_object_name>
2 name: etc-pki-entitlement output: image: <output_image_location>
3 EOF
Copy 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.