本文由作者授权发布,未经许可,请勿转载。
作者:李岚清,网易数帆轻舟事业部资深工程师
基于角色的访问控制(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 everything