Last active
June 6, 2024 13:59
-
-
Save fernandoacorreia/3c02ce1a54c1964417351f6b017cb0b6 to your computer and use it in GitHub Desktop.
Log Kubernetes pod
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 | |
# | |
# Saves logs for all containers in a pod. | |
# | |
set -o nounset -o errexit -o pipefail | |
# Function to print the usage | |
usage() { | |
echo "Usage: $0 [-n namespace] <pod-name>" | |
exit 1 | |
} | |
# Default namespace is empty | |
NAMESPACE_ARG="" | |
NAMESPACE_DISPLAY="" | |
# Parse the arguments | |
while getopts ":n:" opt; do | |
case $opt in | |
n) | |
NAMESPACE_ARG="--namespace $OPTARG" | |
NAMESPACE_DISPLAY="in namespace $OPTARG " | |
;; | |
\?) | |
echo "Invalid option: -$OPTARG" >&2 | |
usage | |
;; | |
:) | |
echo "Option -$OPTARG requires an argument." >&2 | |
usage | |
;; | |
esac | |
done | |
shift $((OPTIND -1)) | |
# Check if a pod name was passed as an argument | |
if [ -z "${1-}" ]; then | |
echo "Error: No pod name provided." | |
usage | |
fi | |
# Assign the remaining argument to a variable | |
POD_NAME=$1 | |
# Function to check if the pod is ready | |
is_pod_ready() { | |
STATUS=$(kubectl get pod $POD_NAME $NAMESPACE_ARG -o jsonpath='{.status.conditions[?(@.type=="Ready")].status}') | |
if [ "$STATUS" == "True" ]; then | |
return 0 | |
else | |
return 1 | |
fi | |
} | |
# Wait until the pod is ready | |
while ! is_pod_ready; do | |
echo "Pod $POD_NAME is not ready yet. Checking again in 5 seconds..." | |
sleep 5 | |
done | |
LOG_DIR="$HOME/scratch/log/$POD_NAME" | |
mkdir -p "$LOG_DIR" | |
kubectl get pod $POD_NAME $NAMESPACE_ARG -o yaml > $LOG_DIR/$POD_NAME.yaml | |
kubectl describe pod $POD_NAME $NAMESPACE_ARG > $LOG_DIR/$POD_NAME-describe.txt | |
LOG_FILE="$LOG_DIR/$POD_NAME.log" | |
echo "Logging from all container in pod $POD_NAME ${NAMESPACE_DISPLAY}to $LOG_FILE" | |
# Logs from all containers in a pod, prefixing each log line with the timestamp and the container name. Saves the log to a file. | |
kubectl logs -f $POD_NAME $NAMESPACE_ARG --all-containers=true --prefix=true --timestamps=true | tee $LOG_FILE |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment