83·安全进阶

Role 与 ClusterRole

kubernetessecurityrbacrole

Role 与 ClusterRole

学习目标

  1. 理解 Role 和 ClusterRole 的区别
  2. 掌握 Role 和 ClusterRole 的创建方法
  3. 了解何时使用 Role vs ClusterRole

核心概念

一句话解释

Role 是命名空间级别的权限定义,ClusterRole 是集群级别的权限定义。

Role vs ClusterRole

┌─────────────────────────────────────────────────────────────┐
│                    Kubernetes 集群                          │
│  ┌─────────────────┐  ┌─────────────────┐                  │
│  │  Namespace: dev │  │ Namespace: prod │                  │
│  │  ┌───────────┐  │  │  ┌───────────┐  │                  │
│  │  │   Role    │  │  │  │   Role    │  │                  │
│  │  │ pod-reader│  │  │  │ pod-reader│  │                  │
│  │  └───────────┘  │  │  └───────────┘  │                  │
│  └─────────────────┘  └─────────────────┘                  │
│                                                             │
│  ┌─────────────────────────────────────────────────────┐   │
│  │              ClusterRole (集群级)                    │   │
│  │         可以访问所有命名空间的资源                    │   │
│  └─────────────────────────────────────────────────────┘   │
└─────────────────────────────────────────────────────────────┘

对比表

特性RoleClusterRole
作用范围单个命名空间整个集群
资源类型命名空间资源集群资源 + 命名空间资源
典型用途团队权限隔离集群管理权限
绑定方式RoleBindingClusterRoleBinding 或 RoleBinding

Role 示例

# 允许读取 default 命名空间的 Pod
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  namespace: default
  name: pod-reader
rules:
- apiGroups: [""]        # "" 表示核心 API 组
  resources: ["pods"]
  verbs: ["get", "list", "watch"]

ClusterRole 示例

# 允许读取所有命名空间的 Pod 和 Node
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: cluster-reader
rules:
- apiGroups: [""]
  resources: ["pods", "nodes"]
  verbs: ["get", "list", "watch"]
- apiGroups: ["apps"]
  resources: ["deployments"]
  verbs: ["get", "list", "watch"]

何时使用 ClusterRole

使用 ClusterRole 的场景:
1. 集群级资源 (Node, PersistentVolume, Namespace)
2. 跨命名空间权限 (监控、日志系统)
3. 集群管理员角色

使用 Role 的场景:
1. 命名空间级资源 (Pod, Service, Deployment)
2. 团队权限隔离
3. 项目级权限管理

资源和 API 组

# 核心 API 组 (apiGroups: [""])
- pods, services, configmaps, secrets, namespaces, nodes, persistentvolumes

# apps API 组 (apiGroups: ["apps"])
- deployments, statefulsets, daemonsets, replicasets

# batch API 组 (apiGroups: ["batch"])
- jobs, cronjobs

# networking.k8s.io API 组 (apiGroups: ["networking.k8s.io"])
- networkpolicies, ingresses

# rbac.authorization.k8s.io API 组 (apiGroups: ["rbac.authorization.k8s.io"])
- roles, rolebindings, clusterroles, clusterrolebindings

Verbs (动词)

Verb说明对应 HTTP 方法
get获取单个资源GET
list列出资源GET (列表)
watch监听资源变化GET (watch)
create创建资源POST
update更新资源PUT
patch部分更新资源PATCH
delete删除资源DELETE
deletecollection批量删除资源DELETE (集合)

实践练习

练习 1:创建 Role

# 创建一个可以读写 ConfigMap 的 Role
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  namespace: default
  name: configmap-manager
rules:
- apiGroups: [""]
  resources: ["configmaps"]
  verbs: ["get", "list", "watch", "create", "update", "patch", "delete"]
# 应用 Role
kubectl apply -f configmap-manager-role.yaml

# 查看 Role
kubectl get role configmap-manager -n default -o yaml

练习 2:创建 ClusterRole

# 创建一个可以读取所有命名空间 Deployment 的 ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: deployment-viewer
rules:
- apiGroups: ["apps"]
  resources: ["deployments"]
  verbs: ["get", "list", "watch"]
# 应用 ClusterRole
kubectl apply -f deployment-viewer-clusterrole.yaml

# 查看 ClusterRole
kubectl get clusterrole deployment-viewer -o yaml

常见误解

1. ClusterRole 必须绑定到集群级

事实:ClusterRole 可以通过 RoleBinding 绑定到特定命名空间
误解:ClusterRole 只能通过 ClusterRoleBinding 绑定
解决:使用 RoleBinding 引用 ClusterRole 限制到命名空间

2. Role 只能定义命名空间资源权限

事实:Role 只能定义命名空间资源的权限
正确:集群资源 (Node, PV) 必须用 ClusterRole 定义

小结

要点说明
Role命名空间级权限定义
ClusterRole集群级权限定义
关键区别作用范围、资源类型
最佳实践最小权限、命名空间隔离、定期审计
常见场景Role 用于团队隔离,ClusterRole 用于集群管理

练习编辑器

bash
Loading...

继续学习

完成本课后,建议继续学习下一课「RoleBinding」 以巩固所学知识。