1分钟配置Kubectl命令自动补全功能

亦余心之所善兮,虽九死其犹未悔。这篇文章主要讲述1分钟配置Kubectl命令自动补全功能相关的知识,希望能为你提供帮助。

前言:?在使用bash命令行时,在提示符下输入某个命令的前面几个字符,然后按TAB键,就会列出以这几个字符开头的命令供我们选择。不光如此,还可以进行参数补全,但只限于文件参数,当输入到参数部分时,按TAB键,就会列出以这个参数开头的文件路径供我们选择。 现在??http://www.caliban.org/bash/??上提供了一个名为bash-completion的软件包,通过一个复杂的脚本bash_completion实现可编程的补全程序,减少系统管理员日常维护工作,减少差错,提高工作效率。
网站:???bash-completion补全工具官网:https://caliban.org/bash/??
安装:?在日常管理k8s集群的时候,时刻都会使用到Kubectl命令行工具,但是该命令还是挺复杂的,使用中也记不住那么多的api选项,所以这里介绍一下Kubectl命令补全工具(其实是Linux中bash命令自动补全工具)的安装。
???yum -y install bash-completion??
???source /usr/share/bash-completion/bash_completion??
???source < (kubectl completion bash)??
???echo "source < (kubectl completion bash)" > > ~/.bashrc??

[root@k8s-master ~]# yum install -y bash-completion
...
...
正在安装: 1:bash-completion-2.1-8.el7.noarch1/1
验证中: 1:bash-completion-2.1-8.el7.noarch1/1

已安装:
bash-completion.noarch 1:2.1-8.el7

完毕!

[root@k8s-master ~]# source /usr/share/bash-completion/bash_completion

[root@k8s-master ~]# source < (kubectl completion bash)

[root@k8s-master ~]# echo "source < (kubectl completion bash)" > > ~/.bashrc//应用kubectl的completion到系统环境


查看bash_completion补全脚本程序:
【1分钟配置Kubectl命令自动补全功能】
[root@k8s-master ~]# vim /usr/share/bash-completion/bash_completion
#-*- shell-script -*-
#
#bash_completion - programmable completion functions for bash 4.1+
#
#Copyright ? 2006-2008, Ian Macdonald < ian@caliban.org>
#? 2009-2013, Bash Completion Maintainers
#< bash-completion-devel@lists.alioth.debian.org>
#
#This program is free software; you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation; either version 2, or (at your option)
#any later version.
#
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
#GNU General Public License for more details.
#
#You should have received a copy of the GNU General Public License
#along with this program; if not, write to the Free Software Foundation,
#Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
#The latest version of this software can be obtained here:
#
#http://bash-completion.alioth.debian.org/
#
#RELEASE: 2.1

if [[ $- == *v* ]]; then
BASH_COMPLETION_ORIGINAL_V_VALUE="https://www.songbingjia.com/android/-v"
else
BASH_COMPLETION_ORIGINAL_V_VALUE="https://www.songbingjia.com/android/+v"
fi

if [[ ${BASH_COMPLETION_DEBUG-} ]]; then
set -v
else
set +v
fi

# Set the following to the location of the backwards compat completion dir.
#
: ${BASH_COMPLETION_COMPAT_DIR:=/etc/bash_completion.d}
readonly BASH_COMPLETION_COMPAT_DIR

# Blacklisted completions, causing problems with our code.
#
_blacklist_glob=\'@()\'

# Turn on extended globbing and programmable completion
shopt -s extglob progcomp
...
...


验证:??以下自动补全命令验证在不安装bash_completion工具时是无法实现自动补全的。??
1.Kubectl命令自动补全:

[root@k8s-master ~]# kube
kubeadmkubectlkubelet

[root@k8s-master ~]# kubectl--按TAB键两次,提示kubectl后面可跟的命令
annotatecertificatedebugexplainoptionsrunwait
api-resourcescluster-infodeleteexposepatchscale
api-versionscompletiondescribegetpluginset
applyconfigdiffhelpport-forwardtaint
attachcordondrainkustomizeproxytop
authcpeditlabelreplaceuncordon
autoscalecreateexeclogsrolloutversion

[root@k8s-master ~]# kubectl e
editexecexplainexpose

[root@k8s-master ~]# kubectl exec -
--add-dir-header--kubeconfig=--request-timeout
--alsologtostderr--log-backtrace-at--request-timeout=
--as--log-backtrace-at=-s
--as=--log-dir--server
--as-group--log-dir=--server=
--as-group=--log-file--skip-headers
-c--log-file=--skip-log-headers
--cache-dir--log-file-max-size--stderrthreshold
--cache-dir=--log-file-max-size=--stderrthreshold=
--certificate-authority--log-flush-frequency--stdin
--certificate-authority=--log-flush-frequency=-t
--client-certificate--logtostderr--tls-server-name
--client-certificate=--match-server-version--tls-server-name=
--client-key-n--token
--client-key=--namespace--token=
--cluster--namespace=--tty
--cluster=--one-output--user
--container--password--user=
--container=--password=--username
--context--pod-running-timeout--username=
--context=--pod-running-timeout=-v
-f--profile--v
--filename--profile=--v=
--filename=--profile-output--vmodule
-i--profile-output=--vmodule=
--insecure-skip-tls-verify-q--warnings-as-errors
--kubeconfig--quiet

[root@k8s-master ~]# kubectl get n
namespacesnetworkpolicies.networking.k8s.ionodes


2.Linux中bash命令自动补全:

[root@k8s-master ~]# iptables --
--append--flush--line-numbers--numeric--rename-chain--verbose
--delete--fragment--list--out-interface--replace--version
--delete-chain--in-interface--match--policy--set-counters--zero
--destination--insert--modprobe--proto--source
--exact--jump--new--protocol--tabl

[root@k8s-master ~]# systemctl s
set-defaultset-propertyshow-environmentstartstopswitch-root
set-environmentshowsnapshotstatussuspend

[root@k8s-master ~]# firewall-cmd --
--add-forward-port=--list-services
--add-icmp-block=--list-source-ports
--add-icmp-block-inversion--list-sources
--add-interface=--lockdown-off
--add-lockdown-whitelist-command=--lockdown-on
--add-lockdown-whitelist-context=--panic-off
--add-lockdown-whitelist-uid=--panic-on
--add-lockdown-whitelist-user=--permanent
--add-masquerade--query-forward-port=
--add-port=--query-icmp-block=
--add-protocol=--query-icmp-block-inversion
--add-rich-rule=--query-interface=
--add-service=--query-lockdown
--add-source=--query-lockdown-whitelist-command=
--add-source-port=--query-lockdown-whitelist-context=
--change-interface=--query-lockdown-whitelist-uid=
--change-source=

    推荐阅读