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

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 

## 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 

# 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下的使用方法了,详细的说明内容在如下链接里:

macOs下自动补齐

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

Bash 3.2版本安装:brew install bash-completion

Bash 4.1+的版本安装:brew install bash-completion@2

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

f2a163df8135190597af3fa026296268.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 >~/.bashrc

将kubectl的completion添加到completion目录下kubectl completion bash >/etc/bash_completion.d/kubectl

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值