11-1 role和ClusterRole
在RBAC API中,一个角色定义了一组特定权限的规则。namespace范围内的角色由Role对象定义,而整个Kubernetes集群范围内有效的角色则通过ClusterRole对象实现。 https://www.jianshu.com/p/9991f189495f 1、Role Role对象只能用于授予对某一namespace中资源的访问权限。 以下示例表示在“default” namespace中定义一个Role对象,用于授予对资源pods的读访问权限,绑定到该Role的用户则具有get/watch/list pod资源的权限: kind: Role apiVersion: rbac.authorization.k8s.io/v1beta1 metadata: namespace: default name: pod-reader rules: - apiGroups: [""] # 空字符串""表明使用core API group resources: ["pods"] verbs: ["get", "watch", "list"] 2、ClusterRole ClusterRole对象可以授予整个集群范围内资源访问权限, 也可以对以下几种资源的授予访问权限: 集群范围资源(例如节点,即node) 非资源类型endpoint(例如”/healthz”) 跨所有namespaces的范围资源(例如pod,需要运行命令kubectl get pods --all-namespaces来查询集群中所有的pod) 以下示例中定义了一个名为pods-reader的ClusterRole,绑定到该ClusterRole的用户或对象具有用对集群中的资源pods的读访问权限: kind: ClusterRole apiVersion: rbac.authorization.k8s.io/v1beta1 metadata: # 鉴于ClusterRole是集群范围对象,所以这里不需要定义"namespace"字段 name: pods-reader rules: - apiGroups: [""] resources: ["pods"] verbs: ["get", "watch", "list"] RoleBinding与ClusterRoleBinding 角色绑定将一个角色中定义的各种权限授予一个或者一组用户,则该用户或用户组则具有对应绑定的Role或ClusterRole定义的权限。 角色绑定包含了一组相关主体(即subject, 包括用户——User、用户组——Group、或者服务账户——Service Account)以及对被授予角色的引用。 在某一namespace中可以通过RoleBinding对象授予权限,而集群范围的权限授予则通过ClusterRoleBinding对象完成。 3、RoleBinding RoleBinding可以将同一namespace中的subject(用户)绑定到某个具有特定权限的Role下,则此subject即具有该Role定义的权限。 下面示例中定义的RoleBinding对象在”default” namespace中将”pod-reader”角色授予用户”Caden”。 这一授权将允许用户”Caden”从”default” namespace中读取pod。 kind: RoleBinding apiVersion: rbac.authorization.k8s.io/v1beta1 metadata: name: read-pods namespace: default subjects: - kind: User name: Caden apiGroup: rbac.authorization.k8s.io roleRef: kind: Role name: pod-reader apiGroup: rbac.authorization.k8s.io 4、ClusterRoleBinding ClusterRoleBinding在整个集群级别和所有namespaces将特定的subject与ClusterRole绑定,授予权限。 下面示例中所定义的ClusterRoleBinding 允许在用户组”pods-reader”中的任何用户都可以读取集群中任何namespace中的pods。 kind: ClusterRoleBinding apiVersion: rbac.authorization.k8s.io/v1beta1 metadata: name: read-pods-global subjects: - kind: Group name: pods-reader apiGroup: rbac.authorization.k8s.io roleRef: kind: ClusterRole name: pods-reader apiGroup: rbac.authorization.k8s.io kind: ClusterRoleBinding apiVersion: rbac.authorization.k8s.io/v1beta1 metadata: name: read-pods-global subjects: - kind: Group name: pods-reader apiGroup: rbac.authorization.k8s.io roleRef: kind: ClusterRole name: pods-reader apiGroup: rbac.authorization.k8s.io 此外,RoleBinding对象也可以引用一个ClusterRole对象用于在RoleBinding所在的namespace内授予用户对所引用的ClusterRole中 定义的namespace资源的访问权限。这一点允许管理员在整个集群范围内首先定义一组通用的角色,然后再在不同的名字空间中复用这些角色。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek “源神”启动!「GitHub 热点速览」
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· NetPad:一个.NET开源、跨平台的C#编辑器