Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env bash
- if [ -z "$*" ] || [ "$1" == "-h" ] || [ "$1" == "--help" ]; then
- echo
- echo "Grab logs for a container, based on the container's ID."
- echo "Script will search all namespaces/pods for that container."
- echo "Make sure you set the correct k8s context before running this script."
- echo
- echo "Usage:"
- echo " $0 <container_id>"
- echo
- echo "Example:"
- echo " $0 7f08b48e1f49b7bc30ef8fd43dfaaa909928effbdb75058e64ce9efac2090440"
- echo
- exit 1
- fi
- # Prepend docker scheme to container id
- CONTAINER_ID="docker://$1"
- # Fetch all pods to search
- ALL_PODS=$(kubectl get pods --all-namespaces -o json)
- # Find the pod with our container
- TARGET_POD=$(echo "$ALL_PODS" | jq ".items[] | select(.status.containerStatuses[].containerID==\"$CONTAINER_ID\")")
- # Get some info needed for the logs command
- TARGET_POD_NAME=$(echo "$TARGET_POD" | jq -Mr '.metadata.name')
- TARGET_POD_NAMESPACE=$(echo "$TARGET_POD" | jq -Mr '.metadata.namespace')
- CONTAINER_NAME=$(echo "$TARGET_POD" | jq -Mr ".status.containerStatuses[] | select(.containerID==\"$CONTAINER_ID\") | .name")
- # Fetch the logs
- kubectl --namespace="$TARGET_POD_NAMESPACE" logs "$TARGET_POD_NAME" -c "$CONTAINER_NAME"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement