【问题标题】:How to configure a ClusterRole for namespaced resources如何为命名空间资源配置 ClusterRole
【发布时间】:2022-04-26 10:56:53
【问题描述】:

我想允许命名空间 A 中的 ServiceAccount 访问命名空间 B 中的资源。 为此,我通过 ClusterRoleBinding 将 ServiceAccount 连接到 ClusterRole。 The documentation 说我可以“使用 ClusterRole [1.] 定义命名空间资源的权限并在单个命名空间内授予”

但是查看K8s documentation 我找不到如何使用命名空间资源创建 ClusterRole 的方法。我怎样才能做到这一点?

【问题讨论】:

    标签: kubernetes roles k8s-serviceaccount k8s-cluster-role


    【解决方案1】:

    ...how to create a ClusterRole with namespaced resources...

    进一步阅读down一点点:

    ClusterRole 可用于授予与角色相同的权限。 因为 ClusterRoles 是集群范围的。您也可以使用它们来 授予访问权限:

    ...

    • 命名空间资源(如 Pod),跨所有命名空间

    ClusterRole 不会帮助您限制对单个命名空间对象的访问。但是,您可以使用 RoleBinding 引用 ClusterRole 并限制对 RoleBinding 命名空间中的对象的访问。

    【讨论】:

    • 我明白了,所以我试图实现的目标是不可能的。我不能在特定的命名空间中定义资源,但可以定义角色。
    • 不确定你的意思,如果正确理解你的问题,你已经在那里了。只需使用RoleBinding 控制RoleBinding 指定的命名空间中ClusterRole 中指定的资源即可。
    【解决方案2】:

    我相信您需要创建集群角色而不是角色。 示例:

    apiVersion: rbac.authorization.k8s.io/v1
    kind: ClusterRole
    metadata:
      name: role-grantor
    rules:
    - apiGroups: ["rbac.authorization.k8s.io"]
      resources: ["rolebindings"]
      verbs: ["create"]
    - apiGroups: ["rbac.authorization.k8s.io"]
      resources: ["clusterroles"]
      verbs: ["bind"]
      # omit resourceNames to allow binding any ClusterRole
      resourceNames: ["admin","edit","view"]
    ---
    apiVersion: rbac.authorization.k8s.io/v1
    kind: RoleBinding
    metadata:
      name: role-grantor-binding
      namespace: user-1-namespace
    roleRef:
      apiGroup: rbac.authorization.k8s.io
      kind: ClusterRole
      name: role-grantor
    subjects:
    - apiGroup: rbac.authorization.k8s.io
      kind: User
      name: user-1
    

    上面的例子来自这个link

    【讨论】:

      【解决方案3】:

      我发现其他两个答案都有点令人困惑,希望这更清楚。

      您在创建 ClusterRole 时做了正确的事,但您想使用命名空间 RoleBinding 绑定它,不是 ClusterRoleBinding

      使用您的示例进行示例。请注意 RoleBinding 如何在 B 命名空间中,赋予 A 的 ServiceAccountClusterRole 中定义的权限,但仅限于 B 命名空间。

      ---
      apiVersion: rbac.authorization.k8s.io/v1
      kind: ClusterRole
      metadata:
        name: what-a-is-allowed-to-do-in-b
      rules:
      - apiGroups: [""]
        resources: ["pods", "deployments"] # etc
        verbs: ["get", "list", "create"]
      ---
      apiVersion: v1
      kind: ServiceAccount
      metadata:
        name: my-app
        namespace: namespace-a
      ---
      apiVersion: rbac.authorization.k8s.io/v1
      kind: RoleBinding
      metadata:
        name: what-a-is-allowed-to-do-in-b
        namespace: namespace-b
      roleRef:
        apiGroup: rbac.authorization.k8s.io
        kind: ClusterRole
        name: what-a-is-allowed-to-do-in-b
      subjects:
      - kind: ServiceAccount
        name: my-app
        namespace: namespace-a
      

      注意事项: 您必须使用 ClusterRole,因为没有它您就无法走出自己的命名空间。通过使用命名空间的RoleBinding,您可以限制对该RoleBinding 命名空间范围的访问。

      【讨论】:

        猜你喜欢
        • 2022-09-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-06-12
        • 1970-01-01
        • 2021-02-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多