容器与云原生 / 系统课时

Role 与 ClusterRole

Role 与 ClusterRole

Role 与 ClusterRole 用来声明 Kubernetes API 操作规则,但它们的作用域和绑定方式不同。本课通过一组可验证的最小权限配置,掌握两类对象的选择方法。

学习目标

  • 区分 Role 与 ClusterRole 的定义范围和可授权资源
  • 理解 RoleBinding 与 ClusterRoleBinding 如何决定权限生效范围
  • 使用 kubectl auth can-i 验证最小权限配置

两类权限对象的本质

RBAC 规则由 apiGroups、resources、verbs 等字段组成。Role 是命名空间级对象,只能在自身命名空间内授予命名空间级资源权限;ClusterRole 是集群级对象,既可描述节点等集群级资源,也可把一组命名空间级规则定义成可复用模板。

权限规则不会自动赋给任何主体。RoleBinding 或 ClusterRoleBinding 才会把规则绑定到用户、组或 ServiceAccount。RoleBinding 的授权范围始终局限在它所在的命名空间;ClusterRoleBinding 则让 ClusterRole 的规则在整个集群范围生效。

Role、ClusterRole 与绑定对象的作用域关系

作用域与绑定方式

对象 存放位置 适合描述的权限 常见绑定方式
Role 某个命名空间 该命名空间内的 Pod、ConfigMap 等 同命名空间的 RoleBinding
ClusterRole 集群级 Node、Namespace 等集群资源,或可复用的命名空间资源规则 RoleBinding 或 ClusterRoleBinding
RoleBinding 某个命名空间 把 Role 或 ClusterRole 的命名空间级规则授予当前命名空间 权限不扩展到其他命名空间
ClusterRoleBinding 集群级 把 ClusterRole 授予整个集群 可访问集群资源或所有命名空间中的目标资源

选择对象时先问“权限要在哪些范围生效”,再问“规则是否需要复用”。只访问一个命名空间时优先使用 Role;多个命名空间需要同一套规则时,可创建一个 ClusterRole,再在每个命名空间分别创建 RoleBinding。

最小权限主示例

下面的配置让 team-a 命名空间中的 auditor 账号只能读取本命名空间的 Pod,同时允许它读取集群级 Node。示例使用只读动词,便于观察作用域而不修改业务资源。

yaml
apiVersion: v1
kind: Namespace
metadata:
  name: team-a
---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: auditor
  namespace: team-a
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  name: pod-reader
  namespace: team-a
rules:
  - apiGroups: [""]
    resources: ["pods"]
    verbs: ["get", "list"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: bind-pod-reader
  namespace: team-a
subjects:
  - kind: ServiceAccount
    name: auditor
    namespace: team-a
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: Role
  name: pod-reader
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: team-a-node-reader
rules:
  - apiGroups: [""]
    resources: ["nodes"]
    verbs: ["get", "list"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: bind-team-a-node-reader
subjects:
  - kind: ServiceAccount
    name: auditor
    namespace: team-a
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: team-a-node-reader

将配置保存为 rbac-scope-demo.yaml,再以具备 RBAC 管理权限的账号执行:

bash
kubectl apply -f rbac-scope-demo.yaml

预期输出

text
namespace/team-a created
serviceaccount/auditor created
role.rbac.authorization.k8s.io/pod-reader created
rolebinding.rbac.authorization.k8s.io/bind-pod-reader created
clusterrole.rbac.authorization.k8s.io/team-a-node-reader created
clusterrolebinding.rbac.authorization.k8s.io/bind-team-a-node-reader created

重复执行时,created 可能变为 unchanged 或 configured。

验证权限边界

kubectl auth can-i 可以在不实际读取资源的情况下询问授权结果。下面通过模拟 ServiceAccount 身份,分别验证当前命名空间、其他命名空间和集群级资源。

bash
kubectl auth can-i --as=system:serviceaccount:team-a:auditor get pods -n team-a

kubectl auth can-i --as=system:serviceaccount:team-a:auditor get pods -n default

kubectl auth can-i --as=system:serviceaccount:team-a:auditor get nodes

预期输出

text
yes
no
yes

第一个 yes 来自 RoleBinding,第二个 no 说明权限没有跨命名空间扩散,第三个 yes 来自 ClusterRoleBinding。执行 --as 通常要求当前操作者具有模拟身份的权限;学习环境可使用集群管理员账号验证。

选择原则与常见错误

只授权一个命名空间时,使用 Role 与 RoleBinding;同一套命名空间规则需要在多个命名空间复用时,使用 ClusterRole 配合多个 RoleBinding;访问 Node、Namespace 等集群级资源,或确实需要跨全部命名空间授权时,才使用 ClusterRoleBinding。

出现 403 Forbidden 时,依次检查 apiGroups、resources、verbs 是否匹配实际请求,再检查 Binding 的命名空间、subjects[].namespace 和 roleRef.kind。roleRef 创建后不可直接修改,切换引用对象通常需要删除并重建 Binding。

避免使用 resources: ["*"] 与 verbs: ["*"] 作为常规方案。最小权限应只包含应用实际需要的资源和动词,并通过 kubectl auth can-i 逐项验证,而不是依赖名称推测授权结果。

按自己的节奏,确认真正理解。

目录与阅读设置

阅读设置

17

记录一个想法

Role 与 ClusterRole

0 / 10000 · 笔记保存到账号。未提交的修改会在关闭、离开或刷新后丢失。

登录哪都通