1.kubectl插件机制简介

kubectl插件机制在Kubernetes 1.14宣布稳定,进入GA状态。kubectl的插件机制就是希望允许开发者以独立的二进制形式发布自定义的kubectl子命令。

kubectl插件可以使用任意语言开发,如可以是一个bash、python的脚本,也可以是其他语言开发编译的二进制可执行文件,只要最终将脚本或二进制可执行文件以kubectl-的前缀放到PATH中即可。使用kubectl plugin list可以在PATH中查看有哪些插件。

1
2
3
kubectl plugin list

error: unable to find any kubectl plugins in your PATH

Kubernetes提供了一个https://github.com/kubernetes/cli-runtime项目,便于我们使用Go语言编写kubectl插件。 官方也给了一个使用Go编写kubectl插件的例子https://github.com/kubernetes/sample-cli-plugin

2.kubectl插件的包管理工具krew

krew是kubectl插件的管理器,使用krew可以轻松的查找、安装和管理kubectl插件。krew自己也作为一个kubectl插件存在。

2.1 安装krew

确认目标机器上已经安装了git,krew在更新本地插件索引时会用到git。

在kubernetes的管理节点上:

1
2
3
4
wget https://storage.googleapis.com/krew/v0.2.1/krew.tar.gz
wget https://storage.googleapis.com/krew/v0.2.1/krew.yaml

tar -zxvf krew.tar.gz
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
./krew-linux_amd64 install --manifest=krew.yaml --archive=krew.tar.gz
Installing plugin: krew
CAVEATS:
\
 |  krew is now installed! To start using kubectl plugins, you need to add
 |  krew's installation directory to your PATH:
 |
 |    * macOS/Linux:
 |      - Add the following to your ~/.bashrc or ~/.zshrc:
 |          export PATH="${KREW_ROOT:-$HOME/.krew}/bin:$PATH"
 |      - Restart your shell.
 |
 |    * Windows: Add %USERPROFILE%\.krew\bin to your PATH environment variable
 |
 |  Run "kubectl krew" to list krew commands and get help.
 |  You can find documentation at https://github.com/GoogleContainerTools/krew.
/
Installed plugin: krew


ls ~/.krew/bin/
kubectl-krew

可以看到可执行文件kubectl-krew被安装到了$HOME/.krew/bin路径下。接下来将$HOME/.krew/bin加入到PATH环境变量中。

再次查看当前可用的kubectl plugin,发现多了一个kubect-krew:

1
2
3
4
kubectl plugin list
The following compatible plugins are available:

/root/.krew/bin/kubectl-krew

2.2 使用krew

kubectl krew命令的帮助信息如下:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
kubectl krew
krew is the kubectl plugin manager.
You can invoke krew through kubectl: "kubectl krew [command]..."

Usage:
  krew [command]

Available Commands:
  help        Help about any command
  info        Show information about a kubectl plugin
  install     Install kubectl plugins
  list        List installed plugins
  remove      Uninstall plugins
  search      Discover kubectl plugins
  update      Update the local copy of the plugin index
  upgrade     Upgrade installed plugins to newer versions
  version     Show krew version and diagnostics

Flags:
  -h, --help      help for krew
  -v, --v Level   log level for V logs

Use "krew [command] --help" for more information about a command.

更新本地插件索引:

1
2
kubectl krew update
Updated the local copy of plugin index.

查看所有可用插件:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
kubectl krew search
NAME                           DESCRIPTION                                        STATUS
access-matrix                  Show an access matrix for all resources            available
bulk-action                    Do bulk actions on Kubernetes resources.           available
ca-cert                        Print the PEM CA certificate of the current clu... available
change-ns                      View or change the current namespace via kubectl.  available
cssh                           SSH into Kubernetes nodes                          available
debug-shell                    Create pod with interactive kube-shell.            available
exec-as                        Like kubectl exec, but offers a `user` flag to ... available
get-all                        Like 'kubectl get all', but _really_ everything    available
gke-credentials                Fetch credentials for GKE clusters                 available
ingress-nginx                  Interact with ingress-nginx                        available
krew                           Package manager for kubectl plugins.               installed
kubesec-scan                   Scan Kubernetes resources with kubesec.io.         available
match-name                     Match names of pods and other API objects          available
mtail                          Tail logs from multiple pods matching label sel... available
node-admin                     List nodes and run privileged pod with chroot      available
oidc-login                     Login for OpenID Connect authentication            available
open-svc                       Open the Kubernetes URL(s) for the specified se... available
pod-logs                       Display a list of pods to get logs from            available
pod-shell                      Display a list of pods to execute a shell in       available
prompt                         Prompts for user confirmation when executing co... available
rbac-lookup                    Reverse lookup for RBAC                            available
rbac-view                      A tool to visualize your RBAC permissions.         available
resource-capacity              Provides an overview of resource requests, limi... available
restart                        Restarts a pod with the given name                 available
rm-standalone-pods             Remove all pods without owner references           available
sniff                          easly start a remote packet capture on kubernet... available
ssh-jump                       A kubectl plugin to SSH into Kubernetes nodes u... available
sudo                           Run Kubernetes commands impersonated as group s... available
view-secret                    Decode secrets                                     available
view-serviceaccount-kubeconfig Show a kubeconfig setting to access the apiserv... available
view-utilization               Shows cluster cpu and memory utilization           available
warp                           Sync and execute local files in Pod                available

下面体验安装一下change-ns这个kubectl plugin:

1
2
3
4
5
6
7
8
kubectl krew install change-ns
Updated the local copy of plugin index.
Installing plugin: change-ns
CAVEATS:
\
 |  This plugin requires an existing KUBECONFIG file, with a `current-context` field set.
/
Installed plugin: change-ns
1
2
3
4
5
6
7
8
kubectl change-ns ingress-nginx
namespace changed to "ingress-nginx"


kubectl get pod
NAME                                             READY   STATUS    RESTARTS   AGE
nginx-ingress-controller-575d546f7c-jsvhv        1/1     Running   2          44h
nginx-ingress-default-backend-7c5fb8d8f4-ncskr   1/1     Running   2          2d

这里体验安装了https://github.com/juanvallejo/kubectl-ns

参考