【问题标题】:Trying to create a namespace in an AWS EKS cluster with kubectl - Getting: Error from server (Forbidden): namespaces is forbidden尝试使用 kubectl 在 AWS EKS 集群中创建命名空间 - 获取:来自服务器的错误(禁止):命名空间被禁止
【发布时间】:2021-02-28 03:26:28
【问题描述】:

我正在尝试在 AWS EKS 集群中创建命名空间并不断收到错误消息。

我可以使用默认命名空间做任何我想做的事情,但是当我尝试创建一个新的命名空间名称时,我被禁止了。

这一定是我对用户“thera-eks”所做的错误。 也许是角色绑定?

看起来我给了角色访问所有内容的权限,因为在规则中我给了它 * 通配符。

我使用的命令是-

kubectl create namespace ernie

我得到的错误是 -

Error from server (Forbidden): namespaces is forbidden: User "thera-eks" cannot create resource "namespaces" in API group "" at the cluster scope

我的 role.yaml 是:

apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  name: full_access
rules:
- apiGroups: ["*"]
  resources: ["*"]
  verbs: ["*"]

我的 rolebinding.yaml 是:

apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: full_access_role_binding
subjects:
- kind: User
  name: thera-eks
  apiGroup: rbac.authorization.k8s.io
roleRef:
  kind: Role
  name: full_access
  apiGroup: rbac.authorization.k8s.io

aws-auth 配置映射是:

data:
  mapRoles: |
    - groups:
      - system:bootstrappers
      - system:nodes
      rolearn: arn:aws:iam::9967xxxxxxxx:role/eksctl-ops-nodegroup-linux-ng-sys-NodeInstanceRole-346VJPTOXI7L
      username: system:node:{{EC2PrivateDNSName}}
    - groups:
      - eks-role
      - system:master
      rolearn: arn:aws:iam::9967xxxxxxxx:role/thera-eks
      username: thera-eks
  mapUsers: |
    - userarn: arn:aws:iam::9967xxxxxxxx:user/test-ecr
    username: test-ecr
    groups:
    - eks-role

角色“thera-eks”的 AWS IAM 权限 JSON 是 -

 {
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "ecr:BatchGetImage",
                "ecr:BatchCheckLayerAvailability",
                "ecr:CompleteLayerUpload",
                "ecr:DescribeImages",
                "ecr:DescribeRepositories",
                "ecr:GetDownloadUrlForLayer",
                "ecr:InitiateLayerUpload",
                "ecr:ListImages",
                "ecr:PutImage",
                "ecr:UploadLayerPart",
                "ecr:GetAuthorizationToken"
            ],
            "Resource": "*"
        },
        {
            "Effect": "Allow",
            "Action": [
                "eks:*",
                "iam:ListRoles",
                "sts:AssumeRole"
            ],
            "Resource": "*"
        }
    ]
}

【问题讨论】:

  • 您创建了一个Role,但Role 仅适用于命名空间的上下文——ClusterRole 是跨越命名空间的东西(因此将能够自己创建命名空间)

标签: kubernetes kubectl amazon-eks


【解决方案1】:

@mdaniel 和@PEkambaram 是对的,但我想用official docs 扩展和支持它以便更好地理解:

RBAC RoleClusterRole 包含表示集合的规则 的权限。权限纯粹是附加的(没有“拒绝” 规则)。

Role 始终在特定命名空间内设置权限;什么时候 你创建一个Role,你必须指定它所属的命名空间。

ClusterRole,相比之下,是一个非命名空间资源。这 资源具有不同的名称(RoleClusterRole),因为 Kubernetes 对象总是必须是命名空间或不命名空间 命名空间;不可能两者兼有。

ClusterRoles 有多种用途。您可以使用ClusterRole 来:

  • 定义命名空间资源的权限并在单个命名空间内授予

  • 定义命名空间资源的权限并在所有命名空间中授予

  • 定义集群范围资源的权限

如果要在命名空间中定义角色,请使用Role;如果要在集群范围内定义角色,请使用ClusterRole

您还将找到ClusterRole 的示例:

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"]

对于ClusterRoleBinding

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

链接的文档将向您展示所有必要的详细信息以及有助于理解和设置 RBAC 的示例。

【讨论】:

  • 就是这样!感谢详细的解释!!!
【解决方案2】:

用户“thera-eks”没有创建命名空间的权限。

使用以下命令检查是否允许创建命名空间

kubectl auth can-i create namespace

您需要拥有集群级别的权限才能创建命名空间对象。在clusterrolebindings中定义clusterrole并映射用户

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-06
    • 2020-06-25
    • 2022-01-26
    • 1970-01-01
    • 2021-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多