【解构云原生】K8s 的 RBAC - 基于角色的访问控制

本文由作者授权发布,未经许可,请勿转载。

作者:李岚清,网易数帆轻舟事业部资深工程师

基于角色的访问控制(Role-Based Access Control, 即“RBAC”)使用 “rbac.authorization.k8s.io” API Group 实现授权控制,用户可以通过 Kubernetes API 动态配置策略。

API 对象

RBAC 共包含4个类型的资源对象:Role、ClusterRole、RoleBinding、ClusterRoleBinding。用户可以通过增删改查上述四个API对象来实现访问权限的动态控制。

假如没有创建任何ClusterRole和Role,则所有用户是无权限访问任何K8S的API的,K8S的权限控制是类似于白名单的,通过创建 Role&RoleBinding 和 ClusterRole&ClusterRoleBinding 将用户权限添加到“白名单”中,以此来实现授权。

Role 和 ClusterRole

在 RBAC API 中,Role 和 Cluster 都表示一组权限的集合,Role 是 namespace-scoped的,定义的是某一个namespace中的资源权限;而如果想要实现集群级别的或者跨namespace的权限控制,则需要创建 ClusterRole。

Role example

如上文所说,Role 是 namespace-scoped的,定义的是某一个namespace中的资源权限。

apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  namespace: default
  name: pod-reader
rules:
- apiGroups: [""] # "" indicates the core API group
  resources: ["pods"]
  verbs: ["get", "watch", "list"]

上述 pod-reader role,表示可以get/list/watch default namespce下面的pods资源。

Role 中可以定义多条rules,每条rule表示一个权限,rule类型定义如下:

type Rule struct {
	// Verbs is a list of Verbs that apply to ALL the ResourceKinds and AttributeRestrictions contained in this rule.  VerbAll represents all kinds.
	Verbs []string

	// APIGroups is the name of the APIGroup that contains the resources.
	// If multiple API groups are specified, any action requested against one of the enumerated resources in any API group will be allowed.
	APIGroups []string
	// Resources is a list of resources this rule applies to.  '*' represents all resources in the specified apiGroups.
	// '*/foo' represents the subresource 'foo' for all resources in the specified apiGroups.
	Resources []string
	// ResourceNames is an optional white list of names that the rule applies to.  An empty set means that everyt
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

网易杭研

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值