Skip to content

Instantly share code, notes, and snippets.

@dohq
Created January 16, 2026 15:27
Show Gist options
  • Select an option

  • Save dohq/f9c0f6d45a81b8cdf0ede3232a71caf4 to your computer and use it in GitHub Desktop.

Select an option

Save dohq/f9c0f6d45a81b8cdf0ede3232a71caf4 to your computer and use it in GitHub Desktop.
interactive kubectl stern
function ks() {
# 1. Select Namespace
local ns=$(kubectl get namespaces --no-headers | fzf --height 20% --reverse --prompt="Namespace > " | awk '{print $1}')
if [ -z "$ns" ]; then return; fi
# 2. Select Pod in Namespace
local pod=$(kubectl get pods -n "$ns" --no-headers | fzf --height 40% --reverse --prompt="Pod (select one to get prefix) > " | awk '{print $1}')
if [ -z "$pod" ]; then return; fi
# 3. Create a "query name" by removing the suffix from the Pod name
# - For Deployment: name-hash-suffix (e.g., vector-5c8pj-abcde) -> vector
# - For DaemonSet/StatefulSet: name-suffix (e.g., vector-5c8pj) -> vector
# Remove up to two trailing segments of hashes (alphanumerics) or indexes (digits) using the following regular expression
local query=$(echo "$pod" | sed -E 's/-[a-z0-9]+(-[a-z0-9]+)?$//')
# 4. Run kubectl stern
echo "Executing: kubectl stern -n $ns $query" --tail 20
kubectl stern -n "$ns" "$query" --tail 20
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment