Created
July 20, 2023 16:17
-
-
Save ornithos/d0683b78ec2baac28439fe898f5a7701 to your computer and use it in GitHub Desktop.
Tail the pods of a Replicaset via kubectl
This file contains 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 | |
# With some help from ChatGPT, although it was only somewhat correct/helpful | |
rs_name="..." # name of replicaset | |
namespace="..." # namespace to find pod/rs within | |
# Function to tail logs for each pod | |
tail_logs() { | |
local pod="$1" | |
echo "Tailing logs for pod: $pod" | |
kubectl logs -f "$pod" --namespace=$namespace --prefix & | |
} | |
# Get the list of pods managed by the ReplicaSet | |
pods=$(kubectl get pods --namespace=$namespace -l app=$rs_name --field-selector=status.phase=Running -o jsonpath='{range .items[*]}{.metadata.name}{"\n"}{end}') | |
# Iterate over each pod and tail logs concurrently | |
for pod in $pods; do | |
tail_logs "$pod" | |
done | |
# Wait for all background jobs to finish | |
wait |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment