【问题标题】:Kubernetes ServiceAccount cannot list nodesKubernetes ServiceAccount 无法列出节点
【发布时间】:2020-05-02 08:45:00
【问题描述】:

我正在尝试授予我的服务帐户foo 获取集群上节点列表的权限(通过kubectl get nodes)。我用这些权限创建了一个集群角色和一个角色绑定:

apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRole
metadata:
 name: foo-cluster-role
rules:
  - apiGroups: [""]
    resources: ["nodes"]
    verbs: ["get", "list", "watch"]

当我使用该服务帐户运行 pod 时,我无法运行 kubectl get nodes

root@debugger:/# kubectl get nodes
Error from server (Forbidden): nodes is forbidden: User "system:serviceaccount:default:foo" cannot list resource "nodes" in API group "" at the cluster scope

奇怪的是,当我通过 kubectl auth can-i 询问时,它告诉我应该可以访问:

root@debugger:/# kubectl auth can-i get nodes
Warning: resource 'nodes' is not namespace scoped
yes

如何设置我的服务帐户,以便我有权列出集群上的节点?

edit clusterrolebinding 看起来像这样:

kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
  name: foo-binding
subjects:
- kind: ServiceAccount
  name: foo
roleRef:
  kind: ClusterRole
  name: foo-cluster-role
  apiGroup: ""

【问题讨论】:

  • 你也可以分享 ClusterRoleBinding 吗?
  • 同时检查kubectl auth can-i get nodes --as=system:serviceaccount:default:foo
  • @hoque 添加了绑定。 kubectl auth can-i 也返回 yes

标签: kubernetes


【解决方案1】:

你必须创建ClusterRoleBinding。请检查以下内容。

    apiVersion: rbac.authorization.k8s.io/v1beta1
    kind: ClusterRole
    metadata:
      name: foo-cluster-role
    rules:
    - apiGroups: [""]
      resources: ["nodes"]
      verbs: ["get", "watch", "list"]

    ---
    apiVersion: rbac.authorization.k8s.io/v1beta1
    kind: ClusterRoleBinding
    metadata:
      name: foo-binding
    subjects:
    - kind: ServiceAccount
      name: foo
    roleRef:
      kind: ClusterRole
      name: foo-cluster-role
      apiGroup: rbac.authorization.k8s.io

【讨论】:

  • 啊!只是将它粘贴到我的问题中就让我注意到我写的是RoleBinding而不是ClusterRoleBinding。谢谢,愚蠢的错误。
  • @spike 谢谢你,我犯了同样的错误复制粘贴文件,直到我读到你的评论 /facepalm 才看到它
猜你喜欢
  • 2018-03-13
  • 1970-01-01
  • 2018-12-29
  • 1970-01-01
  • 2020-09-27
  • 1970-01-01
  • 2019-01-06
  • 2018-07-15
  • 2019-07-20
相关资源
最近更新 更多