首页 > 解决方案 > k8s 命名空间管理员如何使用 top?

问题描述

我们有一个共享租户集群,我们希望我们的开发人员能够运行kubectl top pods --namespace dev-namespace

但在我看来,要使 top 可用,您需要能够运行kubectl get nodes. 但是节点没有命名空间。

有解决办法吗?

我们的命名空间管理员设置如下:

roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: admin
subjects:
- apiGroup: rbac.authorization.k8s.io
  kind: User
  name: username@domain

作为集群管理员,我可以运行 top 命令,因此 metrics-server 似乎工作正常。

标签: kubernetes

解决方案


Kubernetes 有 API 组metrics.k8s.io,您可以使用它来授予对kubectl top pods -n <namespace>. 如果您授予podgetlist权限,则可以运行该命令。

我在运行 Kubernetes 1.21 的 GKE 集群中测试了以下配置,其中kubectl top pod --as=system:serviceaccount:monitoring:test-account -n monitoring. 有了这些权限,我只能kubectl top podmonitoring命名空间中运行,其他命令会失败。

apiVersion: v1
kind: ServiceAccount
metadata:
  name: test-account
  namespace: monitoring
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  name: pod-reader
  namespace: monitoring
rules:
- apiGroups: ["metrics.k8s.io"]
  resources: ["pods"]
  verbs: ["get", "list"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: read-pods
  namespace: monitoring
subjects:
- kind: ServiceAccount
  name: test-account
roleRef:
  kind: Role
  name: pod-reader
  apiGroup: rbac.authorization.k8s.io

推荐阅读