Eidada Kubernetes Scripts & Commands
Introduction
Managing Kubernetes clusters efficiently often requires more than just kubectl
basics. Over time, engineers develop and collect scripts to automate, debug, and monitor their clusters. This post curates some of the most useful scripts and command-line tricks from the open-source eldada/kubernetes-scripts repository, with practical explanations and usage tips for SREs and DevOps engineers.
Top Useful Kubernetes Scripts
Here are some of the most practical scripts from the eldada/kubernetes-scripts repo:
1. Count Pods and Containers per Node
- Script:
countPodsAndContainerPerNodeCSV.sh
- Purpose: Count the number of pods and containers on each node, output as CSV.
- Usage:
./countPodsAndContainerPerNodeCSV.sh
2. Find Empty Namespaces
- Script:
findEmptyNamespaces.sh
- Purpose: Loop over all namespaces and find those with no pods.
- Usage:
./findEmptyNamespaces.sh
3. Get Pods Load
- Script:
getPodsLoad.sh
- Purpose: Show the load average of nodes underlying each pod in a namespace.
- Usage:
./getPodsLoad.sh <namespace>
4. Get Pod Resource Usage as CSV
- Script:
getPodsTopCSV.sh
,getResourcesCSV.sh
- Purpose: Output CPU/memory usage and resource requests/limits for pods/containers in CSV format.
- Usage:
./getPodsTopCSV.sh <namespace>
./getResourcesCSV.sh
5. List Restarting Pods
- Script:
getRestartingPods.sh
- Purpose: List pods with containers that have restarted, formatted as CSV.
- Usage:
./getRestartingPods.sh
6. Check Pod Readiness
- Script:
podReady.sh
- Purpose: Check if a pod is truly ready (all containers ready, status is 'Running').
- Usage:
./podReady.sh <pod-name>
7. Run Command on Multiple Pods
- Script:
runCommandOnPods.sh
- Purpose: Run a shell command on a list of pods.
- Usage:
./runCommandOnPods.sh <namespace> <command>
8. Check User Permissions
- Script:
canIdo.sh
- Purpose: Check what permissions the current user has in a namespace using
kubectl auth can-i
. - Usage:
./canIdo.sh <namespace>
Handy Kubectl Command Tips
Some useful kubectl
commands and shell tricks from the same repo:
- See all cluster nodes load:
kubectl top nodes
- Get all cluster events:
kubectl get events
kubectl get events --namespace=kube-system - Get all node IPs and names:
kubectl get nodes -o json | grep -A 12 addresses
# Or loop for more details:
for n in $(kubectl get nodes -o name); do \
echo -e "\nNode ${n}"; \
kubectl get ${n} -o jsonpath='{.status.addresses}'; \
done - Get all labels attached to pods in a namespace:
for a in $(kubectl get pods -n <namespace> -o name); do \
echo -e "\nPod ${a}"; \
kubectl -n <namespace> describe ${a} | awk '/Labels:/,/Annotations/' | sed '/Annotations/d'; \
done - Forward local port to a pod or service:
kubectl port-forward -n <namespace> <pod> 8080:8080
kubectl port-forward -n <namespace> svc/<service> 8080:80 - Get list of pods sorted by restart count:
kubectl get pods -A --sort-by='.status.containerStatuses[0].restartCount'
- Top pods by CPU or memory usage:
kubectl top pods -A --sort-by=cpu | head -20
kubectl top pods -A --sort-by=memory | head -20
Conclusion
These scripts and command-line tips can save hours of manual work and provide deep insights into your Kubernetes clusters. For more scripts, YAML examples, and advanced usage, check out the full eldada/kubernetes-scripts GitHub repository.