RBAC 控制"谁对什么资源能做什么"。四要素:Subject、Role、Verb、Binding。结尾附只读账号小案例。
一、四要素
Subject(主体):User / Group / ServiceAccount
Role(角色): 对哪些资源、允许哪些动作(get/list/create/delete…)
Binding(绑定):把角色绑给主体
ClusterRole / ClusterRoleBinding:集群级(跨命名空间)
二、Role 与 RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata: { name: pod-reader, namespace: staging }
rules:
- apiGroups: [""] # 核心组(Pod/Service)
resources: [pods, pods/log]
verbs: [get, list, watch]
- apiGroups: [apps]
resources: [deployments]
verbs: [get, list]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata: { name: read-pods, namespace: staging }
subjects:
- kind: User
name: tom
apiGroup: rbac.authorization.k8s.io
roleRef:
kind: Role
name: pod-reader
apiGroup: rbac.authorization.k8s.io
三、ServiceAccount(Pod 的身份)
apiVersion: v1
kind: ServiceAccount
metadata: { name: ci-deployer, namespace: prod }
---
# Pod 里用
spec:
serviceAccountName: ci-deployer
containers: [...]
# CI 里用 ServiceAccount 的 token
kubectl describe secret $(kubectl get sa ci-deployer -o jsonpath="{.secrets[0].name}")
四、常用角色速查
| 场景 | 授权 |
|---|---|
| 只读用户 | Role: get/list/watch |
| 发布账号 | Role: deployments/scale/update |
| 管理员 | cluster-admin(勿轻给) |
| CI/CD | ServiceAccount + RoleBinding |
| 监控系统 | ClusterRole: nodes/pods 只读 |
五、调试授权
kubectl auth can-i list pods --as=tom -n staging # 验证权限
kubectl auth can-i delete deployments --as=ci-deployer -n prod
kubectl get role,rolebinding -n staging
六、注意
- 默认 ServiceAccount 权限很小(好);别给 default SA 加权限;
- cluster-admin 只给管理员;
- CI 用专用 SA + 限定命名空间(别用 kubeconfig 管理员配置);
- token 泄露即权限泄露,定期轮换。
七、小案例:只读账号 + 发布账号
# 只读用户(开发查问题用)
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata: { name: readonly }
rules:
- apiGroups: [""]
resources: [pods, pods/log, services, configmaps, events]
verbs: [get, list, watch]
- apiGroups: [apps]
resources: [deployments, replicasets, statefulsets]
verbs: [get, list, watch]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata: { name: readonly-binding }
subjects:
- kind: User
name: dev-readonly
apiGroup: rbac.authorization.k8s.io
roleRef: { kind: ClusterRole, name: readonly, apiGroup: rbac.authorization.k8s.io }
---
# CI 发布账号(只能动 prod 的 deployment)
apiVersion: v1
kind: ServiceAccount
metadata: { name: ci-deployer, namespace: prod }
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata: { name: deployer, namespace: prod }
rules:
- apiGroups: [apps]
resources: [deployments]
verbs: [get, list, update, patch]
- apiGroups: [apps]
resources: [deployments/scale]
verbs: [update]
- apiGroups: [""]
resources: [pods, pods/log]
verbs: [get, list]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata: { name: ci-binding, namespace: prod }
subjects:
- kind: ServiceAccount
name: ci-deployer
roleRef: { kind: Role, name: deployer, apiGroup: rbac.authorization.k8s.io }
# 验证
kubectl auth can-i update deployments --as=system:serviceaccount:prod:ci-deployer -n prod # yes
kubectl auth can-i delete pods --as=system:serviceaccount:prod:ci-deployer -n prod # no
只读全集群 + CI 只能更新 deployment —— 最小权限原则的完整落地。
八、总结
Role 给动作、Binding 绑主体;ServiceAccount 是 Pod 身份;kubectl auth can-i 调试授权;cluster-admin 慎给、CI 专用 SA 限命名空间。