kubectl completion
Generate autocompletion scripts for bash, zsh, fish, or PowerShell. Dramatically improves kubectl productivity.
kubectl completion SHELL [flags]Common Flags
| Flag | Short | Description |
|---|---|---|
| bash | — | Generate bash completion script |
| zsh | — | Generate zsh completion script |
| fish | — | Generate fish completion script |
| powershell | — | Generate PowerShell completion script |
Examples
Enable bash completion for the current session
source <(kubectl completion bash)Add bash completion to your profile permanently
echo 'source <(kubectl completion bash)' >> ~/.bashrcEnable zsh completion
source <(kubectl completion zsh)Enable completion for a kubectl alias
alias k=kubectl && complete -o default -F __start_kubectl kGenerate fish completion
kubectl completion fish | sourceWhen to Use kubectl completion
kubectl completion generates shell autocompletion scripts that let you tab-complete kubectl commands, resource types, resource names, flags, and even namespace names. It is one of the first things to set up on any machine where you use kubectl.
Bash Setup
# Prerequisites: install bash-completion
# macOS
brew install bash-completion@2
# Ubuntu/Debian
sudo apt-get install bash-completion
# Enable for current session
source <(kubectl completion bash)
# Enable permanently
echo 'source <(kubectl completion bash)' >> ~/.bashrc
source ~/.bashrc
After setup, tab completion works for:
- Commands:
kubectl g<TAB>completes tokubectl get - Resource types:
kubectl get de<TAB>completes tokubectl get deployments - Resource names:
kubectl get pods my-<TAB>completes with actual pod names - Flags:
kubectl get pods --<TAB>shows available flags - Namespaces:
kubectl -n ku<TAB>completes tokubectl -n kube-system
Zsh Setup
# Add to .zshrc
echo 'source <(kubectl completion zsh)' >> ~/.zshrc
# If you get "command not found: compdef", add this first:
echo 'autoload -Uz compinit && compinit' >> ~/.zshrc
# Reload
source ~/.zshrc
Fish Setup
# Enable for current session
kubectl completion fish | source
# Enable permanently
kubectl completion fish > ~/.config/fish/completions/kubectl.fish
PowerShell Setup
# Enable for current session
kubectl completion powershell | Out-String | Invoke-Expression
# Enable permanently
kubectl completion powershell >> $PROFILE
Alias Completion
Most Kubernetes users alias kubectl to k. To get completion with the alias:
# Bash
alias k=kubectl
complete -o default -F __start_kubectl k
echo 'alias k=kubectl' >> ~/.bashrc
echo 'complete -o default -F __start_kubectl k' >> ~/.bashrc
# Zsh
alias k=kubectl
compdef __start_kubectl k
echo 'alias k=kubectl' >> ~/.zshrc
echo 'compdef __start_kubectl k' >> ~/.zshrc
Now k get po<TAB> works the same as kubectl get po<TAB>.
What Completion Provides
| Input | Tab Completes |
|-------|---------------|
| kubectl <TAB> | All subcommands (get, apply, delete, etc.) |
| kubectl get <TAB> | All resource types (pods, services, etc.) |
| kubectl get pods <TAB> | Actual pod names from the cluster |
| kubectl get pods -n <TAB> | All namespace names |
| kubectl --context=<TAB> | All context names |
| kubectl get pods -o <TAB> | Output formats (json, yaml, wide, etc.) |
Performance Considerations
Completion queries the API server to fetch resource names, which means:
- It requires an active cluster connection.
- Large clusters with many resources may have slightly slower completion.
- If the cluster is unreachable, resource name completion won't work (but command and flag completion still works).
Exam Tip: CKA/CKAD
In the CKA and CKAD exams, kubectl completion is usually pre-configured. However, knowing how to set it up is valuable:
# Quick setup at the start of an exam
source <(kubectl completion bash)
alias k=kubectl
complete -o default -F __start_kubectl k
This saves significant time on an exam where every second counts.
Custom Completion for Scripts
If you write wrapper scripts around kubectl, you can leverage completion in your own tools:
# A deployment helper function with completion
deploy() {
kubectl apply -f "manifests/$1.yaml"
}
# Add completion for the function
_deploy() {
COMPREPLY=($(compgen -W "$(ls manifests/ | sed 's/\.yaml$//')" -- "${COMP_WORDS[1]}"))
}
complete -F _deploy deploy
Troubleshooting Completion
# Check if completion is loaded
type __start_kubectl
# Should output: __start_kubectl is a function
# Check bash-completion is working
type _init_completion
# Should output: _init_completion is a function
# Regenerate if completion seems outdated
kubectl completion bash > /tmp/kubectl-completion.bash
source /tmp/kubectl-completion.bash
# For zsh, clear the completion cache
rm -f ~/.zcompdump*
exec zsh
Autocompletion is not just a convenience — it reduces errors, helps discover available flags, and significantly speeds up daily Kubernetes operations. Set it up on every machine where you use kubectl.
Interview Questions About This Command
Common Mistakes
- Not installing bash-completion package, which is required for kubectl completion to work on Linux.
- Forgetting to reload the shell or source the profile after adding completion.
- Setting up completion for bash when using zsh (or vice versa), resulting in no completion.