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

本文涉及的产品
容器服务 Serverless 版 ACK Serverless,317元额度 多规格
容器服务 Serverless 版 ACK Serverless,952元额度 多规格
简介: 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
相关实践学习
通过Ingress进行灰度发布
本场景您将运行一个简单的应用,部署一个新的应用用于新的发布,并通过Ingress能力实现灰度发布。
容器应用与集群管理
欢迎来到《容器应用与集群管理》课程,本课程是“云原生容器Clouder认证“系列中的第二阶段。课程将向您介绍与容器集群相关的概念和技术,这些概念和技术可以帮助您了解阿里云容器服务ACK/ACK Serverless的使用。同时,本课程也会向您介绍可以采取的工具、方法和可操作步骤,以帮助您了解如何基于容器服务ACK Serverless构建和管理企业级应用。 学习完本课程后,您将能够: 掌握容器集群、容器编排的基本概念 掌握Kubernetes的基础概念及核心思想 掌握阿里云容器服务ACK/ACK Serverless概念及使用方法 基于容器服务ACK Serverless搭建和管理企业级网站应用
相关文章
|
Kubernetes Shell 容器
Kubernetes的kubectl命令补全
Kubernetes的kubectl命令补全
189 0
|
Shell Docker 容器
笔记:Docker命令自动补全
笔记:Docker命令自动补全 一 背景 作为一名 Linux 重度用户,每次敲命令总会情不自禁地按下 Tab 键。在键入 Docker 命令时也会出现类似的习惯性动作。那么是否 Docker 相关命令也能自动补全呢? 二 操作步骤 2.1 安装 bash-completion sudo yum install -y bash-completion 安装完成之后重启系统或者重新登录 shell。
21713 0
|
2月前
vcpkg 命令行工具的选项说明
vcpkg 命令行工具的选项说明
11 0
|
5月前
|
Shell Docker 容器
mac终端命令补全设置(docker 命令补全)
mac终端命令补全设置(docker 命令补全)
111 0
|
5月前
|
JSON Kubernetes 数据格式
k8s学习-kubectl命令常用选项详解与实战
k8s学习-kubectl命令常用选项详解与实战
93 0
|
10月前
|
运维 Kubernetes Shell
Kubectl命令再也不用记了,自动补全提示真好
Kubectl命令再也不用记了,自动补全提示真好
97 0
|
10月前
|
Ubuntu Unix Shell
Kubectl 好用的命令行工具:oh-my-zsh 技巧和窍门
Kubectl 好用的命令行工具:oh-my-zsh 技巧和窍门
208 0
|
11月前
|
程序员 开发工具 开发者
如何在 Vim 中使用自动完成功能?
如何在 Vim 中使用自动完成功能?
82 0
|
XML 数据格式 Python
yq:命令行操作yaml文件
yq:命令行操作yaml文件
336 2
|
JSON Prometheus Kubernetes
k8s小工具之kubectl-mytop插件
Kubectl-mytop是一个基于metrics-server组件做指标展示的命令行工具,综合kubectl top以及kubectl describe的展示能力,可以提供Kubernetes集群中node以及pod资源请求、资源限制、资源利用率、以及pod数量做排序展示。Kubectl-mytop插件的目的在于弥补当前常用k8s监控工具的不足,便于如资源不均,调度不均,使用率不均等问题的分析定位。如指定node后,可以查看这个node上所有的pod的request,limit,util资源使用等信息并排序输出,支持指定不同namespace的相同的pod label的资源展示,以及过滤指定
359 0