Stage 2, Part 4 of 9 · p. 13, kubectl Mastery & Debugging

PART 4: CAPACITY PLANNING & COST MODELING

20. Fleet-Wide Debugging with kubectl

Multi-Namespace Queries

bash
# All Pods across namespaces
kubectl get pods -A

# All failing Pods
kubectl get pods -A --field-selector=status.phase=Failed

# All Pods not Running
kubectl get pods -A | grep -v "Running\|Completed"

# All Deployments not fully available
kubectl get deployments -A | awk '$3 != $4'

# All PVCs not Bound
kubectl get pvc -A | grep -v Bound

# All nodes with issues
kubectl get nodes | grep -v "Ready"

Capacity Audits

bash
# CPU/memory requests per namespace
kubectl get pods -A -o json | jq -r '.items[] | "\(.metadata.namespace)\t\(.spec.containers[].resources.requests.cpu // "0")\t\(.spec.containers[].resources.requests.memory // "0")"' | awk '{ns[$1]+=$2; ns_mem[$1]+=$3} END {for (n in ns) print n, ns[n], ns_mem[n]}'

# Nodes with allocated resources
kubectl describe nodes | grep -A5 "Allocated resources"

# Missing requests/limits
kubectl get pods -A -o json | jq '.items[] | select(.spec.containers[].resources.requests == null) | .metadata.name'

Image Audits

bash
# All images running in the cluster
kubectl get pods -A -o jsonpath='{range .items[*]}{.spec.containers[*].image}{"\n"}{end}' | sort -u

# Pods using :latest
kubectl get pods -A -o jsonpath='{range .items[*]}{.metadata.namespace}{"/"}{.metadata.name}{"\t"}{.spec.containers[*].image}{"\n"}{end}' | grep ':latest'

The kubectl Audit Script

bash
#!/bin/bash
# cluster-audit.sh

echo "=== Cluster Info ==="
kubectl cluster-info

echo -e "\n=== Node Status ==="
kubectl get nodes

echo -e "\n=== Namespaces ==="
kubectl get namespaces

echo -e "\n=== Failing Pods ==="
kubectl get pods -A | grep -v "Running\|Completed"

echo -e "\n=== Recent Events (Warnings) ==="
kubectl get events -A --field-selector type=Warning --sort-by=.metadata.creationTimestamp | tail -20

echo -e "\n=== Resource Usage ==="
kubectl top nodes

echo -e "\n=== PVCs not Bound ==="
kubectl get pvc -A | grep -v Bound

echo -e "\n=== Deployments Not Ready ==="
kubectl get deployments -A | awk 'NR>1 && $3 != $4'

echo -e "\n=== Certificate Status ==="
kubectl get certificates -A 2>/dev/null || echo "cert-manager not installed"

21. Cost Visibility via kubectl

Resource Requests

bash
# Total CPU requests by namespace
kubectl get pods -A -o json | jq -r '
  .items[] |
  .metadata.namespace as $ns |
  .spec.containers[] |
  .resources.requests.cpu // "0" |
  "\($ns)\t\(.)"
' | awk '{a[$1] += $2} END {for (n in a) print n, a[n]}'

Idle Resources

bash
# Pods using less than 10% of their request
kubectl get pods -A -o json | jq -r '.items[] | "\(.metadata.namespace)/\(.metadata.name)"' > pods.txt
while read pod; do
  kubectl top pod $(echo $pod | tr '/' ' ') 2>/dev/null
done < pods.txt

Zombie Workloads

bash
# Deployments with 0 replicas
kubectl get deployments -A | awk '$2 == "0/0"'

# Pods older than 30 days
kubectl get pods -A --sort-by=.metadata.creationTimestamp | awk 'NR>1'

# Unused PVCs (no mounted pod)
kubectl get pvc -A

22. SLA & Availability Design for Debugging Access

Access SLA Design

TargetApproach
99.9%kubectl with basic kubeconfig
99.99%Multiple kubeconfigs, HA control plane
99.999%Break-glass access, emergency RBAC

Break-Glass Access

When normal access fails, you need a way in:

MethodDescription
Admin kubeconfigStored securely offline
Cloud IAM roleAWS/GCP/Azure admin role
Emergency service accountStatic token, secret-stored
Node SSHDirect node access as backup

1:1 copy of the class Google Doc, code in code blocks.