<p>I'm trying to monitor Kubernetes PVC disk usage. I need the memory that is in use for Persistent Volume Claim. I found the command:</p>
<blockquote>
<p>kubectl get --raw / api / v1 / persistentvolumeclaims</p>
</blockquote>
<p>Return:</p>
<pre><code>&quot;status&quot;:{
            &quot;phase&quot;:&quot;Bound&quot;,
            &quot;accessModes&quot;:[
               &quot;ReadWriteOnce&quot;
            ],
            &quot;capacity&quot;:{
               &quot;storage&quot;:&quot;1Gi&quot;
            }
         }
</code></pre>
<p>But it only brings me the full capacity of the disk, and as I said I need the used one</p>
<p>Does anyone know which command could return this information to me?</p>

Danilo Marquiori
<p>I don't have a definitive anwser, but I hope this will help you. Also, I would be interested if someone has a better anwser.</p>
<h2>Get current usage</h2>
<blockquote>
<p>The PersistentVolume subsystem provides an API for users and administrators that abstracts details of how storage is provided from how it is consumed.</p>
<p>-- <a href="https://kubernetes.io/docs/concepts/storage/persistent-volumes/#introduction:%7E:text=The%20PersistentVolume%20subsystem%20provides%20an%20API,provided%20from%20how%20it%20is%20consumed." rel="noreferrer">Persistent Volume | Kubernetes</a></p>
</blockquote>
<p>As stated in the Kubernetes documentation, PV (PersistentVolume) and PVC (PersistentVolumeClaim) are abstractions over storage. As such, I do not think you can inspect PV or PVC, but you can inspect the storage medium.</p>
<p>To get the usage, create a debugging pod which will use your PVC, from which you will check the usage. This should work depending on your storage provider.</p>
<pre class="lang-yaml prettyprint-override"><code># volume-size-debugger.yaml
kind: Pod
apiVersion: v1
metadata:
  name: volume-size-debugger
spec:
  volumes:
  - name: debug-pv
    persistentVolumeClaim:
      claimName: &lt;pvc-name&gt;
  containers:
  - name: debugger
    image: busybox
    command: [&quot;sleep&quot;, &quot;3600&quot;]
    volumeMounts:
    - mountPath: &quot;/data&quot;
      name: debug-pv
</code></pre>
<p>Apply the above manifest with <code>kubectl apply -f volume-size-debugger.yaml</code>, and run a shell inside it with <code>kubectl exec -it volume-size-debugger sh</code>. Inside the shell run <code>du -sh</code> to get the usage in a human readable format.</p>
<h2>Monitoring</h2>
<p>As I am sure you have noticed, this is not especially useful for monitoring. It may be useful for a one-time check from time to time, but not for monitoring or low disk space alerts.</p>
<p>One way to setup monitoring would be to have a similar sidecar pod like ours above and gather our metrics from there. One such example seems to be the <a href="https://github.com/prometheus/node_exporter" rel="noreferrer">node_exporter</a>.</p>
<p>Another way would be to use <a href="https://kubernetes.io/blog/2019/01/15/container-storage-interface-ga/" rel="noreferrer">CSI</a> (Container Storage Interface). I have not used CSI and do not know enough about it to really explain more. But here are a couple of related issues and related Kubernetes documentation:</p>
<ul>
<li><a href="https://github.com/prometheus-operator/prometheus-operator/issues/2359" rel="noreferrer">Monitoring Kubernetes PersistentVolumes - prometheus-operator</a></li>
<li><a href="https://github.com/digitalocean/csi-digitalocean/issues/134" rel="noreferrer">Volume stats missing - csi-digitalocean</a></li>
<li><a href="https://kubernetes.io/docs/concepts/storage/storage-capacity/" rel="noreferrer">Storage Capacity | Kubernetes</a></li>
</ul>
