如何在 Kubernetes 中获取容器的磁盘使用情况(没有 docker 命令)?

How to get container's disk usage in Kubernetes (without docker command)?

我有一个 Kubernetes 集群,想知道我的容器使用了多少磁盘 space。我不是在谈论已安装的卷。

我可以使用 docker system df -vdocker ps -s 等 docker 命令获取此信息,但我不想连接到每个工作节点。

有没有办法通过 kubectl 获取容器的磁盘使用情况,或者我可以从 kubelet 指标中获取此信息?

是,但目前不使用 kubectl,您可以从 /metrics/cavisor 端点上的 kubelet, either through the kube-apiserver (proxied) or directly calling the kubelet HTTP(s) server endpoint (default port 10250). Disk metrics are generally available on the /stats/summary endpoint and you can also find some cAdvisor 指标获取指标。

例如,要获取通过 kube-apiserver 返回的第一个 pod 中的第一个容器的 'usedBytes':

$ curl -k -s -H 'Authorization: Bearer <REDACTED>' \
  https://kube-apiserver:6443/api/v1/nodes/<node-name>/proxy/stats/summary \
  | jq '.pods[0].containers[0].rootfs.usedBytes'

Bearer 令牌可以是绑定到 ClusterRole 的服务帐户令牌,如下所示:

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  annotations:
  name: myrole
rules:
- apiGroups:
  - ""
  resources:
  - nodes
  - nodes/proxy
  verbs:
  - get
  - list
  - watch
- nonResourceURLs:
  - /metrics
  - /api/*
  verbs:
  - get

添加到 Rico 的答案中,您可以使用 kubectl 设置 API 服务器的代理,以便您可以使用服务器地址和身份验证的详细信息查询 API照顾好了。

使用

设置 API 服务器的本地代理
kubectl proxy --port=8080

然后你可以使用像

这样的命令
curl http://localhost:8080/api/v1/nodes/<node-name>/proxy/stats/summary 2>/dev/null | jq '.pods[].containers[].rootfs.usedBytes'

这将为您提供每个容器的 rootfs 使用情况列表。