Skip to content

Instantly share code, notes, and snippets.

@nataz77
Last active May 17, 2023 14:08
Show Gist options
  • Save nataz77/8a8dfe62374cd7b63e96dfb81ebcbdc7 to your computer and use it in GitHub Desktop.
Save nataz77/8a8dfe62374cd7b63e96dfb81ebcbdc7 to your computer and use it in GitHub Desktop.
Extract and/or combine logs from multiple k8s pods
#!/usr/bin/env bash
# shellcheck shell=bash
# Usage: ./logs.sh -l app=app or ./logs.sh -l app=app -a combinedapplog.txt or
# Author: nataz77 (https://github.com/nataz77)
function printusage {
echo 'Log extractor';
echo 'eg: ./logs.sh -l app=app ';
echo 'eg: ./logs.sh -l app=app -n app ';
echo "eg: ./logs.sh -l app=app -a log.txt"
echo "eg: ./logs.sh -l app=app -n app -a log.txt"
exit 1;
}
if [ -z "$*" ]; then echo "No arguments specified"; printusage; fi
while getopts "l:a:n:" opt
do
case "${opt}" in
l) LABEL=${OPTARG};;
a) OUTPUT=${OPTARG};;
n) NAMESPACE=${OPTARG};;
*) printusage
esac
done
# let's assume default if no other namespace is specified
if [ -z "$NAMESPACE" ]
then
NAMESPACE=default
fi
PODS=$(kubectl get pods -n "$NAMESPACE" -l "$LABEL" -o go-template --template '{{range .items}}{{.metadata.name}}{{"\n"}}{{end}}')
if [ -z "$PODS" ]; then echo "No pods matched label $LABEL in namespace $NAMESPACE"; exit 2; fi
for pod in $PODS
do
#depending on logs, this may take a while
echo "Getting logs from pod $pod in namespace $NAMESPACE, this may take a while..."
if [ -z "$OUTPUT" ]
then
kubectl logs "$pod" > "$pod".txt
else
# usually it's not ideal to combine pod logs in a single file, but I found it helpful sometimes when you're looking for a specific error and there's no correlationid or machineid with the pod hostname that generated id
kubectl logs "$pod" >> "$OUTPUT".txt
fi
echo "Done"
done
@ngyeagwa
Copy link

The scripts locks very interesting
How do I run the scripts. I'm new to Kubernetes but I need to develop similar scripts

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