kubectl plugin插件机制初体验之krew

kubectl plugin插件机制初体验之krew

2019-04-09
Kubernetes

1.kubectl插件机制简介 #

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

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

1kubectl plugin list
2
3error: 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的管理节点上:

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

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

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

1kubectl plugin list
2The following compatible plugins are available:
3
4/root/.krew/bin/kubectl-krew

2.2 使用krew #

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

 1kubectl krew
 2krew is the kubectl plugin manager.
 3You can invoke krew through kubectl: "kubectl krew [command]..."
 4
 5Usage:
 6  krew [command]
 7
 8Available Commands:
 9  help        Help about any command
10  info        Show information about a kubectl plugin
11  install     Install kubectl plugins
12  list        List installed plugins
13  remove      Uninstall plugins
14  search      Discover kubectl plugins
15  update      Update the local copy of the plugin index
16  upgrade     Upgrade installed plugins to newer versions
17  version     Show krew version and diagnostics
18
19Flags:
20  -h, --help      help for krew
21  -v, --v Level   log level for V logs
22
23Use "krew [command] --help" for more information about a command.

更新本地插件索引:

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

查看所有可用插件:

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

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

1kubectl krew install change-ns
2Updated the local copy of plugin index.
3Installing plugin: change-ns
4CAVEATS:
5\
6 |  This plugin requires an existing KUBECONFIG file, with a `current-context` field set.
7/
8Installed plugin: change-ns
1kubectl change-ns ingress-nginx
2namespace changed to "ingress-nginx"
3
4
5kubectl get pod
6NAME                                             READY   STATUS    RESTARTS   AGE
7nginx-ingress-controller-575d546f7c-jsvhv        1/1     Running   2          44h
8nginx-ingress-default-backend-7c5fb8d8f4-ncskr   1/1     Running   2          2d

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

参考 #

© 2024 青蛙小白