在 Kubernetes 日常运维中,PersistentVolumeClaim(PVC)是我们管理存储资源的核心对象。然而,Kubernetes 原生命令如 kubectl get pvc 只能显示请求(Requests)和容量(Capacity),却无法直接查看 PVC 实际的磁盘使用空间和使用率。这给容量规划和故障排查带来了不小困扰。

你是否也遇到过这样的场景:应用突然报错“磁盘空间不足”,但 kubectl get pvc 显示容量充足?问题很可能出在实际使用量上。这时候,我们就需要一个更强大的工具来“透视”PVC 的真实使用情况。

自由与向往

https://mp.weixin.qq.com/s/LhWFPv-ynElkD88lMEJNoA

0
1
引入神器:kubectl df-pv 插件
幸运的是,社区已经为我们准备了答案——kubectl df-pv 插件。它模仿了 Linux 中经典的 df 命令,专门用于显示 PVC 的实际使用量、可用空间和使用率,堪称 Kubernetes 存储运维的“瑞士军刀”。

Tip:官网文档地址:https://github.com/yashbhutwala/kubectl-df-pv

02
离线部署df-pv插件
前提条件:

已安装krew插件,未安装可以参考《掌握Kubernetes:kubectl ingress-nginx插件轻松管理Ingress资源》文章

1. 下载安装df-pv插件的manifest文件

$ cat <<'EOF' | tee ~/krew/df-pv/df-pv.yaml
apiVersion: krew.googlecontainertools.github.com/v1alpha2
kind: Plugin
metadata:
  name: df-pv
spec:
  version: v0.3.0
  platforms:
  - selector:
      matchLabels:
        os: linux
        arch: amd64
    uri: https://github.com/yashbhutwala/kubectl-df-pv/releases/download/v0.3.0/kubectl-df-pv_v0.3.0_linux_amd64.tar.gz
    sha256: afc9890e169caa97597a028ee87be9e8fe9a9b7db58912e23253faf383ef8e5d
    files:
    - from: "df-pv"
      to: "."
    - from: "LICENSE"
      to: "."
    bin: "df-pv"
  - selector:
      matchLabels:
        os: darwin
        arch: arm64
    uri: https://github.com/yashbhutwala/kubectl-df-pv/releases/download/v0.3.0/kubectl-df-pv_v0.3.0_darwin_arm64.tar.gz
    sha256: 42bb837344a34710eb9924563ffe309d7f7a09ccf492146f777dbcb0440cb871
    files:
    - from: "df-pv"
      to: "."
    - from: "LICENSE"
      to: "."
    bin: "df-pv"
  - selector:
      matchLabels:
        os: darwin
        arch: amd64
    uri: https://github.com/yashbhutwala/kubectl-df-pv/releases/download/v0.3.0/kubectl-df-pv_v0.3.0_darwin_amd64.tar.gz
    sha256: b1e2f12f96508c487f5b2cf9d7a5be4cd3a9d2fe2c4423c41f28a0befb336301
    files:
    - from: "df-pv"
      to: "."
    - from: "LICENSE"
      to: "."
    bin: "df-pv"
  - selector:
      matchLabels:
        os: windows
        arch: amd64
    uri: https://github.com/yashbhutwala/kubectl-df-pv/releases/download/v0.3.0/kubectl-df-pv_v0.3.0_windows_amd64.zip
    sha256: 54dee7c5fdd8ddadce5971ee6c1fcbe1c7cd18c4de8bcf9e1f8f29f496cc6dd7
    files:
    - from: "df-pv.exe"
      to: "."
    - from: "LICENSE"
      to: "."
    bin: "df-pv.exe"
  shortDescription: Show disk usage (like unix df) for persistent volumes
  homepage: https://github.com/yashbhutwala/kubectl-df-pv
  description: |
    This plugin emulates Unix style df for persistent volumes.
EOF

2. 下载安装的压缩包


$ wget https://github.com/yashbhutwala/kubectl-df-pv/releases/download/v0.3.0/kubectl-df-pv_v0.3.0_linux_amd64.tar.gz

–2025-07-29 16:47:53– https://github.com/yashbhutwala/kubectl-df-pv/releases/download/v0.3.0/kubectl-df-pv_v0.3.0_linux_amd64.tar.gz

2025-07-29 16:48:02 (2.06 MB/s) - ‘kubectl-df-pv_v0.3.0_linux_amd64.tar.gz’ saved [9080681/9080681]

3. 通过 krew 安装df-pv插件


$ kubectl krew install --manifest df-pv.yaml --archive kubectl-df-pv_v0.3.0_linux_amd64.tar.gz

Installing plugin: df-pv
Installed plugin: df-pv

\
 | Use this plugin:
 |      kubectl df-pv
 | Documentation:
 |      https://github.com/yashbhutwala/kubectl-df-pv
/

0
3
df-pv插件实战

1. 查看某个命名空间所有pvc使用情况


$ kubectl df-pv -n obs-system 

PV NAME PVC NAME NAMESPACE NODE NAME POD NAME VOLUME MOUNT NAME SIZE USED AVAILABLE %USED IUSED IFREE %IUSED
pvc-ce4b4c4a-fdb7-4b05-9c30-895f9bd9e60f elasticsearch-elasticsearch-0 obs-system k8s-node03 elasticsearch-0 elasticsearch 29Gi 2024Mi 27Gi 6.73 822 1965258 0.04

2. 查看所有命名空间的pvc使用情况


$ kubectl df-pv 

PV NAME PVC NAME NAMESPACE NODE NAME POD NAME VOLUME MOUNT NAME SIZE USED AVAILABLE %USED IUSED IFREE %IUSED
pvc-c1c227cb-ab07-4544-ba76-c8f12df52f5a data-harbor-trivy-1 harbor k8s-node03 harbor-trivy-1 data 61Gi 34Gi 26Gi 56.28 619926 31754346 1.91

结语
在 Kubernetes 复杂的存储生态中,掌握 PVC 的真实使用状态是保障应用稳定运行的关键一步。kubectl df-pv 插件以其简洁高效的设计,填补了原生命令的空白,让存储使用情况“看得见、管得住”。

文档更新时间: 2025-07-31 07:36   作者:admin