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.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=                      --query-lockdown-whitelist-user=
--change-zone=                        --query-masquerade
--complete-reload                     --query-panic
--direct                              --query-port=
--get-active-zones                    --query-protocol=
--get-default-zone                    --query-rich-rule=
--get-description                     --query-service=
--get-helpers                         --query-source=
--get-icmptypes                       --query-source-port=
--get-ipset-types                     --reload
--get-log-denied                      --remove-forward-port=
--get-services                        --remove-icmp-block=
--get-short                           --remove-icmp-block-inversion
--get-zone-of-interface=              --remove-interface=
--get-zones                           --remove-lockdown-whitelist-command=
--help                                --remove-lockdown-whitelist-context=
--info-helper=                        --remove-lockdown-whitelist-uid=
--info-icmptype=                      --remove-lockdown-whitelist-user=
--info-ipset=                         --remove-masquerade
--info-service=                       --remove-port=
--info-zone=                          --remove-protocol=
--list-all                            --remove-rich-rule=
--list-all-zones                      --remove-service=
--list-forward-ports                  --remove-source=
--list-icmp-blocks                    --remove-source-port=
--list-interfaces                     --set-default-zone=
--list-lockdown-whitelist-commands    --set-description=
--list-lockdown-whitelist-contexts    --set-log-denied=
--list-lockdown-whitelist-uids        --set-short=
--list-lockdown-whitelist-users       --state
--list-ports                          --version
--list-protocols                      --zone=
--list-rich-rules
相关实践学习
深入解析Docker容器化技术
Docker是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的Linux机器上,也可以实现虚拟化,容器是完全使用沙箱机制,相互之间不会有任何接口。Docker是世界领先的软件容器平台。开发人员利用Docker可以消除协作编码时“在我的机器上可正常工作”的问题。运维人员利用Docker可以在隔离容器中并行运行和管理应用,获得更好的计算密度。企业利用Docker可以构建敏捷的软件交付管道,以更快的速度、更高的安全性和可靠的信誉为Linux和Windows Server应用发布新功能。 在本套课程中,我们将全面的讲解Docker技术栈,从环境安装到容器、镜像操作以及生产环境如何部署开发的微服务应用。本课程由黑马程序员提供。 &nbsp; &nbsp; 相关的阿里云产品:容器服务 ACK 容器服务 Kubernetes 版(简称 ACK)提供高性能可伸缩的容器应用管理能力,支持企业级容器化应用的全生命周期管理。整合阿里云虚拟化、存储、网络和安全能力,打造云端最佳容器化应用运行环境。 了解产品详情: https://www.aliyun.com/product/kubernetes
相关文章
|
Kubernetes Shell 容器
Kubernetes的kubectl命令补全
Kubernetes的kubectl命令补全
538 0
|
存储 Prometheus Kubernetes
K8s + prometheus + vm(VictoriaMetrics)
K8s + prometheus + vm(VictoriaMetrics)
852 1
|
Java Linux 数据安全/隐私保护
华为云计算FusionCompute虚拟化平台的安装与设置
华为云计算FusionCompute虚拟化平台的安装与设置
1478 0
华为云计算FusionCompute虚拟化平台的安装与设置
|
iOS开发 MacOS
还在为 iTerm 多窗口操作烦恼?tmux 这款神器轻松帮你解决(下)
粉在之前文章中教过大家如何结合 zsh 让 iterm2 发挥最佳效果。 什么还没有看过?赶紧看下补一下前提知识:收集了这么多实用技巧,帮助你的 iterm2 成为最帅的那个! 上篇文中,阿粉提到每次上线发布的时候,都会打开很多 iTerm 窗口,通过 tab 页拖拽方式让所有窗口可以同时显示。
1421 0
还在为 iTerm 多窗口操作烦恼?tmux 这款神器轻松帮你解决(下)
|
Kubernetes 容器
kubeadm 部署的 k8s 增加 ip 并重新生成证书
kubeadm 部署的 k8s 增加 ip 并重新生成证书
1518 0
|
域名解析 Kubernetes 网络协议
k8s教程(service篇)-pod的dns域名
k8s教程(service篇)-pod的dns域名
3214 0
|
域名解析 Ubuntu Linux
Docker 镜像基本操作
本文介绍 Docker 镜像最常用的三个基本操作 login、pull、push以及如何登录子账户。通过认识镜像的基本操作,您可以更熟练地使用容器镜像服务 ACR。
8446 0
Docker 镜像基本操作
|
Kubernetes 应用服务中间件 nginx
k8s基础使用--使用k8s部署nginx服务
本文介绍了Kubernetes中核心概念Deployment、Pod与Service的基本原理及应用。Pod作为最小调度单元,用于管理容器及其共享资源;Deployment则负责控制Pod副本数量,确保其符合预期状态;Service通过标签选择器实现Pod服务的负载均衡与暴露。此外,还提供了具体操作步骤,如通过`kubectl`命令创建Deployment和Service,以及如何验证其功能。实验环境包括一台master节点和两台worker节点,均已部署k8s-1.27。
1147 1
|
Serverless API
【MCP教程系列】在阿里云百炼,实现超级简单的MCP服务部署
阿里云百炼推出业界首个全生命周期MCP服务,支持一键在线注册托管。企业可将自研或外部MCP服务部署于阿里云百炼平台,借助FC函数计算能力,免去资源购买与服务部署的复杂流程,快速实现开发。创建MCP服务仅需四步,平台提供预置服务与自定义部署选项,如通过npx安装代码配置Flomo等服务。还可直接在控制台开通预置服务,体验高效便捷的企业级解决方案。
3546 0