1分钟配置Kubectl命令自动补全功能
Posted wx60c1f94c784b4
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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.noarch 1/1
验证中 : 1:bash-completion-2.1-8.el7.noarch 1/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补全脚本程序:
[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="-v"
else
BASH_COMPLETION_ORIGINAL_V_VALUE="+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
kubeadm kubectl kubelet
[root@k8s-master ~]# kubectl --按TAB键两次,提示kubectl后面可跟的命令
annotate certificate debug explain options run wait
api-resources cluster-info delete expose patch scale
api-versions completion describe get plugin set
apply config diff help port-forward taint
attach cordon drain kustomize proxy top
auth cp edit label replace uncordon
autoscale create exec logs rollout version
[root@k8s-master ~]# kubectl e
edit exec explain expose
[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
namespaces networkpolicies.networking.k8s.io nodes
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-default set-property show-environment start stop switch-root
set-environment show snapshot status suspend
[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= 《三》kubectl命令行管理工具YAML配置详解