1

我无法使用 rest api 获取命名空间列表,而 rest 端点是https://<localhost>:8001/api/v1/namespaces

使用这个 Kubernetes 文档

我正在使用邮递员。我将重复这些步骤:

  1. 创建了一个用户并赋予集群管理员权限:

kubectl create serviceaccount exampleuser

  1. 使用集群角色 cluster-admin 为我们的用户创建了一个角色绑定:

kubectl create rolebinding <nameofrolebinding> --clusterrole cluster-admin --serviceaccount default:exampleuser

  1. 使用以下方法检查角色绑定:

kubectl describe rolebinding <nameofrolebinding>

  1. 现在通过使用:

kubectl describe serviceaccount exampleuser kubectl describe secret exampleuser-xxxx-xxxx

我将使用我在这里得到的令牌来验证邮递员。

GET https://<ipofserver>:port/api/v1/namespace

使用不记名令牌进行身份验证。

列出集群中所有命名空间的预期结果。喜欢 kubectl get namespaces。但收到如下警告。

{
    "kind": "Status",
    "apiVersion": "v1",
    "metadata": {},
    "status": "Failure",
    "message": "namespaces is forbidden: User \"system:serviceaccount:default:exampleuser\" cannot list resource \"namespaces\" in API group \"\" at the cluster scope",
    "reason": "Forbidden",
    "details": {
        "kind": "namespaces"
    },
    "code": 403
}

我已经为用户使用了“cluster-admin”clusterrole,但仍然出现与身份验证相关的错误。请帮忙。

4

2 回答 2

1

您应该使用clusterrolebinding而不是rolebinding

kubectl create clusterrolebinding <nameofrolebinding> --clusterrole cluster-admin --serviceaccount default:exampleuser

RoleBinding means permissions to a namespaced resources, but namespace is not a namespaced resources, you can check this by kubectl api-resouces.

More detail at rolebinding-and-clusterrolebinding:

Permissions can be granted within a namespace with a RoleBinding, or cluster-wide with a ClusterRoleBinding

于 2019-05-22T08:30:18.507 回答
0

so issue is instead of using rolebinding , i need to use clusterrolebinding check below

kubectl create rolebinding nameofrolebinding --clusterrole cluster-admin --serviceaccount default:exampleuser

kubectl create clusterrolebinding nameofrolebinding --clusterrole cluster-admin --serviceaccount default:exampleuser

rolebinding scope is upto a namespace and clusterrolebinding scope is entire cluster.

To work with api/v1/namespaces we need to use clusterrolebinding

于 2019-05-22T08:33:52.393 回答