Skip to content

Instantly share code, notes, and snippets.

@james-hinton
Created March 16, 2023 10:38
Show Gist options
  • Save james-hinton/2a5fe1688fd99c75e1a3a10ac32834be to your computer and use it in GitHub Desktop.
Save james-hinton/2a5fe1688fd99c75e1a3a10ac32834be to your computer and use it in GitHub Desktop.
A shell script to fetch logs for Kubernetes pods based on the beginning of their names and an optional namespace.
#!/bin/bash
if [ "$#" -lt 1 ] || [ "$#" -gt 2 ]; then
echo "Usage: $0 <container-name-start> [namespace]"
exit 1
fi
container_name_start="$1"
namespace_flag=""
if [ "$#" -eq 2 ]; then
namespace="$2"
namespace_flag="--namespace=$namespace"
fi
pods=$(kubectl get pods $namespace_flag --no-headers -o custom-columns=":metadata.name")
for pod in $pods; do
if [[ $pod == $container_name_start* ]]; then
echo "Fetching logs for pod: $pod"
kubectl logs $namespace_flag "$pod"
fi
done
@james-hinton
Copy link
Author

Example Usage:
./get_logs.sh <pod-name-start> [<optional-namespace>]

<pod-name-start>: The beginning of the pod name you want to fetch logs for.
[<optional-namespace>]: (Optional) The specific namespace where the pod is located. If not provided, the script will use the current namespace.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment