Skip to content

Instantly share code, notes, and snippets.

@navicore
Created March 6, 2023 23:15
Show Gist options
  • Save navicore/4169eb3f7f4789db3b432c80e591de17 to your computer and use it in GitHub Desktop.
Save navicore/4169eb3f7f4789db3b432c80e591de17 to your computer and use it in GitHub Desktop.
kubectl logs bash deployments
#!/usr/bin/env bash
if [ "$#" -ne 1 ]; then
echo "Usage: $0 DEPLOYMENT_NAME"
exit 1
fi
DEPLOYMENT_NAME="$1"
LOG_DIR="./logs"
# Create the logs directory if it doesn't exist
mkdir -p "$LOG_DIR"
# Get the list of pods for the deployment
PODS=$(kubectl get pods -l "app=$DEPLOYMENT_NAME" -o jsonpath='{.items[*].metadata.name}')
# Loop through each pod and get its logs
for POD in $PODS; do
LOG_FILE="$LOG_DIR/$POD.log"
echo "Getting logs for pod $POD..."
kubectl logs "$POD" > "$LOG_FILE"
done
echo "Finished getting logs for all pods in deployment $DEPLOYMENT_NAME."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment