Skip to content

Instantly share code, notes, and snippets.

@seanslma
Created December 16, 2024 00:04
Show Gist options
  • Save seanslma/c3e6707016e4bad263f844b57650ae89 to your computer and use it in GitHub Desktop.
Save seanslma/c3e6707016e4bad263f844b57650ae89 to your computer and use it in GitHub Desktop.
List the pod disk usage for each pod in kubernetes
#!/bin/bash
# Check if kubectl is configured to connect to AKS cluster
if ! command -v kubectl &> /dev/null
then
echo "kubectl could not be found. Please install kubectl and configure it to access your AKS cluster."
exit 1
fi
# Get the list of all pods in the cluster
echo "Getting pod list..."
PODS=$(kubectl get pods --all-namespaces --field-selector=status.phase=Running -o custom-columns="NAMESPACE:.metadata.namespace,POD:.metadata.name")
if [[ -z "$PODS" ]]; then
echo "No pods found in the cluster."
exit 1
fi
# Print the header for the output
echo "NAMESPACE,POD,DISK_USAGE"
# Loop through each pod
while read -r line
do
# Extract namespace and pod name
NAMESPACE=$(echo $line | awk '{print $1}')
POD=$(echo $line | awk '{print $2}')
# Skip the header line
if [[ "$POD" == "POD" ]]; then
continue
fi
DISK_USAGE=$(kubectl exec -n $NAMESPACE $POD -- du -sh / 2>/dev/null | awk '{print $1}')
if [[ -z "$DISK_USAGE" ]]; then
DISK_USAGE="N/A" # If disk usage can't be retrieved
fi
# Print the result
echo "$NAMESPACE,$POD,$DISK_USAGE"
done <<< "$PODS"
echo "Disk usage check complete."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment