Sign In

Curriculum 5: Kubectl CLI Cheat Sheet

Bash & Zsh Completion

8 min · 15 XP

Completion and Productivity Tips

Beyond basic shell completion, there are tools and techniques that make working with kubectl significantly faster.

Tab Completion Basics

Once shell completion is set up (see the Shell Completion lesson), Tab does more than complete command names. It completes:

  • Resource types: kubectl get dep<Tab> completes to deployments
  • Resource names: kubectl describe pod my-<Tab> completes to matching pod names
  • Flags: kubectl get pods --<Tab> shows available flags
  • Namespaces: kubectl -n kube-<Tab> completes namespace names

Double-pressing Tab shows all available options when there are multiple matches.

fzf Integration

fzf is a fuzzy finder that supercharges kubectl workflows. Install it first:

# macOS
brew install fzf

# Debian/Ubuntu
sudo apt-get install fzf

Then combine it with kubectl for interactive resource selection:

# Interactively select a pod to describe
kubectl describe pod $(kubectl get pods -o name | fzf)

# Interactively select a pod to view logs
kubectl logs $(kubectl get pods -o name | fzf | sed 's|pod/||') -f

# Switch namespace interactively
kubectl config set-context --current --namespace=$(kubectl get ns -o name | fzf | sed 's|namespace/||')

kubectl Plugins with krew

krew is a plugin manager for kubectl that adds extra commands:

# Install krew (see https://krew.sigs.k8s.io/docs/user-guide/setup/install/)
# Then install useful plugins
kubectl krew install ctx    # switch contexts quickly
kubectl krew install ns     # switch namespaces quickly
kubectl krew install neat   # clean up YAML output

After installing:

# Switch context
kubectl ctx my-cluster

# Switch namespace
kubectl ns development

# Get clean YAML without managed fields
kubectl get pod my-app -o yaml | kubectl neat

Productivity Shell Functions

Create functions for common multi-step tasks:

# Quick pod shell access
kshell() {
  kubectl exec -it "$1" -- /bin/sh
}

# Get events sorted by time
kevents() {
  kubectl get events --sort-by='.lastTimestamp'
}

Key Takeaways

  • Tab completion works for resource types, names, flags, and namespaces
  • fzf adds interactive fuzzy selection to any kubectl workflow
  • krew provides plugins like ctx, ns, and neat for common tasks
  • Shell functions can automate multi-step kubectl operations
  • Investing time in your shell setup pays off daily