Cloud Native 24 min read

Master kubectl: Essential Tips, Commands, and Plugins for Kubernetes

This guide explains how kubectl interacts with the Kubernetes API, walks through command‑completion setup, custom column output, multi‑cluster and namespace switching, and shows how to extend kubectl with plugins such as kubectx, krew, and custom scripts, boosting productivity for cloud‑native workflows.

Cloud Native Technology Community
Cloud Native Technology Community
Cloud Native Technology Community
Master kubectl: Essential Tips, Commands, and Plugins for Kubernetes

How kubectl works

kubectl is a client that sends HTTP requests to the Kubernetes API server. Each operation corresponds to an API endpoint, e.g. POST /apis/apps/v1/namespaces/{namespace}/replicasets. The API server stores the manifest in etcd, triggers the appropriate controller, which creates Pods that are scheduled, run by kubelet, and start containers.

kubectl request flow diagram
kubectl request flow diagram

Command completion

Enable shell completion to auto‑complete sub‑commands, flags, and resource names.

Generate scripts: kubectl completion bash or kubectl completion zsh Linux Bash: install bash-completion (e.g. apt-get install bash-completion or yum install bash-completion) and source the script with source <(kubectl completion bash) or place it in /etc/bash_completion.d/kubectl.

macOS: install a newer Bash (≥4.1) via Homebrew ( brew install bash) and brew install bash-completion@2, then source the script as above.

Zsh: add source <(kubectl completion zsh) to ~/.zshrc, enable compinit, and optionally define an alias alias k=kubectl.

Bash completion example
Bash completion example

Viewing resource schemas

Use kubectl explain to display a resource’s schema; add --recursive to show the full hierarchy. List available resources with kubectl api-resources.

Custom column output

Customize kubectl get output with

-o custom-columns=<header>:<jsonpath>[,<header>:<jsonpath>]...

. Example to list pod names:

kubectl get pods -o custom-columns='NAME:metadata.name'

To add the node name column:

kubectl get pods -o custom-columns='NAME:metadata.name,NODE:spec.nodeName'

JSONPath supports list selectors ( *), index access ( [0]), and filters ( [?(@.image!="nginx:latest")]).

Example applications

Show pod images:

kubectl get pods -o custom-columns='NAME:metadata.name,IMAGES:spec.containers[*].image'

Show node availability zone (cloud‑deployed clusters):

kubectl get nodes -o custom-columns='NAME:metadata.name,ZONE:metadata.labels.failure-domain\.beta\.kubernetes\.io/zone'

Multi‑cluster and namespace switching

kubectl reads ~/.kube/config (or the file referenced by KUBECONFIG) which defines contexts (cluster, user, namespace). Switch contexts with kubectl config use-context or specify --context. Merge multiple kubeconfig files:

KUBECONFIG=$HOME/.kube/config:$HOME/.kube/second-config \
  kubectl config view --merge --flatten > $HOME/.kube/merged && mv $HOME/.kube/merged $HOME/.kube/config

Kubectx / Kubens

The kubectx and kubens plugins (installable via kubectl krew install ctx and kubectl krew install ns or Homebrew) provide quick commands to change the current context or namespace. They can be combined with kube‑ps1 to display the active context in the prompt.

kubectl plugins

Since version 1.12, kubectl supports plugins named kubectl‑<name>. Place an executable with that name in PATH and invoke it as kubectl <name>. List installed plugins with kubectl plugin list. The krew project indexes many community plugins and provides commands to search, install, upgrade, and remove them.

Creating a simple plugin

Example plugin that lists pod images:

#!/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. Run with kubectl img.

Summary

Understanding that kubectl is an API‑centric client, enabling command completion, customizing output, managing multiple clusters, and extending functionality with plugins are essential techniques for efficient Kubernetes administration.

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.

AutomationKubernetescommand-linePluginskubectl
Cloud Native Technology Community
Written by

Cloud Native Technology Community

The Cloud Native Technology Community, part of the CNBPA Cloud Native Technology Practice Alliance, focuses on evangelizing cutting‑edge cloud‑native technologies and practical implementations. It shares in‑depth content, case studies, and event/meetup information on containers, Kubernetes, DevOps, Service Mesh, and other cloud‑native tech, along with updates from the CNBPA alliance.

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.