【问题标题】:Unable to read resources via K8s API无法通过 K8s API 读取资源
【发布时间】:2021-01-27 23:06:19
【问题描述】:

UDPDATED
我正在尝试通过部署在 K8s 上的 pod 内的 curl 获取资源。
虽然我能够通过 curl 请求获取 pod 列表,但我不能在 configmap 和节点上。

这里是我正在使用的角色绑定(为 pod 工作)

apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  name: test-ro
rules:
- apiGroups: [""] # "" indicates the core API group
  resources: ["pods", “configmaps”]
  verbs: ["get","list"]


 apiVersion: rbac.authorization.k8s.io/v1
    kind: ClusterRole
    metadata:
      name: test-cro
    rules:
    - apiGroups: [""] # "" indicates the core API group
      resources: ["nodes”]
      verbs: ["get","list"]

当我尝试获取节点列表时:

    curl -sSk -H "Authorization: Bearer $KUBE_TOKEN"       https://$KUBERNETES_SERVICE_HOST:$KUBERNETES_PORT_443_TCP_PORT/api/v1/nodes
        {
          "kind": "Status",
          "apiVersion": "v1",
          "metadata": {
            
          },
          "status": "Failure",
          "message": "nodes is forbidden: User \"system:serviceaccount:test:test\" cannot list resource \"nodes\" in API group \"\" at the cluster scope",
  "reason": "Forbidden",
  "details": {
    "kind": "nodes"
  },

对于配置映射也是如此:

curl -sSk -H "Authorization: Bearer $KUBE_TOKEN"       https://$KUBERNETES_SERVICE_HOST:$KUBERNETES_PORT_443_TCP_PORT/api/v1/namespaces/default/configmaps
    {
      "kind": "Status",
      "apiVersion": "v1",
      "metadata": {
        
      },
      "status": "Failure",
      "message": "configmaps is forbidden: User \"system:serviceaccount:test:test\" cannot list resource \"configmaps\" in API group \"\" in the namespace \"default\"",
      "reason": "Forbidden",
      "details": {
        "kind": "configmaps"
      },
      "code": 403

而不是在 pod 上它正在工作。
可能是什么问题? RoleBinding 配置错误?

【问题讨论】:

    标签: curl kubernetes rbac


    【解决方案1】:

    要授予test-ro 角色访问列表 ConfigMaps 的权限,必须以复数形式指定资源名称。这可能是列出 Pod 有效但列出 ConfigMap 无效的原因。所以角色应该这样指定:

    apiVersion: rbac.authorization.k8s.io/v1
    kind: Role
    metadata:
      name: test-ro
    rules:
    - apiGroups: [""] # "" indicates the core API group
      resources: ["pods", "configmaps"]
      verbs: ["get","list"]
    

    列出节点需要一些不同的配置,因为节点是集群级资源而不是命名空间资源。 Due to this, the nodes permissions must be given in a ClusterRole.

    此外,列出节点的 API url 没有命名空间。正确的网址是https://$KUBERNETES_SERVICE_HOST:$KUBERNETES_PORT_443_TCP_PORT/api/v1/nodes

    ClusterRole 的工作示例如下:

    apiVersion: rbac.authorization.k8s.io/v1
    kind: ClusterRole
    metadata:
      name: test-clusterrole
    rules:
    - apiGroups: [""] # "" indicates the core API group
      resources: ["nodes"]
      verbs: ["get","list"]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-07
      • 1970-01-01
      • 1970-01-01
      • 2012-11-30
      相关资源
      最近更新 更多