kubectl在shell中的自动补全

在zsh上设置

1
source <(kubectl completion zsh)

可以导入到.zshrc中实现自动加载:

1
kubectl completion zsh >> ~/.zshrc

键入-n以后,按tab,自动弹出可用的ns:

1
2
➜  admin kubectl -n
default demo-echo demo-webshell kong kube-public kube-system

在linux上设置

1
2
3
4
yum install bash-completion
source /usr/share/bash-completion/bash_completion
echo 'source <(kubectl completion bash)' >>~/.bashrc
kubectl completion bash >/etc/bash_completion.d/kubectl

在mas上设置-bash

1
2
3
4
5
brew install bash-completion@2
export BASH_COMPLETION_COMPAT_DIR=/usr/local/etc/bash_completion.d
[[ -r /usr/local/etc/profile.d/bash_completion.sh ]] && . /usr/local/etc/profile.d/bash_completion.sh
echo 'source <(kubectl completion bash)' >>~/.bashrc
kubectl completion bash >/usr/local/etc/bash_completion.d/kubectl
Donate