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 容器
kubeadm 部署的 k8s 增加 ip 并重新生成证书
kubeadm 部署的 k8s 增加 ip 并重新生成证书
1817 0
|
Kubernetes 容器 Perl
【kubernetes】如何找到k8s内部拉取的镜像
【kubernetes】如何找到k8s内部拉取的镜像
1233 1
|
运维 安全 Linux
Jumpserver堡垒机部署和基本使用
Jumpserver堡垒机部署和基本使用
2396 2
|
存储 JSON 应用服务中间件
|
人工智能 API 数据库
MCP Server 开发实战 | 大模型无缝对接 Grafana
以 AI 世界的“USB-C”标准接口——MCP(Model Context Protocol)为例,演示如何通过 MCP Server 实现大模型与阿里云 Grafana 服务的无缝对接,让智能交互更加高效、直观。
4324 124
|
Kubernetes Ubuntu Linux
Centos7 搭建 kubernetes集群
本文介绍了如何搭建一个三节点的Kubernetes集群,包括一个主节点和两个工作节点。各节点运行CentOS 7系统,最低配置为2核CPU、2GB内存和15GB硬盘。详细步骤包括环境配置、安装Docker、关闭防火墙和SELinux、禁用交换分区、安装kubeadm、kubelet、kubectl,以及初始化Kubernetes集群和安装网络插件Calico或Flannel。
1650 4
|
Kubernetes 应用服务中间件 nginx
k8s基础使用--使用k8s部署nginx服务
本文介绍了Kubernetes中核心概念Deployment、Pod与Service的基本原理及应用。Pod作为最小调度单元,用于管理容器及其共享资源;Deployment则负责控制Pod副本数量,确保其符合预期状态;Service通过标签选择器实现Pod服务的负载均衡与暴露。此外,还提供了具体操作步骤,如通过`kubectl`命令创建Deployment和Service,以及如何验证其功能。实验环境包括一台master节点和两台worker节点,均已部署k8s-1.27。
1517 1
|
Kubernetes 应用服务中间件 nginx
搭建Kubernetes v1.31.1服务器集群,采用Calico网络技术
在阿里云服务器上部署k8s集群,一、3台k8s服务器,1个Master节点,2个工作节点,采用Calico网络技术。二、部署nginx服务到k8s集群,并验证nginx服务运行状态。
5085 3
|
存储 Linux 开发者
虚拟机centos7.9一键部署docker
本文介绍了如何在 CentOS 7.9 虚拟机上安装 Docker 社区版 (Docker-ce-20.10.20)。通过使用阿里云镜像源,利用 `wget` 下载并配置 Docker-ce 的 YUM 仓库文件,然后通过 `yum` 命令完成安装。安装后,通过 `systemctl` 设置 Docker 开机自启并启动 Docker 服务。最后,使用 `docker version` 验证安装成功,并展示了客户端与服务器的版本信息。文中还提供了列出所有可用 Docker-ce 版本的命令。
3543 0
虚拟机centos7.9一键部署docker
|
Linux 数据安全/隐私保护 虚拟化
centos7部署openVPN
centos7部署openVPN
6033 1