4. RBAC实践:创建只读用户、创建管理员用户、聚合ClusterRole
好,咱们直接进入正题。RBAC这东西,说白了就是Kubernetes里的权限门禁系统。你想想看,一个集群里那么多人,有开发的、有运维的、有审计的,总不能所有人都能删Pod、改Deployment吧?那不乱套了。
我个人习惯把RBAC分成三个层次来理解:谁(User/ServiceAccount)能对什么(Resource)做什么(Verb)。今天我们就通过三个实战场景,把这块彻底搞明白。
4.1 创建只读用户:给开发同学看,不让他们动
先来个最常见的需求。我记得刚接手一个生产集群时,开发团队天天抱怨「我们连Pod日志都看不了」。但给他们管理员权限?想都别想。于是只读用户就派上用场了。
4.1.1 创建ServiceAccount
kubectl create serviceaccount readonly-user -n default
嗯,这里要注意,ServiceAccount是命名空间级别的。如果你想让这个用户看整个集群,得用ClusterRoleBinding。
4.1.2 创建只读Role
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
namespace: default
name: readonly-role
rules:
- apiGroups: [""]
resources: ["pods", "pods/log", "services", "endpoints"]
verbs: ["get", "list", "watch"]
- apiGroups: ["apps"]
resources: ["deployments", "statefulsets"]
verbs: ["get", "list", "watch"]
看到没?verbs里只有get、list、watch,没有create、update、delete。这就是只读的核心。
kubectl api-resources 查看。我在项目中经常用这个命令,省得记一堆API组名。
4.1.3 绑定Role和ServiceAccount
kubectl create rolebinding readonly-binding \
--role=readonly-role \
--serviceaccount=default:readonly-user \
-n default
4.1.4 生成kubeconfig
这一步很多人会忘。光有权限不行,还得有凭证。
# 获取token
TOKEN=$(kubectl get secret $(kubectl get sa readonly-user -o jsonpath='{.secrets[0].name}') -o jsonpath='{.data.token}' | base64 --decode)
# 配置kubeconfig
kubectl config set-credentials readonly-user --token=$TOKEN
kubectl config set-context readonly-context \
--cluster=your-cluster \
--user=readonly-user
kubectl create token 命令生成。有一次升级集群后,所有只读用户突然失效,排查了半天才发现是这个原因。
4.2 创建管理员用户:给运维团队用的「超级权限」
管理员用户就简单多了。说白了就是给一个人绑定cluster-admin这个内置的ClusterRole。
4.2.1 创建管理员ServiceAccount
kubectl create serviceaccount admin-user -n kube-system
我个人习惯把管理员账号放在kube-system命名空间里,这样和普通用户隔离开,方便审计。
4.2.2 绑定cluster-admin
kubectl create clusterrolebinding admin-binding \
--clusterrole=cluster-admin \
--serviceaccount=kube-system:admin-user
就这么简单。cluster-admin是Kubernetes内置的最高权限ClusterRole,几乎能干所有事。
- 集群初始化配置
- 故障紧急修复
- 安装集群级别的组件(如Ingress Controller、监控系统)
4.3 聚合ClusterRole:把多个权限拼在一起
这个功能我特别喜欢。你想想看,有时候一个角色需要同时拥有多个领域的权限,比如既要能看Pod,又要能管理Ingress,还要能查看节点状态。一个个写rules太麻烦了。
聚合ClusterRole就是干这个的。它可以把多个ClusterRole的权限「合并」到一起。
4.3.1 创建基础ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: pod-reader
rules:
- apiGroups: [""]
resources: ["pods"]
verbs: ["get", "list", "watch"]
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: service-reader
rules:
- apiGroups: [""]
resources: ["services"]
verbs: ["get", "list", "watch"]
4.3.2 创建聚合ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: combined-reader
labels:
rbac.authorization.k8s.io/aggregate-to-viewer: "true"
aggregationRule:
clusterRoleSelectors:
- matchLabels:
rbac.authorization.k8s.io/aggregate-to-viewer: "true"
rules: [] # 规则由聚合自动生成
看到关键点了吗?aggregationRule 里的 clusterRoleSelectors 会通过标签选择器,自动把带有 rbac.authorization.k8s.io/aggregate-to-viewer: "true" 标签的ClusterRole的权限合并进来。
4.3.3 给基础ClusterRole打标签
kubectl label clusterrole pod-reader rbac.authorization.k8s.io/aggregate-to-viewer=true
kubectl label clusterrole service-reader rbac.authorization.k8s.io/aggregate-to-viewer=true
打完标签后,combined-reader这个ClusterRole就自动拥有了pod-reader和service-reader的所有权限。
4.4 本章核心知识图谱
下面这张图展示了RBAC的核心逻辑和今天三个实践的关系:
4.5 几个实用建议
最后,分享几个我在实际项目中总结的经验:
- 权限粒度要适中:别太细(每个Pod一个Role),也别太粗(所有人都是cluster-admin)。我一般按「应用模块」来划分。
- 定期审计权限:用
kubectl auth can-i --list --as=system:serviceaccount:namespace:name检查某个账号到底能干什么。 - 善用内置ClusterRole:Kubernetes自带了view、edit、admin、cluster-admin四个ClusterRole,大部分场景够用了。
- 聚合ClusterRole是神器:当你的集群规模变大、团队变多时,它能帮你省下大量重复的YAML编写工作。
好了,RBAC的实践就讲到这里。记住一个核心原则:给够用的权限,不多给一分。这样你的集群才能既安全又高效。
,