kubectl插件的创建和使用

制作kubectl的插件非常简单,只需要创建以kubectl-作为前缀的可执行文件,并放在系统的可执行文件存放目录(比如/usr/local/bin)下面,kubectl就能够自动找到这些插件。下面举个例子作为说明。

我们在/usr/local/bin下面创建一个kubectl-whoami文件,内容如下:

#!/bin/bash

# this plugin makes use of the `kubectl config` command in order to output
# information about the current user, based on the currently selected context
kubectl config view --template='{{ range .contexts }}{{ if eq .name "'$(kubectl config current-context)'" }}Current user: {{ .context.user }}{{ end }}{{ end }}'

然后我们把这个文件设为可执行文件:

chmod a+x kubectl-whoami

这样就已经完成了一个插件的制作,这个whoami的插件可以用于查看当前的k8s用户,我们可以尝试运行

kubectl whoami

将会看到类似下面的输出:

Current user: minikube

想查看系统装了哪些kubectl插件,可以运行:

kubectl plugin list

像我们刚刚只制作了一个插件,那么看到的输出应该是:

The following compatible plugins are available:

/usr/local/bin/kubectl-whoami
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容