Created
January 16, 2026 15:27
-
-
Save dohq/f9c0f6d45a81b8cdf0ede3232a71caf4 to your computer and use it in GitHub Desktop.
interactive kubectl stern
This file contains hidden or 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
| 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