Analyze Persistent Volume Claims and storage resources
Analyze Persistent Volume Claims, storage classes, and persistent volumes to identify issues and optimize capacity. Use when troubleshooting storage problems, planning capacity, or reviewing storage configurations.
/plugin marketplace add kcns008/cluster-code/plugin install kcns008-cluster-core-plugins-cluster-core@kcns008/cluster-codesonnetI'll provide comprehensive analysis of your PVCs, persistent volumes, and storage resources.
# PVC status summary
kubectl get pvc {{#if namespace}}-n {{namespace}}{{/if}} -o wide
# Detailed PVC information
kubectl get pvc {{#if namespace}}-n {{namespace}}{{/if}} -o jsonpath='{range .items[*]}{.metadata.name}{"\n"}{.status.phase}{"\n"}{.spec.storageClassName}{"\n"}{.status.capacity.storage}{"\n"}{.spec.accessModes}{"\n\n"}{end}'
# Find PVCs with issues
kubectl get pvc {{#if namespace}}-n {{namespace}}{{/if}} --field-selector=status.phase!=Bound
# Available storage classes
kubectl get storageclass
# Default storage class
kubectl get storageclass -o jsonpath='{range .items[*]}{.metadata.name}{"\n"}{.metadata.annotations.storageclass\.kubernetes\.io/is-default-class}{"\n"}{end}' | grep "true"
# Storage class details
kubectl get storageclass -o yaml | grep -A 10 -E "(provisioner|parameters|reclaimPolicy)"
# Persistent volume status
kubectl get pv -o wide
# Volume usage by PVC
kubectl get pv -o jsonpath='{range .items[*]}{.metadata.name}{"\t"}{.spec.claimRef.namespace}{"\t"}{.spec.claimRef.name}{"\t"}{.status.phase}{"\n"}{end}'
# Unbound volumes and available capacity
kubectl get pv --field-selector=status.phase!=Bound -o wide
{{#if detailed}}
# Storage capacity summary
kubectl get pv -o jsonpath='{range .items[*]}{.spec.capacity.storage}{"\n"}{end}' | awk '{sum += $1} END {print "Total Storage Capacity: " sum "Gi"}'
# Used vs. Available storage
used_storage=$(kubectl get pvc --all-namespaces -o jsonpath='{range .items[*]}{.status.capacity.storage}{"\n"}{end}' | sed 's/Gi//' | awk '{sum += $1} END {print sum}')
total_storage=$(kubectl get pv -o jsonpath='{range .items[*]}{.spec.capacity.storage}{"\n"}{end}' | sed 's/Gi//' | awk '{sum += $1} END {print sum}')
echo "Storage Utilization: $used_storage Gi used / $total_storage Gi total"
# Storage class usage distribution
kubectl get pvc --all-namespaces -o jsonpath='{range .items[*]}{.spec.storageClassName}{"\n"}{end}' | sort | uniq -c
# Access mode distribution
kubectl get pvc --all-namespaces -o jsonpath='{range .items[*]}{.metadata.name}{"\t"}{.spec.accessModes}{"\n"}{end}'
# ReadWriteOnce vs ReadWriteMany usage
rwo_count=$(kubectl get pvc --all-namespaces -o jsonpath='{range .items[*]}{.spec.accessModes[?(@=="ReadWriteOnce")]}{"\n"}{end}' | wc -l)
rwx_count=$(kubectl get pvc --all-namespaces -o jsonpath='{range .items[*]}{.spec.accessModes[?(@=="ReadWriteMany")]}{"\n"}{end}' | wc -l)
rox_count=$(kubectl get pvc --all-namespaces -o jsonpath='{range .items[*]}{.spec.accessModes[?(@=="ReadOnlyMany")]}{"\n"}{end}' | wc -l)
echo "Access Mode Usage:"
echo " ReadWriteOnce: $rwo_count PVCs"
echo " ReadWriteMany: $rwx_count PVCs"
echo " ReadOnlyMany: $rox_count PVCs"
{{/if}}
{{#if pvc}}
Analyzing PVC: {{pvc}} in namespace: {{namespace}}
# PVC details
kubectl get pvc {{pvc}} -n {{namespace}} -o yaml
# Associated persistent volume
pv_name=$(kubectl get pvc {{pvc}} -n {{namespace}} -o jsonpath='{.spec.volumeName}')
if [ ! -z "$pv_name" ]; then
echo "Persistent Volume: $pv_name"
kubectl describe pv $pv_name
fi
# Pods using this PVC
kubectl get pods -n {{namespace}} -o jsonpath='{range .items[*]}{.metadata.name}{"\n"}{range .spec.volumes[*]}{.persistentVolumeClaim.claimName}{"\n"}{end}' | grep -B 1 {{pvc}}
# Recent events for this PVC
kubectl get events -n {{namespace}} --field-selector=involvedObject.name={{pvc}},involvedObject.kind=PersistentVolumeClaim --sort-by='.lastTimestamp'
{{/if}}
# PVCs stuck in Pending state
kubectl get pvc --all-namespaces --field-selector=status.phase=Pending -o wide
# PVCs with storage class issues
kubectl get pvc --all-namespaces -o jsonpath='{range .items[*]}{.metadata.name}{"\t"}{.spec.storageClassName}{"\t"}{.status.phase}{"\n"}{end}' | while read pvc sc phase; do
if [ "$phase" != "Bound" ]; then
echo "PVC $pvc with storage class $sc is $phase"
# Check if storage class exists
if ! kubectl get storageclass $sc >/dev/null 2>&1; then
echo " ❌ Storage class $sc does not exist"
fi
fi
done
# Volumes with issues
kubectl get pv --field-selector=status.phase!=Bound -o wide
# Approaching capacity limits
kubectl get pv -o jsonpath='{range .items[*]}{.metadata.name}{"\t"}{.spec.capacity.storage}{"\t"}{.status.phase}{"\n"}{end}' | while read pv capacity phase; do
if [ "$phase" = "Bound" ]; then
echo "Volume $pv: $capacity ($phase)"
else
echo "Volume $pv: $capacity ($phase) - AVAILABLE"
fi
done
# Storage class provisioning issues
kubectl get storageclass -o yaml | grep -A 5 -E "(provisioner|reclaimPolicy|volumeBindingMode)"
I can help you:
Would you like me to proceed with the comprehensive PVC analysis?