吾八哥博客

您现在的位置是:首页 > 云原生 > Kubernetes > 正文

Kubernetes

macOs和Linux环境下kubectl命令自动补齐的方法

吾八哥2020-05-30Kubernetes2764

kubectl命令自动补齐帮助说明

Kubernetes提供了命令补齐的帮助说明,执行如下命令:

5bug-MacBook:~/codes/projects/k8s-demo$ kubectl completion --help
Output shell completion code for the specified shell (bash or zsh). The shell code must be evaluated to provide
interactive completion of kubectl commands.  This can be done by sourcing it from the .bash_profile.

 Detailed instructions on how to do this are available here:
https://kubernetes.io/docs/tasks/tools/install-kubectl/#enabling-shell-autocompletion

 Note for zsh users: [1] zsh completions are only supported in versions of zsh >= 5.2

Examples:
  # Installing bash completion on macOS using homebrew
  ## If running Bash 3.2 included with macOS
  brew install bash-completion
  ## or, if running Bash 4.1+
  brew install bash-completion@2
  ## If kubectl is installed via homebrew, this should start working immediately.
  ## If you've installed via other means, you may need add the completion to your completion directory
  kubectl completion bash > $(brew --prefix)/etc/bash_completion.d/kubectl


  # Installing bash completion on Linux
  ## If bash-completion is not installed on Linux, please install the 'bash-completion' package
  ## via your distribution's package manager.
  ## Load the kubectl completion code for bash into the current shell
  source <(kubectl completion bash)
  ## Write bash completion code to a file and source if from .bash_profile
  kubectl completion bash > ~/.kube/completion.bash.inc
  printf "
  # Kubectl shell completion
  source '$HOME/.kube/completion.bash.inc'
  " >> $HOME/.bash_profile
  source $HOME/.bash_profile

  # Load the kubectl completion code for zsh[1] into the current shell
  source <(kubectl completion zsh)
  # Set the kubectl completion code for zsh[1] to autoload on startup
  kubectl completion zsh > "${fpath[1]}/_kubectl"

Usage:
  kubectl completion SHELL [options]

Use "kubectl options" for a list of global command-line options (applies to all commands).

这里实际上已经说明macOs下和linxu下的使用方法了,详细的说明内容在如下链接里:

https://kubernetes.io/docs/tasks/tools/install-kubectl/#enabling-shell-autocompletion

macOs下自动补齐

根据bash版本安装不同版本的bash-completion插件

Bash 3.2版本安装:

brew install bash-completion

Bash 4.1+的版本安装:

brew install bash-completion@2

安装完成后会有如下提示:

688f250d-dd2a-41e4-91b2-16e4103e126c.png

按照提示,在~/.bash_profile文件里增加如下内容:

[[ -r "/usr/local/etc/profile.d/bash_completion.sh" ]] && . "/usr/local/etc/profile.d/bash_completion.sh"

将kubectl的completion添加到completion目录下

kubectl completion bash > $(brew --prefix)/etc/bash_completion.d/kubectl

重启终端后kubectl的自动补齐命令就配置OK了!

Linux下自动补齐

安装bash-completion插件

这里以centos为例,根据官方文档说明,需要执行如下命令安装补齐插件:

yum install -y bash-completion

在~/.bashrc文件里增加如下内容:

source /usr/share/bash-completion/bash_completion
echo 'source <(kubectl completion bash)' >>~/.bashrc

将kubectl的completion添加到completion目录下

kubectl completion bash >/etc/bash_completion.d/kubectl

同样需要重启终端才能让自动补齐命令功能生效。