Skip to content

Instantly share code, notes, and snippets.

@ornithos
Created July 20, 2023 16:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ornithos/d0683b78ec2baac28439fe898f5a7701 to your computer and use it in GitHub Desktop.
Save ornithos/d0683b78ec2baac28439fe898f5a7701 to your computer and use it in GitHub Desktop.
Tail the pods of a Replicaset via kubectl
#!/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