使用共享资源


builds for Red Hat OpenShift 1.2

使用 Shared Resources CSI Driver Operator

Red Hat OpenShift Documentation Team

摘要

本文档提供了使用共享资源 CSI 驱动程序的程序示例。

第 1 章 共享资源 CSI Driver Operator

传统上,存储供应商已将存储驱动程序作为 Kubernetes 的一部分提供。随着容器存储接口(CSI)的实现,第三方供应商可以使用标准接口来提供存储插件,而无需更改核心 Kubernetes 代码。CSI Operators 为构建的用户提供存储选项,如卷快照,它们无法通过 in-tree 卷插件实现。

Shared Resource CSI 驱动程序是一个特殊的 CSI 驱动程序,它允许集群管理员使用 csi 卷类型在命名空间间安全地共享 SecretConfigMap 对象。共享资源 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 驱动程序创建 RoleRoleBinding 对象。
    • 管理集群中命名空间中的角色和角色绑定,以控制用户。
    • 管理角色和角色绑定,以便 pod 指定的服务帐户可以使用 SharedSecret CR 挂载引用 Secret 对象的 CSI 卷。
    • 访问包含您要共享的 Secret 对象的命名空间。

流程

  1. 创建 RoleRoleBinding 对象,为 Shared Resource CSI 驱动程序授予使用以下示例配置访问 SharedSecret 对象的权限:

    Role 对象示例

    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 Toggle word wrap

    1
    Role CR 的名称。

    RoleBinding 对象示例

    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 Toggle word wrap

    1
    RoleBinding CR 的名称。
    2
    Role CR 的名称。
    3
    ServiceAccount CR 的名称。
  2. 使用以下示例配置创建 SharedSecret CR:

    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 Toggle word wrap
    1
    <name_of_the_source_namespace > 替换为 SharedSecret CR 的名称。
  3. 使用以下示例配置,创建一个 ClusterRole 对象来授予 RBAC 权限以使用引用的共享资源:

    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 Toggle word wrap
    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。

流程

  1. 在与角色关联的集群中创建 RoleBinding 对象,并为服务帐户授予使用共享资源的权限。请参见以下示例配置:

    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 Toggle word wrap
    1
    RoleBinding CR 的名称。
    2
    <service_account_name > 替换为应用程序的服务帐户名称。
  2. 将共享资源 csi 驱动程序挂载到 pod 或接受 csi 卷的任何其他资源中。请参见以下示例配置:

    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 Toggle word wrap
    1
    将 <app_namespace> 替换为应用程序的命名空间名称。
    2
    sharedSecret 属性的值,它必须与 SharedSecret CR 的名称相同。
    重要

    如果 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)驱动程序创建 RoleRoleBinding 对象。
    • 管理集群中命名空间中的角色和角色绑定,以控制哪些用户可以获取、列出和监视实例。
    • 管理角色和角色绑定,以便 pod 指定的服务帐户可以使用 SharedConfigMap CR 挂载引用 ConfigMap 对象的 csi 卷。
    • 访问包含您要共享的 ConfigMap 对象的命名空间。

流程

  1. 创建 RoleRoleBinding 对象,为 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> 
    1
    
    rules:
      - apiGroups: [""]
        resources: ["configmaps"]
        resourceNames: ["shared-config"]
        verbs: ["get", "list", "watch"]
    Copy to Clipboard Toggle word wrap

    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> 
    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 Toggle word wrap

    1
    <name_of_the_source_namespace > 替换为源命名空间的名称。
    2
    指定 Shared Resource CSI 驱动程序 DaemonSet 的服务帐户列表。使用 Red Hat OpenShift 构建部署时,服务帐户名称为 csi-driver-shared-resource,命名空间与部署 Red Hat OpenShift Operator 的 Builds 匹配。
  2. 为您要在集群中的命名空间之间共享的 ConfigMap 对象创建 SharedConfigMap CR。以下示例显示了示例配置:

    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 Toggle word wrap
    1
    指定 SharedConfigMap CR 的名称。
    2
    <name_of_the_source_namespace > 替换为源命名空间的名称。
  3. 创建一个 ClusterRole CR 实例,其授予基于角色的访问控制(RBAC)权限,以使用引用的共享资源。以下示例显示了示例配置:

    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 Toggle word wrap
    1
    <cluster_role_name > 替换为集群角色的名称。
    2
    指定 SharedSecret CR 的名称。

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。

流程

  1. 创建与角色关联的 RoleBinding 对象,并为服务帐户授予使用共享资源的权限。

    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 Toggle word wrap
    1
    <app_namespace > 替换为应用程序的命名空间名称。
    2
    <service_account_name > 替换为应用程序的服务帐户名称。
  2. 将 Shared Resource Container Storage Interface (CSI)驱动程序挂载到 pod 或接受 csi 卷的任何其他资源中。

    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 Toggle word wrap
    1
    将 <app_namespace> 替换为应用程序的命名空间名称。
    2
    指定 sharedConfigMap 对象的名称。
    重要

    如果 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。

流程

  1. 运行以下命令,使用集群的授权 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
    Copy to Clipboard Toggle word wrap
  2. 创建 ClusterRole 对象,以授予使用以下示例配置访问 SharedSecret 对象的权限:

    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 Toggle word wrap
    1
    ClusterRole CR 的名称。
  3. 创建 RoleRoleBinding 对象,为 Shared Resource CSI 驱动程序授予访问 SharedSecret 对象的权限:

    Role 对象示例

    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 Toggle word wrap

    1
    Role CR 的名称。

    RoleBinding 对象示例

    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 Toggle word wrap

    1
    RoleBinding CR 的名称。
    2
    安装 openshift-builds 的命名空间的名称。
  4. 为运行构建的命名空间中 构建器和 管道 服务帐户创建 RoleBinding 对象:

    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 Toggle word wrap
    1
    构建器 和管道 服务帐户的 RoleBinding CR 的名称。
    注意

    使用 SharedSecret 对象的服务帐户由 OpenShift 控制器创建和管理。

  5. 使用 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 
    1
    
          volumeAttributes:
            sharedSecret: <sharedsecret_object_name> 
    2
    
        name: etc-pki-entitlement
      output:
        image: <output_image_location> 
    3
    
    EOF
    Copy to Clipboard Toggle word wrap
    1
    您必须将 readOnly 设置为 true,以便在构建中挂载共享资源。
    2
    <sharedsecret_object_name > 替换为 SharedSecret 对象的名称,使其包含在构建中。
    3
    <output_image_location > 替换为您要推送构建的镜像的位置。

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.

返回顶部
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

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

让开源更具包容性

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

關於紅帽

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

Theme

© 2025 Red Hat