使用 RBAC 授权
基于角色的访问控制 (RBAC) 是一种根据组织内单个用户的角色来规定对计算机或网络资源的访问权限的方法。
RBAC 鉴权使用 rbac.authorization.k8s.io
API 组 来驱动鉴权决策,从而允许你通过 Kubernetes API 动态配置策略。
要启用 RBAC,请在启动 API 服务器 时使用 --authorization-config
标志,并将其值设置为包含 RBAC
鉴权器的文件;例如
apiVersion: apiserver.config.k8s.io/v1
kind: AuthorizationConfiguration
authorizers:
...
- type: RBAC
...
或者,在启动 API 服务器 时,使用 --authorization-mode
标志,并将其值设置为包含 RBAC
的逗号分隔列表;例如
kube-apiserver --authorization-mode=...,RBAC --other-options --more-options
API 对象
RBAC API 声明了四种 Kubernetes 对象:Role、ClusterRole、RoleBinding 和 ClusterRoleBinding。你可以使用 kubectl
等工具描述或修改 RBAC 对象,就像操作任何其他 Kubernetes 对象一样。
注意
这些对象在设计上强制实施访问限制。如果你在学习过程中对集群进行更改,请参阅权限提升预防和引导,了解这些限制如何阻止你进行某些更改。Role 与 ClusterRole
RBAC 的 Role 或 ClusterRole 包含表示一组权限的规则。权限是纯粹的加性(没有“拒绝”规则)。
Role 总是在特定的命名空间内设置权限;创建 Role 时,必须指定它所属的命名空间。
相比之下,ClusterRole 是一个非命名空间资源。这些资源之所以有不同的名称(Role 和 ClusterRole),是因为 Kubernetes 对象必须要么属于命名空间,要么不属于命名空间;不能两者兼是。
ClusterRole 有多种用途。你可以使用 ClusterRole 来:
- 定义对命名空间资源的权限,并在单个或多个命名空间内被授予访问权限
- 定义对命名空间资源的权限,并在所有命名空间中被授予访问权限
- 定义对集群范围资源的权限
如果要在命名空间内定义角色,请使用 Role;如果要在集群范围内定义角色,请使用 ClusterRole。
Role 示例
以下是“default”命名空间中的一个 Role 示例,可用于授予对 Pod 的读取访问权限
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"]
ClusterRole 示例
ClusterRole 可用于授予与 Role 相同的权限。由于 ClusterRole 是集群范围的,你还可以使用它们授予对以下对象的访问权限:
集群范围资源(例如 节点)
非资源端点(例如
/healthz
)所有命名空间中的命名空间资源(例如 Pods)
例如:你可以使用 ClusterRole 允许特定用户运行
kubectl get pods --all-namespaces
以下是一个 ClusterRole 示例,可用于授予在任何特定命名空间或所有命名空间中读取 Secret 的访问权限(取决于它是如何绑定的)
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
# "namespace" omitted since ClusterRoles are not namespaced
name: secret-reader
rules:
- apiGroups: [""]
#
# at the HTTP level, the name of the resource for accessing Secret
# objects is "secrets"
resources: ["secrets"]
verbs: ["get", "watch", "list"]
Role 或 ClusterRole 对象的名称必须是有效的路径段名称。
RoleBinding 与 ClusterRoleBinding
角色绑定将角色中定义的权限授予一个或一组用户。它包含一个主体列表(用户、组或 ServiceAccount),以及对被授予角色的引用。RoleBinding 授予特定命名空间内的权限,而 ClusterRoleBinding 授予集群范围内的访问权限。
RoleBinding 可以引用同一命名空间中的任何 Role。另外,RoleBinding 可以引用 ClusterRole,并将该 ClusterRole 绑定到 RoleBinding 所在的命名空间。如果你想将 ClusterRole 绑定到集群中的所有命名空间,则使用 ClusterRoleBinding。
RoleBinding 或 ClusterRoleBinding 对象的名称必须是有效的路径段名称。
RoleBinding 示例
以下是一个 RoleBinding 示例,它将“pod-reader” Role 授予“default”命名空间内的用户“jane”。这允许“jane”读取“default”命名空间中的 Pod。
apiVersion: rbac.authorization.k8s.io/v1
# This role binding allows "jane" to read pods in the "default" namespace.
# You need to already have a Role named "pod-reader" in that namespace.
kind: RoleBinding
metadata:
name: read-pods
namespace: default
subjects:
# You can specify more than one "subject"
- kind: User
name: jane # "name" is case sensitive
apiGroup: rbac.authorization.k8s.io
roleRef:
# "roleRef" specifies the binding to a Role / ClusterRole
kind: Role #this must be Role or ClusterRole
name: pod-reader # this must match the name of the Role or ClusterRole you wish to bind to
apiGroup: rbac.authorization.k8s.io
RoleBinding 也可以引用 ClusterRole,以便将该 ClusterRole 中定义的权限授予 RoleBinding 所在命名空间内的资源。这种引用方式允许你在集群范围内定义一组通用角色,然后在多个命名空间中重复使用它们。
例如,即使下面的 RoleBinding 引用了 ClusterRole,“dave”(主体,区分大小写)也只能读取“development”命名空间中的 Secret,因为 RoleBinding 的命名空间(在其元数据中)是“development”。
apiVersion: rbac.authorization.k8s.io/v1
# This role binding allows "dave" to read secrets in the "development" namespace.
# You need to already have a ClusterRole named "secret-reader".
kind: RoleBinding
metadata:
name: read-secrets
#
# The namespace of the RoleBinding determines where the permissions are granted.
# This only grants permissions within the "development" namespace.
namespace: development
subjects:
- kind: User
name: dave # Name is case sensitive
apiGroup: rbac.authorization.k8s.io
roleRef:
kind: ClusterRole
name: secret-reader
apiGroup: rbac.authorization.k8s.io
ClusterRoleBinding 示例
要授予整个集群范围内的权限,你可以使用 ClusterRoleBinding。下面的 ClusterRoleBinding 允许“manager”组中的任何用户读取任何命名空间中的 Secret。
apiVersion: rbac.authorization.k8s.io/v1
# This cluster role binding allows anyone in the "manager" group to read secrets in any namespace.
kind: ClusterRoleBinding
metadata:
name: read-secrets-global
subjects:
- kind: Group
name: manager # Name is case sensitive
apiGroup: rbac.authorization.k8s.io
roleRef:
kind: ClusterRole
name: secret-reader
apiGroup: rbac.authorization.k8s.io
创建绑定后,你无法更改它所引用的 Role 或 ClusterRole。如果你尝试更改绑定的 roleRef
,将会收到验证错误。如果确实想要更改绑定的 roleRef
,则需要移除该绑定对象并创建新的替代对象。
有两个原因导致此限制:
- 使
roleRef
不可变,允许授予某人对现有绑定对象进行update
的权限,以便他们可以管理主体列表,而不能更改授予给这些主体的角色。 - 绑定到不同的角色本质上是不同的绑定。要求删除/重新创建绑定以更改
roleRef
可确保绑定中的完整主体列表有意被授予新角色(而不是只启用或意外地修改 roleRef,而不验证所有现有主体是否都应获得新角色的权限)。
kubectl auth reconcile
命令行工具用于创建或更新包含 RBAC 对象的清单文件,并处理在需要更改绑定所引用角色时删除和重新创建绑定对象。有关详细信息,请参见命令用法和示例。
引用资源
在 Kubernetes API 中,大多数资源通过其对象名称的字符串表示形式来表示和访问,例如 Pod 对应的 pods
。RBAC 使用与相关 API 端点 URL 中出现的完全相同的名称来引用资源。某些 Kubernetes API 涉及子资源,例如 Pod 的日志。请求 Pod 的日志看起来像:
GET /api/v1/namespaces/{namespace}/pods/{name}/log
在这种情况下,pods
是 Pod 资源的命名空间资源,log
是 pods
的一个子资源。要在 RBAC Role 中表示这一点,使用斜杠 (/
) 分隔资源和子资源。要允许主体读取 pods
并访问每个 Pod 的 log
子资源,你需要这样写:
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
namespace: default
name: pod-and-pod-logs-reader
rules:
- apiGroups: [""]
resources: ["pods", "pods/log"]
verbs: ["get", "list"]
你还可以通过 resourceNames
列表按名称引用特定请求的资源。指定后,请求可以被限制到资源的单个实例。以下示例将其主体仅限于 get
或 update
名为 my-configmap
的 ConfigMap。
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
namespace: default
name: configmap-updater
rules:
- apiGroups: [""]
#
# at the HTTP level, the name of the resource for accessing ConfigMap
# objects is "configmaps"
resources: ["configmaps"]
resourceNames: ["my-configmap"]
verbs: ["update", "get"]
注意
你不能按资源名称限制create
或 deletecollection
请求。对于 create
,此限制是因为新对象的名称在鉴权时可能未知。如果按 resourceName 限制 list
或 watch
,客户端在其 list
或 watch
请求中必须包含匹配指定 resourceName 的 metadata.name
字段选择器才能获得授权。例如:kubectl get configmaps --field-selector=metadata.name=my-configmap
除了引用单个 resources
、apiGroups
和 verbs
,你还可以使用通配符 *
来引用所有此类对象。对于 nonResourceURLs
,可以使用通配符 *
进行后缀全局匹配。对于 resourceNames
,空集表示允许所有内容。以下示例允许对 example.com
API 组中的所有当前和未来的资源执行任何当前和未来的操作。这类似于内置的 cluster-admin
角色。
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
namespace: default
name: example.com-superuser # DO NOT USE THIS ROLE, IT IS JUST AN EXAMPLE
rules:
- apiGroups: ["example.com"]
resources: ["*"]
verbs: ["*"]
注意
在资源和动词条目中使用通配符可能导致授予对敏感资源的权限过于宽松。例如,如果添加了新的资源类型、新的子资源或新的自定义动词,通配符条目会自动授予访问权限,这可能是不希望的。应采用最小权限原则,使用特定的资源和动词,以确保只应用工作负载正常运行所需的权限。聚合 ClusterRole
你可以将多个 ClusterRole 聚合到一个组合的 ClusterRole 中。作为集群控制平面一部分运行的控制器会监视设置了 aggregationRule
的 ClusterRole 对象。aggregationRule
定义了一个标签选择器,控制器使用该选择器来匹配应合并到当前 ClusterRole 的 rules
字段中的其他 ClusterRole 对象。
注意
控制平面会覆盖你在聚合 ClusterRole 的rules
字段中手动指定的任何值。如果你想更改或添加规则,请在由 aggregationRule
选择的 ClusterRole
对象中进行。以下是一个聚合 ClusterRole 示例
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: monitoring
aggregationRule:
clusterRoleSelectors:
- matchLabels:
rbac.example.com/aggregate-to-monitoring: "true"
rules: [] # The control plane automatically fills in the rules
如果创建与现有聚合 ClusterRole 的标签选择器匹配的新 ClusterRole,则该更改将触发将新规则添加到聚合 ClusterRole 中。以下示例通过创建一个标记为 rbac.example.com/aggregate-to-monitoring: true
的 ClusterRole 来为“monitoring” ClusterRole 添加规则。
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: monitoring-endpoints
labels:
rbac.example.com/aggregate-to-monitoring: "true"
# When you create the "monitoring-endpoints" ClusterRole,
# the rules below will be added to the "monitoring" ClusterRole.
rules:
- apiGroups: [""]
resources: ["services", "endpointslices", "pods"]
verbs: ["get", "list", "watch"]
默认面向用户的角色使用 ClusterRole 聚合。这允许你作为集群管理员,为自定义资源(例如由 CustomResourceDefinition 或聚合 API 服务器提供服务的资源)包含规则,以扩展默认角色。
例如:以下 ClusterRole 允许“admin”和“edit”默认角色管理名为 CronTab 的自定义资源,而“view”角色只能对 CronTab 资源执行读取操作。可以假定 API 服务器看到的 URL 中 CronTab 对象名称为 "crontabs"
。
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: aggregate-cron-tabs-edit
labels:
# Add these permissions to the "admin" and "edit" default roles.
rbac.authorization.k8s.io/aggregate-to-admin: "true"
rbac.authorization.k8s.io/aggregate-to-edit: "true"
rules:
- apiGroups: ["stable.example.com"]
resources: ["crontabs"]
verbs: ["get", "list", "watch", "create", "update", "patch", "delete"]
---
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: aggregate-cron-tabs-view
labels:
# Add these permissions to the "view" default role.
rbac.authorization.k8s.io/aggregate-to-view: "true"
rules:
- apiGroups: ["stable.example.com"]
resources: ["crontabs"]
verbs: ["get", "list", "watch"]
Role 示例
以下示例是 Role 或 ClusterRole 对象的摘录,仅显示了 rules
部分。
允许在核心 API 组中读取 "pods"
资源。
rules:
- apiGroups: [""]
#
# at the HTTP level, the name of the resource for accessing Pod
# objects is "pods"
resources: ["pods"]
verbs: ["get", "list", "watch"]
允许在 "apps"
API 组中读取/写入 Deployment(在 HTTP 层面:URL 资源部分包含 "deployments"
的对象)
rules:
- apiGroups: ["apps"]
#
# at the HTTP level, the name of the resource for accessing Deployment
# objects is "deployments"
resources: ["deployments"]
verbs: ["get", "list", "watch", "create", "update", "patch", "delete"]
允许在核心 API 组中读取 Pod,以及在 "batch"
API 组中读取或写入 Job 资源。
rules:
- apiGroups: [""]
#
# at the HTTP level, the name of the resource for accessing Pod
# objects is "pods"
resources: ["pods"]
verbs: ["get", "list", "watch"]
- apiGroups: ["batch"]
#
# at the HTTP level, the name of the resource for accessing Job
# objects is "jobs"
resources: ["jobs"]
verbs: ["get", "list", "watch", "create", "update", "patch", "delete"]
允许读取名为“my-config”的 ConfigMap(必须与 RoleBinding 绑定以限制在单个命名空间中的单个 ConfigMap)
rules:
- apiGroups: [""]
#
# at the HTTP level, the name of the resource for accessing ConfigMap
# objects is "configmaps"
resources: ["configmaps"]
resourceNames: ["my-config"]
verbs: ["get"]
允许在核心组中读取资源 "nodes"
(因为 Node 是集群范围的,这必须在通过 ClusterRoleBinding 绑定的 ClusterRole 中才能生效)
rules:
- apiGroups: [""]
#
# at the HTTP level, the name of the resource for accessing Node
# objects is "nodes"
resources: ["nodes"]
verbs: ["get", "list", "watch"]
允许对非资源端点 /healthz
及所有子路径执行 GET 和 POST 请求(必须在通过 ClusterRoleBinding 绑定的 ClusterRole 中才能生效)
rules:
- nonResourceURLs: ["/healthz", "/healthz/*"] # '*' in a nonResourceURL is a suffix glob match
verbs: ["get", "post"]
引用主体
RoleBinding 或 ClusterRoleBinding 将角色绑定到主体。主体可以是组、用户或 ServiceAccount。
Kubernetes 将用户名表示为字符串。这些可以是:普通名称,例如“alice”;电子邮件风格的名称,例如“bob@example.com”;或表示为字符串的数字用户 ID。作为集群管理员,由你来配置身份认证模块,以便身份认证按你想要的格式生成用户名。
注意
前缀system:
保留供 Kubernetes 系统使用,因此应确保你的用户或组名称不会意外地以 system:
开头。除此特殊前缀外,RBAC 鉴权系统对用户名的格式没有要求。在 Kubernetes 中,身份认证器模块提供组信息。组与用户一样,表示为字符串,并且该字符串没有格式要求,除了前缀 system:
是保留的。
ServiceAccount 的名称以 system:serviceaccount:
为前缀,并属于名称以 system:serviceaccounts:
为前缀的组。
注意
system:serviceaccount:
(单数)是 ServiceAccount 用户名的前缀。system:serviceaccounts:
(复数)是 ServiceAccount 组的前缀。
RoleBinding 示例
以下示例是 RoleBinding
摘录,仅显示了 subjects
部分。
对于用户 alice@example.com
subjects:
- kind: User
name: "alice@example.com"
apiGroup: rbac.authorization.k8s.io
对于组 frontend-admins
subjects:
- kind: Group
name: "frontend-admins"
apiGroup: rbac.authorization.k8s.io
对于“kube-system”命名空间中的默认 ServiceAccount
subjects:
- kind: ServiceAccount
name: default
namespace: kube-system
对于“qa”命名空间中的所有 ServiceAccount
subjects:
- kind: Group
name: system:serviceaccounts:qa
apiGroup: rbac.authorization.k8s.io
对于所有命名空间中的所有 ServiceAccount
subjects:
- kind: Group
name: system:serviceaccounts
apiGroup: rbac.authorization.k8s.io
对于所有已认证用户
subjects:
- kind: Group
name: system:authenticated
apiGroup: rbac.authorization.k8s.io
对于所有未经认证用户
subjects:
- kind: Group
name: system:unauthenticated
apiGroup: rbac.authorization.k8s.io
对于所有用户
subjects:
- kind: Group
name: system:authenticated
apiGroup: rbac.authorization.k8s.io
- kind: Group
name: system:unauthenticated
apiGroup: rbac.authorization.k8s.io
默认角色和角色绑定
API 服务器会创建一组默认的 ClusterRole 和 ClusterRoleBinding 对象。其中许多带有 system:
前缀,表明该资源由集群控制平面直接管理。所有默认的 ClusterRole 和 ClusterRoleBinding 都带有 kubernetes.io/bootstrapping=rbac-defaults
标签。
注意
修改名称带有system:
前缀的 ClusterRole 和 ClusterRoleBinding 时要小心。修改这些资源可能导致集群无法正常工作。自动协调
每次启动时,API 服务器都会使用任何缺失的权限更新默认集群角色,并使用任何缺失的主体更新默认集群角色绑定。这允许集群修复意外修改,并有助于在新的 Kubernetes 发布版中权限和主体发生变化时,保持角色和角色绑定最新。
要选择退出此协调过程,请在默认集群角色或默认集群 RoleBinding 上将 rbac.authorization.kubernetes.io/autoupdate
注解设置为 false
。请注意,缺少默认权限和主体可能导致集群无法正常工作。
如果 RBAC 鉴权器处于活动状态,则自动协调默认启用。
API 发现角色
默认集群角色绑定授权未经认证和已认证用户读取被认为可以公开访问的 API 信息(包括 CustomResourceDefinition)。要禁用匿名未经认证访问,请在 API 服务器配置中添加 --anonymous-auth=false
标志。
要通过 kubectl
查看这些角色的配置,请运行:
kubectl get clusterroles system:discovery -o yaml
注意
如果你编辑该 ClusterRole,你的更改将在 API 服务器重启时通过自动协调被覆盖。为避免被覆盖,要么不要手动编辑该角色,要么禁用自动协调。默认 ClusterRole | 默认 ClusterRoleBinding | 描述 |
---|---|---|
system:basic-user | system:authenticated 组 | 允许用户对其自身的基本信息拥有只读访问权限。在 v1.14 之前,此角色也绑定到:system:unauthenticated默认启用。 |
system:discovery | system:authenticated 组 | 允许对发现 API 级别和协商 API 级别所需的 API 发现端点进行只读访问。在 v1.14 之前,此角色也绑定到:system:unauthenticated默认启用。 |
system:public-info-viewer | system:authenticated 和 system:unauthenticated 组 | 允许对集群中非敏感信息进行只读访问。在 Kubernetes v1.14 中引入。 |
面向用户的角色
某些默认 ClusterRole 没有 system:
前缀。这些是面向用户的角色。它们包括超级用户角色 (cluster-admin
)、旨在使用 ClusterRoleBinding 在集群范围内授予的角色,以及旨在使用 RoleBinding 在特定命名空间内授予的角色 (admin
、edit
、view
)。
面向用户的 ClusterRole 使用ClusterRole 聚合,允许管理员在这些 ClusterRole 上包含自定义资源的规则。要为 admin
、edit
或 view
角色添加规则,请创建一个带有一个或多个以下标签的 ClusterRole:
metadata:
labels:
rbac.authorization.k8s.io/aggregate-to-admin: "true"
rbac.authorization.k8s.io/aggregate-to-edit: "true"
rbac.authorization.k8s.io/aggregate-to-view: "true"
默认 ClusterRole | 默认 ClusterRoleBinding | 描述 |
---|---|---|
cluster-admin | system:masters 组 | 允许超级用户访问,可对任何资源执行任何操作。当用于 ClusterRoleBinding 时,它提供对集群中所有资源和所有命名空间的完全控制。当用于 RoleBinding 时,它提供对角色绑定所在命名空间中所有资源(包括命名空间本身)的完全控制。 |
admin | 无 | 允许管理权限,旨在通过 RoleBinding 在命名空间内授予。 如果在 RoleBinding 中使用,则允许对命名空间中的大多数资源进行读写访问,包括在命名空间内创建角色和角色绑定的能力。此角色不允许对资源配额或命名空间本身进行写访问。此角色也不允许在 Kubernetes v1.22+ 创建的集群中对 EndpointSlice 进行写访问。更多信息请参见“EndpointSlice 写访问”部分。 |
edit | 无 | 允许对命名空间中的大多数对象进行读写访问。 此角色不允许查看或修改角色或角色绑定。但是,此角色允许访问 Secret 并以命名空间中的任何 ServiceAccount 身份运行 Pod,因此可以用来获得命名空间中任何 ServiceAccount 的 API 访问级别。此角色也不允许在 Kubernetes v1.22+ 创建的集群中对 EndpointSlice 进行写访问。更多信息请参见“EndpointSlice 写访问”部分。 |
view | 无 | 允许只读访问,查看命名空间中的大多数对象。它不允许查看角色或角色绑定。 此角色不允许查看 Secret,因为读取 Secret 的内容可以获取命名空间中的 ServiceAccount 凭据,从而允许以命名空间中的任何 ServiceAccount 身份进行 API 访问(一种权限提升形式)。 |
核心组件角色
默认 ClusterRole | 默认 ClusterRoleBinding | 描述 |
---|---|---|
system:kube-scheduler | system:kube-scheduler 用户 | 允许访问 调度器 组件所需的资源。 |
system:volume-scheduler | system:kube-scheduler 用户 | 允许访问 kube-scheduler 组件所需的卷资源。 |
system:kube-controller-manager | system:kube-controller-manager 用户 | 允许访问 控制器管理器 组件所需的资源。单个控制器所需的权限在控制器角色中有详细说明。 |
system:node | 无 | 允许访问 kubelet 所需的资源,包括对所有 Secret 的读访问和对所有 Pod 状态对象的写访问。 你应该使用节点鉴权器和NodeRestriction 准入插件,而不是使用system:node角色,并根据调度到其上运行的 Pod 来授予 kubelet API 访问权限。 该system:node角色仅为兼容从 v1.8 之前版本升级的 Kubernetes 集群而存在。 |
system:node-proxier | system:kube-proxy 用户 | 允许访问 kube-proxy 组件所需的资源。 |
其他组件角色
默认 ClusterRole | 默认 ClusterRoleBinding | 描述 |
---|---|---|
system:auth-delegator | 无 | 允许委托身份认证和鉴权检查。附加组件 API 服务器通常使用此角色进行统一的身份认证和鉴权。 |
system:heapster | 无 | Heapster 组件的角色(已弃用)。 |
system:kube-aggregator | 无 | kube-aggregator 组件的角色。 |
system:kube-dns | kube-dns ServiceAccount 在 kube-system 命名空间中 | kube-dns 组件的角色。 |
system:kubelet-api-admin | 无 | 允许对 Kubelet API 进行完全访问。 |
system:node-bootstrapper | 无 | 允许访问执行Kubelet TLS 引导所需的资源。 |
system:node-problem-detector | 无 | node-problem-detector 组件的角色。 |
system:persistent-volume-provisioner | 无 | 允许访问大多数动态卷供应器所需的资源。 |
system:monitoring | system:monitoring 组 | 允许读取控制平面监控端点(即 kube-apiserver 存活探针和就绪探针端点(/healthz, /livez, /readyz),单独的健康检查端点(/healthz/*, /livez/*, /readyz/*),以及/metrics)。请注意,单独的健康检查端点和指标端点可能暴露敏感信息。 |
内置控制器角色
Kubernetes 控制器管理器运行 Kubernetes 控制平面中内置的控制器。当使用 --use-service-account-credentials
参数调用时,kube-controller-manager 使用单独的服务账号启动每个控制器。每个内置控制器都有对应的角色,前缀为 system:controller:
。如果控制器管理器不是使用 --use-service-account-credentials
参数启动的,它将使用自己的凭据运行所有控制循环,该凭据必须被授予所有相关的角色。这些角色包括:
system:controller:attachdetach-controller
system:controller:certificate-controller
system:controller:clusterrole-aggregation-controller
system:controller:cronjob-controller
system:controller:daemon-set-controller
system:controller:deployment-controller
system:controller:disruption-controller
system:controller:endpoint-controller
system:controller:expand-controller
system:controller:generic-garbage-collector
system:controller:horizontal-pod-autoscaler
system:controller:job-controller
system:controller:namespace-controller
system:controller:node-controller
system:controller:persistent-volume-binder
system:controller:pod-garbage-collector
system:controller:pv-protection-controller
system:controller:pvc-protection-controller
system:controller:replicaset-controller
system:controller:replication-controller
system:controller:resourcequota-controller
system:controller:root-ca-cert-publisher
system:controller:route-controller
system:controller:service-account-controller
system:controller:service-controller
system:controller:statefulset-controller
system:controller:ttl-controller
权限提升防护与引导
RBAC API 通过限制用户编辑角色或角色绑定来防止权限提升。由于这是在 API 层面强制执行的,因此即使在未使用 RBAC 授权器时也适用。
创建或更新角色的限制
只有满足以下至少一个条件时,你才能创建/更新角色
- 你已拥有该角色包含的所有权限,且权限范围与要修改的对象相同(对于 ClusterRole 为集群范围,对于 Role 为相同命名空间内或集群范围)。
- 你被明确授予了在
rbac.authorization.k8s.io
API 组中对roles
或clusterroles
资源执行escalate
动词(操作)的权限。
例如,如果 user-1
不具备列出集群范围 Secrets 的能力,则他们无法创建包含该权限的 ClusterRole。要允许用户创建/更新角色
- 根据需要,授予他们允许创建/更新 Role 或 ClusterRole 对象的角色。
- 授予他们权限以允许在他们创建/更新的角色中包含特定的权限
- 隐式地,通过授予他们那些权限(如果他们试图创建或修改一个包含他们自己未被授予的权限的 Role 或 ClusterRole,API 请求将被拒绝)
- 或者显式地允许在
Role
或ClusterRole
中指定任何权限,方法是授予他们在rbac.authorization.k8s.io
API 组中对roles
或clusterroles
资源执行escalate
动词(操作)的权限
创建或更新角色绑定的限制
只有当你已拥有该引用的角色所包含的所有权限(与角色绑定在同一范围)或 你已被授权对该引用的角色执行 bind
动词(操作)时,你才能创建/更新角色绑定。例如,如果 user-1
不具备列出集群范围 Secrets 的能力,则他们无法创建 ClusterRoleBinding 指向授予该权限的角色。要允许用户创建/更新角色绑定
- 根据需要,授予他们允许创建/更新 RoleBinding 或 ClusterRoleBinding 对象的角色。
- 授予他们绑定特定角色所需的权限
- 隐式地,通过授予他们该角色中包含的权限。
- 显式地,通过授予他们对特定 Role(或 ClusterRole)执行
bind
动词(操作)的权限。
例如,这个 ClusterRole 和 RoleBinding 将允许 user-1
在命名空间 user-1-namespace
中授予其他用户 admin
、edit
和 view
角色
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: role-grantor
rules:
- apiGroups: ["rbac.authorization.k8s.io"]
resources: ["rolebindings"]
verbs: ["create"]
- apiGroups: ["rbac.authorization.k8s.io"]
resources: ["clusterroles"]
verbs: ["bind"]
# omit resourceNames to allow binding any ClusterRole
resourceNames: ["admin","edit","view"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: role-grantor-binding
namespace: user-1-namespace
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: role-grantor
subjects:
- apiGroup: rbac.authorization.k8s.io
kind: User
name: user-1
在引导初始角色和角色绑定时,初始用户需要授予他们自己尚未拥有的权限。要引导初始角色和角色绑定
- 使用具有 "system:masters" 组的凭据,该组通过默认绑定被绑定到 "cluster-admin" 超级用户角色。
命令行工具
kubectl create role
创建一个 Role 对象,该对象定义了单一命名空间内的权限。示例:
创建一个名为 "pod-reader" 的 Role,允许用户对 Pod 执行
get
、watch
和list
操作kubectl create role pod-reader --verb=get --verb=list --verb=watch --resource=pods
创建一个名为 "pod-reader" 的 Role,并指定
resourceNames
kubectl create role pod-reader --verb=get --resource=pods --resource-name=readablepod --resource-name=anotherpod
创建一个名为 "foo" 的 Role,并指定
apiGroups
kubectl create role foo --verb=get,list,watch --resource=replicasets.apps
创建一个名为 "foo" 的 Role,并包含子资源权限
kubectl create role foo --verb=get,list,watch --resource=pods,pods/status
创建一个名为 "my-component-lease-holder" 的 Role,包含对特定名称资源进行
get
/update
操作的权限kubectl create role my-component-lease-holder --verb=get,list,watch,update --resource=lease --resource-name=my-component
kubectl create clusterrole
创建一个 ClusterRole。示例:
创建一个名为 "pod-reader" 的 ClusterRole,允许用户对 Pod 执行
get
、watch
和list
操作kubectl create clusterrole pod-reader --verb=get,list,watch --resource=pods
创建一个名为 "pod-reader" 的 ClusterRole,并指定
resourceNames
kubectl create clusterrole pod-reader --verb=get --resource=pods --resource-name=readablepod --resource-name=anotherpod
创建一个名为 "foo" 的 ClusterRole,并指定
apiGroups
kubectl create clusterrole foo --verb=get,list,watch --resource=replicasets.apps
创建一个名为 "foo" 的 ClusterRole,并包含子资源权限
kubectl create clusterrole foo --verb=get,list,watch --resource=pods,pods/status
创建一个名为 "foo" 的 ClusterRole,并指定
nonResourceURL
kubectl create clusterrole "foo" --verb=get --non-resource-url=/logs/*
创建一个名为 "monitoring" 的 ClusterRole,并指定
aggregationRule
kubectl create clusterrole monitoring --aggregation-rule="rbac.example.com/aggregate-to-monitoring=true"
kubectl create rolebinding
在特定命名空间内授予 Role 或 ClusterRole。示例:
在命名空间 "acme" 内,将 "admin" ClusterRole 中的权限授予名为 "bob" 的用户
kubectl create rolebinding bob-admin-binding --clusterrole=admin --user=bob --namespace=acme
在命名空间 "acme" 内,将 "view" ClusterRole 中的权限授予命名空间 "acme" 中名为 "myapp" 的服务账号
kubectl create rolebinding myapp-view-binding --clusterrole=view --serviceaccount=acme:myapp --namespace=acme
在命名空间 "acme" 内,将 "view" ClusterRole 中的权限授予命名空间 "myappnamespace" 中名为 "myapp" 的服务账号
kubectl create rolebinding myappnamespace-myapp-view-binding --clusterrole=view --serviceaccount=myappnamespace:myapp --namespace=acme
kubectl create clusterrolebinding
在整个集群范围内(所有命名空间)授予 ClusterRole。示例:
在整个集群范围内,将 "cluster-admin" ClusterRole 中的权限授予名为 "root" 的用户
kubectl create clusterrolebinding root-cluster-admin-binding --clusterrole=cluster-admin --user=root
在整个集群范围内,将 "system:node-proxier" ClusterRole 中的权限授予名为 "system:kube-proxy" 的用户
kubectl create clusterrolebinding kube-proxy-binding --clusterrole=system:node-proxier --user=system:kube-proxy
在整个集群范围内,将 "view" ClusterRole 中的权限授予命名空间 "acme" 中名为 "myapp" 的服务账号
kubectl create clusterrolebinding myapp-view-binding --clusterrole=view --serviceaccount=acme:myapp
kubectl auth reconcile
根据清单文件创建或更新 rbac.authorization.k8s.io/v1
API 对象。
创建缺失的对象,对于命名空间范围的对象,如果需要也会创建包含它们的命名空间。
更新现有角色以包含输入对象中的权限,如果指定了 --remove-extra-permissions
,则删除额外的权限。
更新现有绑定以包含输入对象中的主体,如果指定了 --remove-extra-subjects
,则删除额外的主体。
示例:
测试应用 RBAC 对象清单文件,显示将进行的更改
kubectl auth reconcile -f my-rbac-rules.yaml --dry-run=client
应用 RBAC 对象清单文件,保留任何额外的权限(在角色中)和任何额外的主体(在绑定中)
kubectl auth reconcile -f my-rbac-rules.yaml
应用 RBAC 对象清单文件,删除任何额外的权限(在角色中)和任何额外的主体(在绑定中)
kubectl auth reconcile -f my-rbac-rules.yaml --remove-extra-subjects --remove-extra-permissions
服务账号权限
默认的 RBAC 策略授予控制平面组件、节点和控制器有限范围的权限,但对 kube-system
命名空间外的服务账号授予没有任何权限(API 发现角色授予的权限除外)。
这允许你根据需要将特定角色授予特定 ServiceAccount。细粒度的角色绑定提供了更高的安全性,但需要更多的管理工作。更广泛的授权可能会给予 ServiceAccount 不必要的(和可能升级的)API 访问权限,但更容易管理。
按安全性从高到低排序,方法如下:
将角色授予应用程序特定的服务账号(最佳实践)
这要求应用程序在其 Pod 规范中指定
serviceAccountName
,并且该服务账号已被创建(通过 API、应用程序清单、kubectl create serviceaccount
等)。例如,在 "my-namespace" 内将只读权限授予 "my-sa" 服务账号
kubectl create rolebinding my-sa-view \ --clusterrole=view \ --serviceaccount=my-namespace:my-sa \ --namespace=my-namespace
将角色授予命名空间中的 "default" 服务账号
如果应用程序未指定
serviceAccountName
,它将使用 "default" 服务账号。注意
授予 "default" 服务账号的权限可供命名空间中任何未指定serviceAccountName
的 Pod 使用。例如,在 "my-namespace" 内将只读权限授予 "default" 服务账号
kubectl create rolebinding default-view \ --clusterrole=view \ --serviceaccount=my-namespace:default \ --namespace=my-namespace
许多插件以
kube-system
命名空间中的 "default" 服务账号身份运行。为了允许这些插件以超级用户权限运行,可以将 cluster-admin 权限授予kube-system
命名空间中的 "default" 服务账号。注意
启用此功能意味着kube-system
命名空间包含授予对集群 API 超级用户访问权限的 Secrets。kubectl create clusterrolebinding add-on-cluster-admin \ --clusterrole=cluster-admin \ --serviceaccount=kube-system:default
将角色授予命名空间中的所有服务账号
如果你希望命名空间中的所有应用程序都拥有某个角色,无论它们使用哪个服务账号,你可以将角色授予该命名空间的服务账号组。
例如,在 "my-namespace" 内将只读权限授予该命名空间中的所有服务账号
kubectl create rolebinding serviceaccounts-view \ --clusterrole=view \ --group=system:serviceaccounts:my-namespace \ --namespace=my-namespace
将有限角色授予集群范围内的所有服务账号(不推荐)
如果你不想按命名空间管理权限,可以将集群范围的角色授予所有服务账号。
例如,在所有命名空间中将只读权限授予集群中的所有服务账号
kubectl create clusterrolebinding serviceaccounts-view \ --clusterrole=view \ --group=system:serviceaccounts
将超级用户访问权限授予集群范围内的所有服务账号(强烈不推荐)
如果你完全不关心权限划分,可以将超级用户访问权限授予所有服务账号。
警告
这将允许任何应用程序完全访问你的集群,并且还授予任何对 Secrets 具有读访问权限(或创建任何 Pod 的能力)的用户完全访问你的集群的权限。kubectl create clusterrolebinding serviceaccounts-cluster-admin \ --clusterrole=cluster-admin \ --group=system:serviceaccounts
EndpointSlices 的写访问权限
在 Kubernetes v1.22 之前创建的 Kubernetes 集群,其聚合的 "edit" 和 "admin" 角色中包含对 EndpointSlices(以及现在已弃用的 Endpoints API)的写访问权限。作为对 CVE-2021-25740 的缓解措施,使用 Kubernetes v1.22 或更高版本创建的集群,其聚合角色中不包含此访问权限。
已升级到 Kubernetes v1.22 的现有集群不受此变更影响。CVE 公告中包含了在现有集群中限制此访问权限的指导。
如果你希望新集群在聚合角色中保留此级别的访问权限,可以创建以下 ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
annotations:
kubernetes.io/description: |-
Add endpoints write permissions to the edit and admin roles. This was
removed by default in 1.22 because of CVE-2021-25740. See
https://issue.k8s.io/103675. This can allow writers to direct LoadBalancer
or Ingress implementations to expose backend IPs that would not otherwise
be accessible, and can circumvent network policies or security controls
intended to prevent/isolate access to those backends.
EndpointSlices were never included in the edit or admin roles, so there
is nothing to restore for the EndpointSlice API.
labels:
rbac.authorization.k8s.io/aggregate-to-edit: "true"
name: custom:aggregate-to-edit:endpoints # you can change this if you wish
rules:
- apiGroups: [""]
resources: ["endpoints"]
verbs: ["create", "delete", "deletecollection", "patch", "update"]
从 ABAC 升级
最初运行旧版本 Kubernetes 的集群通常使用宽松的 ABAC 策略,包括授予所有服务账号完整的 API 访问权限。
默认的 RBAC 策略授予控制平面组件、节点和控制器有限范围的权限,但对 kube-system
命名空间外的服务账号授予没有任何权限(API 发现角色授予的权限除外)。
虽然更加安全,但这可能会对期望自动获得 API 权限的现有工作负载造成干扰。以下是管理此过渡的两种方法:
并行授权器
同时运行 RBAC 和 ABAC 授权器,并指定一个包含旧版 ABAC 策略的策略文件
--authorization-mode=...,RBAC,ABAC --authorization-policy-file=mypolicy.json
详细解释第一个命令行选项:如果之前的授权器(例如 Node)拒绝了请求,则 RBAC 授权器尝试授权该 API 请求。如果 RBAC 也拒绝了该 API 请求,则运行 ABAC 授权器。这意味着任何由 RBAC *或* ABAC 策略允许的请求都将被允许。
当 kube-apiserver 运行时,如果 RBAC 组件的日志级别为 5 或更高(--vmodule=rbac*=5
或 --v=5
),你可以在 API 服务器日志中看到 RBAC 拒绝信息(前缀为 RBAC
)。你可以使用这些信息来确定需要将哪些角色授予哪些用户、组或服务账号。
一旦你已将角色授予服务账号,并且工作负载运行正常,服务器日志中没有 RBAC 拒绝消息,你就可以移除 ABAC 授权器了。
宽松的 RBAC 权限
你可以使用 RBAC 角色绑定复制宽松的 ABAC 策略。
警告
以下策略允许**所有**服务账号充当集群管理员。在容器中运行的任何应用程序都会自动接收服务账号凭据,并且可以对 API 执行任何操作,包括查看 secrets 和修改权限。不推荐此策略。
kubectl create clusterrolebinding permissive-binding \
--clusterrole=cluster-admin \
--user=admin \
--user=kubelet \
--group=system:serviceaccounts
在过渡到使用 RBAC 后,你应该调整集群的访问控制,以确保其满足你的信息安全需求。