Cloud Native 23 min read

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.

DevOps Cloud Academy
DevOps Cloud Academy
DevOps Cloud Academy
Advanced kubectl Usage: Command Completion, Resource Explanation, Custom Columns, Context Switching, and Plugins

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 zsh

Bash

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' >> ~/.zshrc

Viewing 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/config

Tools 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 list

Example: 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.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

CLICloud NativeKubernetesDevOpsPluginskubectl
DevOps Cloud Academy
Written by

DevOps Cloud Academy

Exploring industry DevOps practices and technical expertise.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.