Created
August 12, 2025 10:11
-
-
Save sawaYch/3f6f07e8ffb7c65c6322acd6ddcdd510 to your computer and use it in GitHub Desktop.
kubectl log to my aws k8s cluster (kubectl get pods, logs)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Script to find and follow logs of pods based on pattern matching | |
# Usage: ./kubectl-logs <part1> <part2> [part3] ... | |
# Example: ./kubectl-logs shl staging | |
# This will find pods matching: sieservice-shl-*-staging-* | |
NAMESPACE="sieservice" | |
# Check if at least one argument is provided | |
if [ $# -eq 0 ]; then | |
echo "Usage: $0 <part1> <part2> [part3] ..." | |
echo "Example: $0 shl staging" | |
echo "This will find pods matching pattern: sieservice-shl-*-staging-*" | |
exit 1 | |
fi | |
# Construct the grep pattern from arguments | |
# Start with "sieservice-" | |
PATTERN="sieservice" | |
for arg in "$@"; do | |
PATTERN="${PATTERN}-.*${arg}" | |
done | |
echo "Getting pods in namespace: $NAMESPACE" | |
echo "Looking for pod matching pattern: ${PATTERN}-" | |
echo "Arguments provided: $@" | |
echo "----------------------------------------" | |
# Get the pod name that matches the pattern | |
POD_NAME=$(kubectl get pods -n $NAMESPACE --no-headers | grep -E "^${PATTERN}-" | awk '{print $1}') | |
if [ -z "$POD_NAME" ]; then | |
echo "Error: No pod found matching pattern '${PATTERN}-' in namespace '$NAMESPACE'" | |
echo "" | |
echo "Available pods in namespace '$NAMESPACE':" | |
kubectl get pods -n $NAMESPACE | |
exit 1 | |
fi | |
# Check if multiple pods match the pattern | |
POD_COUNT=$(echo "$POD_NAME" | wc -l) | |
if [ $POD_COUNT -gt 1 ]; then | |
echo "Multiple pods found matching the pattern:" | |
echo "$POD_NAME" | |
echo "" | |
echo "Please be more specific with your search terms." | |
exit 1 | |
fi | |
echo "Found pod: $POD_NAME" | |
echo "Following logs..." | |
echo "----------------------------------------" | |
# Follow the logs of the found pod | |
kubectl logs -n $NAMESPACE -f $POD_NAME |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment