Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. #!/usr/bin/env bash
  2.  
  3. if [ -z "$*" ] || [ "$1" == "-h" ] || [ "$1" == "--help" ]; then
  4. echo
  5. echo "Grab logs for a container, based on the container's ID."
  6. echo "Script will search all namespaces/pods for that container."
  7. echo "Make sure you set the correct k8s context before running this script."
  8. echo
  9. echo "Usage:"
  10. echo " $0 <container_id>"
  11. echo
  12. echo "Example:"
  13. echo " $0 7f08b48e1f49b7bc30ef8fd43dfaaa909928effbdb75058e64ce9efac2090440"
  14. echo
  15. exit 1
  16. fi
  17.  
  18. # Prepend docker scheme to container id
  19. CONTAINER_ID="docker://$1"
  20.  
  21. # Fetch all pods to search
  22. ALL_PODS=$(kubectl get pods --all-namespaces -o json)
  23.  
  24. # Find the pod with our container
  25. TARGET_POD=$(echo "$ALL_PODS" | jq ".items[] | select(.status.containerStatuses[].containerID==\"$CONTAINER_ID\")")
  26.  
  27. # Get some info needed for the logs command
  28. TARGET_POD_NAME=$(echo "$TARGET_POD" | jq -Mr '.metadata.name')
  29. TARGET_POD_NAMESPACE=$(echo "$TARGET_POD" | jq -Mr '.metadata.namespace')
  30. CONTAINER_NAME=$(echo "$TARGET_POD" | jq -Mr ".status.containerStatuses[] | select(.containerID==\"$CONTAINER_ID\") | .name")
  31.  
  32. # Fetch the logs
  33. kubectl --namespace="$TARGET_POD_NAMESPACE" logs "$TARGET_POD_NAME" -c "$CONTAINER_NAME"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement