Role 与 ClusterRole
学习目标
- 理解 Role 和 ClusterRole 的区别
- 掌握 Role 和 ClusterRole 的创建方法
- 了解何时使用 Role vs ClusterRole
核心概念
一句话解释
Role 是命名空间级别的权限定义,ClusterRole 是集群级别的权限定义。
Role vs ClusterRole
┌─────────────────────────────────────────────────────────────┐
│ Kubernetes 集群 │
│ ┌─────────────────┐ ┌─────────────────┐ │
│ │ Namespace: dev │ │ Namespace: prod │ │
│ │ ┌───────────┐ │ │ ┌───────────┐ │ │
│ │ │ Role │ │ │ │ Role │ │ │
│ │ │ pod-reader│ │ │ │ pod-reader│ │ │
│ │ └───────────┘ │ │ └───────────┘ │ │
│ └─────────────────┘ └─────────────────┘ │
│ │
│ ┌─────────────────────────────────────────────────────┐ │
│ │ ClusterRole (集群级) │ │
│ │ 可以访问所有命名空间的资源 │ │
│ └─────────────────────────────────────────────────────┘ │
└─────────────────────────────────────────────────────────────┘
对比表
| 特性 | Role | ClusterRole |
|---|---|---|
| 作用范围 | 单个命名空间 | 整个集群 |
| 资源类型 | 命名空间资源 | 集群资源 + 命名空间资源 |
| 典型用途 | 团队权限隔离 | 集群管理权限 |
| 绑定方式 | RoleBinding | ClusterRoleBinding 或 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...