k8s常用命令kubectl之定义plugin

kubectl可以自定义插件,允许你使用任何语言,只要你生成可执行的文件,并且以kubectl-开头
我们可以用shell自定义一个j简单的hello kubectl

vim kubectl-hello

输入,保存退出

#! /bin/sh
echo hello kubectl
chomd a+x kubectl-hello
mv ./kubectl-hello /usr/local/bin

然后执行可以看到输出,一个简单的plugin就制作好了

~# kubectl hello
hello kubectl

然后我们可以使用以下命令列出所有的插件

~# kubectl plugin list
The following compatible plugins are available:

/usr/local/bin/kubectl-hello

接下来来个复杂点的

vim 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: {
    
    { printf "%s\n" .context.user }}{
    
    { end }}{
    
    { end }}'

然后执行授权并复制到/usr/local/bin目录下

chmod a+x kubectl-whoami
mv ./kubectl-whoami /usr/local/bin

然后执行

~# kubectl whoami
Current user: kubernetes-admin

插件可以对一个批量命令集合进行再包装,做一个复杂操作不用再一条条命令去敲是不是想的很周到?

猜你喜欢

转载自blog.csdn.net/a807719447/article/details/115279884
今日推荐