RBAC API类型
RBAC API 所声明的四种顶级类型【Role、ClusterRole、RoleBinding 和 ClusterRoleBinding】。用户可以像与其他 API 资源交互一样,(通过 kubectl API 调用等方式)与这些资源交互。

Role 和 ClusterRole
在 RBAC API 中,一个角色包含一组相关权限的规则。权限是纯粹累加的(不存在拒绝某操作的规则),即只能给权限累加,不存在给了XX权限,然后去掉XX01权限的情况。角色可以用 Role 来定义到某个命名空间(namespace)上, 或者用 ClusterRole 来定义到整个集群作用域(所有namespace)。

一个 Role 只可以用来对某一命名空间中的资源赋予访问权限。

Role示例:
定义到名称为 “default” 的命名空间,可以用来授予对该命名空间中的 Pods 的读取权限:

复制代码
1 apiVersion: rbac.authorization.k8s.io/v1
2 kind: Role
3 metadata:
4 name: pod-reader
5 namespace: default
6 rules:
7 - apiGroups: [""] # “” 指定核心 API 组
8 resources: [“pods”]
9 verbs: [“get”, “watch”, “list”]
复制代码
ClusterRole 可以授予的权限和 Role 相同,但是因为 ClusterRole 属于集群范围,所以它也可以授予以下访问权限:

集群范围资源 (比如 nodes访问)
非资源端点(比如 “/healthz” 访问)
跨命名空间访问的有名称空间作用域的资源(如 Pods),比如运行命令kubectl get pods --all-namespaces 时需要此能力

ClusterRole示例
可用来对某特定命名空间下的 Secrets 的读取操作授权,或者跨所有名称空间执行授权(取决于它是如何绑定的):

复制代码
1 apiVersion: rbac.authorization.k8s.io/v1
2 kind: ClusterRole
3 metadata:
4 name: secret-reader
5 # 此处的 “namespace” 被省略掉是因为 ClusterRoles 是没有命名空间的。
6 rules:
7 - apiGroups: [""]
8 resources: [“secrets”]
9 verbs: [“get”, “watch”, “list”]
复制代码

RoleBinding 和 ClusterRoleBinding
角色绑定(RoleBinding)是将角色中定义的权限赋予一个用户或者一组用户。 它包含若干主体【subjects】(users、groups或 service accounts)的列表和对这些主体所获得的角色引用。

可以使用 RoleBinding 在指定的命名空间中执行授权,或者在集群范围的命名空间使用 ClusterRoleBinding 来执行授权。

一个 RoleBinding 可以引用同一的命名空间中的 Role。

RoleBinding示例
将 “pod-reader” 角色授予在 “default” 命名空间中的用户 “jane”; 这样,用户 “jane” 就具有了读取 “default” 命名空间中 pods 的权限。

在下面的例子中,角色绑定使用 roleRef 将用户 “jane” 绑定到前文创建的角色 Role,其名称是 pod-reader。

复制代码
1 apiVersion: rbac.authorization.k8s.io/v1
2 # 此角色绑定,使得用户 “jane” 能够读取 “default” 命名空间中的 Pods
3 kind: RoleBinding
4 metadata:
5 name: read-pods
6 namespace: default
7 subjects:
8 - kind: User
9 name: jane # 名称大小写敏感
10 apiGroup: rbac.authorization.k8s.io
11 roleRef:
12 kind: Role #this must be Role or ClusterRole
13 name: pod-reader # 这里的名称必须与你想要绑定的 Role 或 ClusterRole 名称一致
14 apiGroup: rbac.authorization.k8s.io
复制代码
roleRef 里的内容决定了实际创建绑定的方法。kind 可以是 Role 或 ClusterRole,name 是你要引用的 Role 或 ClusterRole 的名称。

RoleBinding 也可以引用 ClusterRole,这可以允许管理者在 整个集群中定义一组通用的角色,然后在多个命名空间中重用它们。

RoleBinding示例2
下面的例子,RoleBinding 引用的是 ClusterRole, “dave” (subjects区分大小写)将只可以读取在”development” 名称空间( RoleBinding 的命名空间)中的”secrets” 。

复制代码
1 apiVersion: rbac.authorization.k8s.io/v1
2 # 这个角色绑定允许 “dave” 用户在 “development” 命名空间中有读取 secrets 的权限。
3 kind: RoleBinding
4 metadata:
5 name: read-secrets
6 namespace: development # 这里只授予 “development” 命名空间的权限。
7 subjects:
8 - kind: User
9 name: dave # 名称区分大小写
10 apiGroup: rbac.authorization.k8s.io
11 roleRef:
12 kind: ClusterRole
13 name: secret-reader
14 apiGroup: rbac.authorization.k8s.io
复制代码
最后,ClusterRoleBinding 可用来在集群级别并对所有命名空间执行授权。

ClusterRoleBinding示例
复制代码
1 apiVersion: rbac.authorization.k8s.io/v1
2 # 这个集群角色绑定允许 “manager” 组中的任何用户读取任意命名空间中 “secrets”。
3 kind: ClusterRoleBinding
4 metadata:
5 name: read-secrets-global
6 subjects:
7 - kind: Group
8 name: manager # 名称区分大小写
9 apiGroup: rbac.authorization.k8s.io
10 roleRef:
11 kind: ClusterRole
12 name: secret-reader
13 apiGroup: rbac.authorization.k8s.io
复制代码
当我们创建binding后,则不能修改binding所引用的Role或ClusterRole。尝试修改会导致验证错误;如果要改变binding的roleRef,那么应该删除该binding对象并且创建一个新的用来替换原来的。

Referring to resources【资源引用】
Kubernetes集群内一些资源一般以其名称字符串来表示,这些字符串一般会在API的URL地址中出现;同时某些资源也会包含子资源,例如pod的logs资源就属于pods的子资源,API中URL样例如下:

GET /api/v1/namespaces/{namespace}/pods/{name}/log
在这种情况下,”pods” 是有名称空间的资源,而 “log” 是 pods 的子资源。在 RBAC 角色中,使用”/“分隔资源和子资源。

允许一个主体(subject)要同时读取 pods 和 pod logs,你可以这么写:

复制代码
1 apiVersion: rbac.authorization.k8s.io/v1
2 kind: Role
3 metadata:
4 namespace: default
5 name: pod-and-pod-logs-reader
6 rules:
7 - apiGroups: [""]
8 resources: [“pods”, “pods/log”]
9 verbs: [“get”, “list”]
复制代码
对于某些请求,也可以通过 resourceNames 列表按名称引用资源。

例如:在指定时,可以将请求类型限制到资源的单个实例。限制只可以 “get” 和 “update” 到单个configmap,则可以这么写:

复制代码
1 apiVersion: rbac.authorization.k8s.io/v1
2 kind: Role
3 metadata:
4 namespace: default
5 name: configmap-updater
6 rules:
7 - apiGroups: [""]
8 resources: [“configmaps”]
9 resourceNames: [“my-configmap”]
10 verbs: [“update”, “get”]
复制代码
需要注意的是,create 请求不能被 resourceName 限制,因为在鉴权时还不知道对象名称。 另一个例外是 deletecollection。

Referring to subjects【主体引用】
RoleBinding或ClusterRoleBinding绑定一个role到主体(subjects)。主体(subjects)可以是groups,users或ServiceAccounts。

Kubernetes将用户名表示为字符串。这些可以是:普通名称,比如“alice” ;邮件风格的名字,比如“bob@example.com” ;或表示为字符串的数字用户id。

注意:前缀 system: 是保留给Kubernetes系统使用的,因此应该确保不会出现名称以system: 开头的用户或组。除了这个特殊的前缀,RBAC授权系统不要求用户名使用任何格式。

ServiceAccounts具有前缀为system:serviceaccount: 的名称,属于具有前缀为system:serviceaccounts:的名称的组。

RoleBinding的示例
下面的示例只是展示 RoleBinding 中 subjects 的部分。

用户的名称为 “alice@example.com” :

1 subjects:
2 - kind: User
3 name: “alice@example.com”
4 apiGroup: rbac.authorization.k8s.io

组的名称为 “frontend-admins” :

复制代码
1 subjects:
2 - kind: Group
3 name: “frontend-admins”
4 apiGroup: rbac.authorization.k8s.io
复制代码

默认service account在 kube-system 命名空间中:

1 subjects:
2 - kind: ServiceAccount
3 name: default
4 namespace: kube-system

在名称为 “qa” 命名空间中所有的服务账号:

1 subjects:
2 - kind: Group
3 name: system:serviceaccounts:qa
4 apiGroup: rbac.authorization.k8s.io

在任意名称空间的所有service accounts:

复制代码
1 subjects:
2 - kind: Group
3 name: system:serviceaccounts
4 apiGroup: rbac.authorization.k8s.io
复制代码

所有认证过的用户(版本 1.5+):

1 subjects:
2 - kind: Group
3 name: system:authenticated
4 apiGroup: rbac.authorization.k8s.io

所有未认证的用户(版本 1.5+):

复制代码
1 subjects:
2 - kind: Group
3 name: system:unauthenticated
4 apiGroup: rbac.authorization.k8s.io
复制代码

所有用户 (版本 1.5+):

复制代码
1 subjects:
2 - kind: Group
3 name: system:authenticated
4 apiGroup: rbac.authorization.k8s.io
5 - kind: Group
6 name: system:unauthenticated
7 apiGroup: rbac.authorization.k8s.io
复制代码

准入控制
准入控制是API Server的插件集合,通过添加不同的插件,实现额外的准入控制规则。甚至于API Server的一些主要的功能都需要通过Admission Controllers实现,比如:ServiceAccount。

查看哪些插件是默认启用的:

kube-apiserver -h | grep enable-admission-plugins

在 1.17 中,它们是:

复制代码
1 NamespaceLifecycle, LimitRanger, ServiceAccount,
2 TaintNodesByCondition, Priority, DefaultTolerationSeconds,
3 DefaultStorageClass, StorageObjectInUseProtection, PersistentVolumeClaimResize,
4 MutatingAdmissionWebhook, ValidatingAdmissionWebhook, RuntimeClass, ResourceQuota
亚马逊测评 www.yisuping.com

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐