RBAC:具有多个命名空间的角色

dav*_*cks 5 yaml rbac ansible kubernetes

尝试编写我的第一组 RBAC 角色。因此,试图找出为多个命名空间组件分配两个角色的最佳方法。

管理员角色(RW 对于 3 个命名空间,即默认值,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
Run Code Online (Sandbox Code Playgroud)

然后使用角色绑定:

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
Run Code Online (Sandbox Code Playgroud)

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

yyy*_*hir 4

角色是有范围的,要么绑定到特定的命名空间,要么绑定到集群范围。对于命名空间范围的角色,您只需在多个命名空间中部署相同的角色即可

其背后的想法是在集群中拥有分区权限,尽管这意味着更多的管理工作,但却是一种更安全的做法。

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

您可能会发现这篇CNCF 文章对这个问题很有用。