Created
March 16, 2023 10:38
-
-
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.
This file contains 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 | |
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.