首页 > 解决方案 > Kubernetes 角色应该授予对所有资源的访问权限,但它会忽略某些资源

问题描述

该角色namespace-limited应该对命名空间内的所有资源(指定 API 组的)具有完全访问权限。我的角色清单如下所示:

kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: namespace-limited
  namespace: restricted-xample
rules:
- apiGroups:
  - core
  - apps
  - batch
  - networking.k8s.io
  resources: ["*"] # asterisk to grant access to all resources of the specified api groups
  verbs: ["*"]

我使用 RoleBinding 将 Role 与 ServiceAccount 相关联,但不幸的是,此 ServiceAccount 无权访问PodService、和Resources。这些资源都是API 组的一部分。但是,所有其他常见的工作负载都可以工作。这是为什么?SecretConfigMapEndpointcore

标签: kuberneteskubectl

解决方案


核心组,也称为遗留组,位于 REST 路径/api/v1并使用apiVersion: v1

您需要""用于核心 API 组。

apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  namespace: restricted-xample
  name: namespace-limited
rules:
- apiGroups: ["", "apps", "batch", "networking.k8s.io"] # "" indicates the core API group
  resources: ["*"]
  verbs: ["*"]

要测试服务帐户的权限,请使用以下命令

kubectl auth can-i get pods --as=system:serviceaccount:restricted-xample:default -n restricted-xample 
kubectl auth can-i get secrets --as=system:serviceaccount:restricted-xample:default -n restricted-xample 
kubectl auth can-i get configmaps --as=system:serviceaccount:restricted-xample:default -n restricted-xample
kubectl auth can-i get endpoints --as=system:serviceaccount:restricted-xample:default -n restricted-xample 

推荐阅读