Advanced kubectl Usage: Command Completion, Resource Explanation, Custom Columns, Context Switching, and Plugins
This guide explains how kubectl interacts with the Kubernetes API, demonstrates command‑completion setup for Bash, Zsh and macOS, shows how to view resource specifications, use custom‑column output, switch clusters and namespaces via kubeconfig or kubectx, and extend functionality with plugins such as krew.
Before mastering kubectl productivity tricks, understand that kubectl is a client for the Kubernetes HTTP REST API; every operation is an API request.
Creating resources (e.g., a ReplicaSet from replicaset.yaml) translates to a POST request to /apis/apps/v1/namespaces/{namespace}/replicasets, and the API server stores the definition in etcd, triggering controllers, the scheduler, and kubelet to run pods.
Command Completion
Enable tab‑completion for kubectl in Bash or Zsh with:
kubectl completion bash kubectl completion zshBash
Linux
Install bash-completion (e.g., apt-get install bash-completion or yum install bash-completion) and source the script:
source <(kubectl completion bash)macOS
Install a newer Bash via Homebrew ( brew install bash), then install bash-completion@2 and source the completion script similarly.
Zsh
Add to ~/.zshrc: source <(kubectl completion zsh) Optionally enable compdef and define an alias:
autoload -Uz compinit
compinit
echo 'alias k=kubectl' >> ~/.zshrc
echo 'complete -F __start_kubectl k' >> ~/.zshrcViewing Resource Specs
Use kubectl explain to display a resource’s schema, optionally with --recursive for full depth, and list available resources with kubectl api-resources.
Custom Column Output
Define columns with
-o custom-columns=<header>:<jsonpath>[,<header>:<jsonpath>].... Example to list pod names and nodes:
kubectl get pods -o custom-columns='NAME:metadata.name,NODE:spec.nodeName'JSONPath supports list selectors, filters, and wildcards (e.g., spec.containers[*].image).
Multi‑Cluster and Namespace Switching
kubectl reads ~/.kube/config (or the KUBECONFIG env var) to determine the current context, which includes cluster endpoint, user credentials, and namespace. Switch contexts with kubectl config use-context or edit the file directly.
Merge multiple kubeconfig files:
KUBECONFIG=$HOME/.kube/config:$HOME/.kube/other-config kubectl config view --merge --flatten > $HOME/.kube/configTools like kubectx and kubens (installable via kubectl krew install ctx or Homebrew) simplify switching.
kubectl Plugins
Since v1.12, kubectl supports plugins named kubectl‑<name>. Install plugins by placing the executable in PATH. The krew index helps discover and manage plugins:
# Search plugins
kubectl krew search <query>
# Install a plugin
kubectl krew install <plugin>
# List installed plugins
kubectl krew listExample: a simple plugin to list pod images ( kubectl‑img) containing:
#!/bin/bash
kubectl get pods -o custom-columns='NAME:metadata.name,IMAGES:spec.containers[*].image'Make it executable ( chmod +x kubectl-img) and place it in PATH to run kubectl img.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
DevOps Cloud Academy
Exploring industry DevOps practices and technical expertise.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
