Created
December 14, 2024 17:03
-
-
Save korutx/07fd9f6c161407629f4acbd9d84fea14 to your computer and use it in GitHub Desktop.
Kubectl get pods sorted and colored by node
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
| #!/bin/bash | |
| # Script to display Kubernetes pods grouped and color-coded by node | |
| kubectl get pods -o wide | sort -k7 | awk ' | |
| BEGIN { | |
| # Define ANSI color codes for groups | |
| colors[1] = "\033[1;31m"; # Red | |
| colors[2] = "\033[1;32m"; # Green | |
| colors[3] = "\033[1;33m"; # Yellow | |
| colors[4] = "\033[1;34m"; # Blue | |
| colors[5] = "\033[1;35m"; # Magenta | |
| colors[6] = "\033[1;36m"; # Cyan | |
| reset = "\033[0m"; # Reset color | |
| } | |
| NR == 1 { print; next } # Print the header | |
| { | |
| # Group by Node (column 7) | |
| node = $7; | |
| if (!(node in seen)) { | |
| seen[node] = ++count; # Assign a unique group ID for each node | |
| } | |
| color = colors[seen[node] % 6 + 1]; # Cycle through colors | |
| printf "%s%s%s\n", color, $0, reset; | |
| }' |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
kgpwc.shThis script enhances Kubernetes
kubectlfunctionality by sorting and color-coding pods based on the nodes they are running on. It provides an easy-to-read visualization for Kubernetes administrators managing multiple nodes in a cluster.Features
kubectl get pods -o wideand sorts the pods by the node column to group them.Installation
Download the script and save it to a directory included in your
$PATH(e.g.,~/bin):Make the script executable:
chmod +x ~/bin/kgpwcEnsure the
~/bindirectory is in your$PATH. Add this line to your~/.zshrcor~/.bashrcif it’s not already there:Reload your shell configuration:
Usage
Run the script directly in your terminal:
This will output a list of Kubernetes pods, sorted and grouped by node, with each node visually distinguished by a unique color.
Example Output
Troubleshooting
No Colors in Output:
kubectlIssues:kubectlis correctly configured to communicate with your Kubernetes cluster:Script Not Found:
$PATHand is marked as executable.