Recommended kubectl Plugins: A Practical Guide
Kubernetes is powerful, but managing clusters efficiently often requires more than just the built-in kubectl commands. The kubectl plugin ecosystem can supercharge your productivity, simplify troubleshooting, and help you manage resources at scale. This guide introduces essential plugins, explains their use cases, and walks you through installation and best practices.
What Are kubectl Plugins?
kubectl plugins are add-ons that extend the functionality of the standard kubectl command-line tool. They help automate repetitive tasks, improve output readability, and provide new capabilities for cluster management. Most plugins are distributed via Krew, the official plugin manager for kubectl.
Getting Started: Installing Krew (Plugin Manager)
Before you can use most plugins, you need to install Krew:
# Install Krew (the kubectl plugin manager)
(
set -x; cd "$(mktemp -d)" &&
OS="$(uname | tr '[:upper:]' '[:lower:]')" &&
ARCH="$(uname -m | sed -e 's/x86_64/amd64/' -e 's/\(arm\)\(64\)\?.*/\1\2/' -e 's/aarch64$/arm64/')" &&
KREW="krew-${OS}_${ARCH}" &&
curl -fsSLO "https://github.com/kubernetes-sigs/krew/releases/latest/download/${KREW}.tar.gz" &&
tar zxvf "${KREW}.tar.gz" &&
./${KREW} install krew
)
# Add krew to your PATH (follow the output instructions)
Once Krew is installed, you can discover and install plugins with simple commands like kubectl krew search and kubectl krew install <plugin>.
Essential Plugins and How to Use Them
1. kubectx – Fast Context Switching
- What it does: Quickly switch between Kubernetes contexts (clusters).
- Why use it: If you work with multiple clusters (e.g., dev, staging, prod, edge), this saves time and reduces mistakes.
- Install:
kubectl krew install ctx - Usage:
kubectl ctx # List all contexts
kubectl ctx my-prod # Switch to 'my-prod' context - Example:
You're debugging an issue in staging, then need to deploy to production. Switch instantly:
kubectl ctx stagingkubectl ctx production
2. kubens – Namespace Switching
- What it does: Switch between Kubernetes namespaces with ease.
- Why use it: Avoids typing
-n <namespace>repeatedly and reduces errors. - Install:
kubectl krew install ns - Usage:
kubectl ns # List all namespaces
kubectl ns dev # Switch to 'dev' namespace - Example:
You're working on a microservice in the
paymentsnamespace, then need to check logs inmonitoring:kubectl ns paymentskubectl ns monitoring
3. stern – Multi-Pod Log Tailing
- What it does: Tails logs from multiple pods matching a pattern, with color-coded output.
- Why use it: Great for debugging distributed apps or microservices that scale horizontally.
- Install:
kubectl krew install stern - Usage:
kubectl stern myapp
kubectl stern myapp --namespace production - Example:
You want to see logs from all pods of your
apideployment as they scale up and down:kubectl stern api
4. debug-shell – Instant Pod Debugging
- What it does: Launches a debugging shell in a running pod.
- Why use it: Quickly troubleshoot issues inside containers, even if your image lacks a shell.
- Install:
kubectl krew install debug-shell - Usage:
kubectl debug-shell myapp-pod-12345 - Example:
Your app is crashing, and you need to inspect the filesystem or environment variables inside the pod.
5. resource-capacity – Cluster Resource Overview
- What it does: Shows CPU and memory requests/limits for nodes and pods.
- Why use it: Helps you understand resource allocation and avoid over/under-provisioning.
- Install:
kubectl krew install resource-capacity - Usage:
kubectl resource-capacity
kubectl resource-capacity --pods --util - Example:
You want to see which pods are using the most memory in your cluster.
6. tree – Resource Hierarchies
- What it does: Visualizes owner relationships (e.g., Deployment → ReplicaSet → Pod).
- Why use it: Makes it easy to understand how resources are connected.
- Install:
kubectl krew install tree - Usage:
kubectl tree deployment myapp - Example:
You want to see all ReplicaSets and Pods created by a Deployment:
kubectl tree deployment myapp
7. neat – Clean Up YAML Output
- What it does: Removes clutter from
kubectlYAML output, showing only the essentials. - Why use it: Makes configs easier to read and review.
- Install:
kubectl krew install neat - Usage:
kubectl get pod mypod -o yaml | kubectl neat - Example:
You want to share a resource definition without all the status and metadata noise.
8. whoami – Show Current User
- What it does: Displays the Kubernetes user your current context is authenticated as.
- Why use it: Useful for debugging RBAC issues and verifying your identity.
- Install:
kubectl krew install whoami - Usage:
kubectl whoami - Example:
You're unsure which user or service account your kubeconfig is using.
Best Practices for Plugin Usage
- Keep Plugins Updated:
Regularly run
kubectl krew upgradeto update all installed plugins. - Standardize Across Teams: Agree on a core set of plugins for your team and document their usage.
- Document Custom Plugins: If you write your own plugins, add clear usage docs for your team.
- Security: Only install plugins from trusted sources.
Troubleshooting
- Plugin Not Found: Make sure Krew is in your PATH. Restart your terminal if needed.
- Permission Issues: Some plugins require extra permissions (e.g., to read logs or exec into pods).
- Plugin Fails to Run:
Run
kubectl krew updateandkubectl krew upgradeto ensure everything is up to date.
Further Reading & Resources
With these plugins and best practices, you'll be able to manage your Kubernetes clusters more efficiently and with greater confidence. Happy clustering!