监控 Kubernetes PVC 磁盘使用情况

Monitoring Kubernetes PVC disk usage

我正在尝试监控 Kubernetes PVC 磁盘使用情况。我需要用于 Persistent Volume Claim 的内存。我找到了命令:

kubectl get --raw / api / v1 / persistentvolumeclaims

Return:

"status":{
            "phase":"Bound",
            "accessModes":[
               "ReadWriteOnce"
            ],
            "capacity":{
               "storage":"1Gi"
            }
         }

但它只给我带来了磁盘的全部容量,正如我所说,我需要用过的

有谁知道哪个命令可以return给我这个信息?

我没有确定的答案,但希望这对您有所帮助。另外,如果有人有更好的答案,我会很感兴趣。

获取当前使用情况

The PersistentVolume subsystem provides an API for users and administrators that abstracts details of how storage is provided from how it is consumed.

-- Persistent Volume | Kubernetes

正如 Kubernetes 文档中所述,PV (PersistentVolume) 和 PVC (PersistentVolumeClaim) 是对存储的抽象。因此,我认为您不能检查 PV 或 PVC,但可以检查存储介质。

要获取使用情况,请创建一个将使用您的 PVC 的调试 pod,您将从中检查使用情况。这应该取决于您的存储提供商。

# volume-size-debugger.yaml
kind: Pod
apiVersion: v1
metadata:
  name: volume-size-debugger
spec:
  volumes:
  - name: debug-pv
    persistentVolumeClaim:
      claimName: <pvc-name>
  containers:
  - name: debugger
    image: busybox
    command: ["sleep", "3600"]
    volumeMounts:
    - mountPath: "/data"
      name: debug-pv

使用 kubectl apply -f volume-size-debugger.yaml 应用上述清单,并在其中 运行 一个 shell kubectl exec -it volume-size-debugger sh。在 shell 运行 du -sh 中获取人类可读格式的用法。

监控

我相信您已经注意到,这对于监控不是特别有用。它可能对不时的 one-time 检查有用,但对监控或低磁盘 space 警报没有用。

设置监控的一种方法是拥有一个类似于我们上面的 sidecar pod,并从那里收集我们的指标。一个这样的例子似乎是 node_exporter.

另一种方法是使用 CSI(容器存储接口)。我没有使用过 CSI,对它的了解还不够,无法真正解释更多。但这里有几个相关问题和相关的 Kubernetes 文档:

+1 到 touchmarine's 回答不过我想稍微扩展一下并加上我的三美分。

But it only brings me the full capacity of the disk, and as I said I need the used one

PVC 是一种抽象,它表示 对存储的请求 并且根本不存储诸如磁盘使用情况之类的信息。作为更高级别的抽象,它根本不关心其消费者如何使用底层存储。

@touchmarine,与其使用唯一功能是 sleepPod,而且每次您需要检查需要手动附加到磁盘的磁盘使用情况时,我建议使用像这样:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      volumes:
      - name: media
        persistentVolumeClaim:
          claimName: media
      containers:
      - name: nginx
        image: nginx
        ports:
        - containerPort: 80
        volumeMounts:
        - mountPath: "/data"
          name: media
      - name: busybox
        image: busybox
        command: ["/bin/sh"]
        args: ["-c", "while true; do du -sh /data; sleep 10;done"]
        volumeMounts:
        - mountPath: "/data"
          name: media

它当然可以是一个 single-container busybox Pod,就像@touchmarine 的例子一样,但在这里我决定也展示它如何被用作 sidecar 运行 在单个 Pod.

中的 nginx 容器旁边

因为它运行一个简单的 bash 脚本 - 一个无限的 while 循环,它将当前磁盘使用情况打印到标准输出,可以使用 kubectl logs 读取它而不需要使用 kubectl exec 并附加到 Pod:

$ kubectl logs nginx-deployment-56bb5c87f6-dqs5h busybox
20.0K   /data
20.0K   /data
20.0K   /data

我想它也可以更有效地用于配置某种磁盘使用情况监控。