Kubernetes Pod Monitoring with Shell Script

🚀 DevOps in Action: Kubernetes Pod Monitoring with Shell Script In real-world DevOps, it’s not just about deployments — it’s about visibility, reliability, and quick recovery. Here’s a simple shell script I use to monitor Kubernetes pods and detect issues early 👇 #!/bin/bash set -euo pipefail NAMESPACE="default" LOGFILE="k8s_monitor.log" log() { echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" | tee -a "$LOGFILE" } # Get pods not in Running or Completed state pods=$(kubectl get pods -n "$NAMESPACE" --no-headers | awk '$3!="Running" && $3!="Completed" {print $1}') if [ -z "$pods" ]; then log "All pods are running fine ✅" else log "Found problematic pods ❌" for pod in $pods; do log "Checking pod: $pod" kubectl describe pod "$pod" -n "$NAMESPACE" | grep -i "error" | tee -a "$LOGFILE" done fi 💡 What this script demonstrates: ✅ Kubernetes monitoring using kubectl ✅ Log parsing with awk and grep ✅ Error detection for non-running pods ✅ Timestamp-based logging ✅ Production-ready practices (set -euo pipefail) 🔁 CI/CD Tip: You can integrate this script into a Jenkins/GitHub Actions pipeline step to fail deployments if pods are unhealthy. 👉 That’s how you move from simple scripting to proactive infrastructure monitoring. How do you monitor your Kubernetes workloads? 👇 #DevOps #Kubernetes #ShellScripting #CICD #SRE #Cloud #Monitoring

To view or add a comment, sign in

Explore content categories