【问题标题】:RBAC: roles with multiple namespacesRBAC:具有多个命名空间的角色
【发布时间】:2020-01-03 17:12:27
【问题描述】:

尝试编写我的第一组 RBAC 角色。因此,试图找出为多个命名空间组件设置 2 个角色的最佳方式。

管理员角色(3 个命名空间的 RW 表示默认,ns1 和 ns2) 用户角色(只读 3 个命名空间,默认为 ns1 和 ns2)

正在考虑需要一个具有 2 个 clusterRoles 的服务帐户用于管理员/用户

apiVersion: rbac.authorization.k8s.io/v1
kind: ServiceAccount
metadata:
  name: sa
  namespace: default

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: admin-master
rules:
- apiGroups:
    - batch
  resources:
    - pods
  verbs:
    - create
    - delete
    - deletecollection
    - get
    - list
    - patch
    - update
    - watch

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: user-master
rules:
- apiGroups:
    - batch
  resources:
    - pods
  verbs:
    - get
    - list
    - watch

然后利用roleBindings:

apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: admin-rw
  namespace: ns1
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: admin-master
subjects:
  - kind: ServiceAccount
    name: sa
    namespace: default
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: user-readonly
  namespace: ns1
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: user-master
subjects:
  - kind: ServiceAccount
    name: sa
    namespace: default

但不确定如何将角色 admin-rw/user-readonly 与命名空间 2 (ns2) 绑定的最佳方式?

【问题讨论】:

    标签: kubernetes ansible yaml rbac


    【解决方案1】:

    角色是有范围的,要么绑定到特定的命名空间,要么绑定到整个集群。对于命名空间范围的角色,you can just simply deploy the same role in multiple namespaces

    这背后的想法是在集群中拥有分区权限,虽然it implies more administrative effort但是是一种更安全的做法。

    此外,在您的定义中,您尝试将权限绑定到特定命名空间,但是,您使用的是ClusterRole,即cluster-scoped resource。如果您想要命名空间范围的权限,您可能需要将其更改为 Role

    您可能会发现这个CNCF article 在这个问题上很有用。

    【讨论】:

      猜你喜欢
      • 2021-12-11
      • 2021-09-12
      • 2015-09-10
      • 1970-01-01
      • 1970-01-01
      • 2011-11-11
      • 1970-01-01
      • 2019-04-18
      • 1970-01-01
      相关资源
      最近更新 更多