「Kubernetes」- 扩展 kubectl 命令、安装 kubectl 插件

  CREATED BY JENKINSBOT

问题描述

除了我们常用的 kubectl get、kubectl delete、kubectl describe 子命令之外,kubectl 还支持某些特殊的子命令。比如 kubectl cert-manager、kubectl ingress 等等特殊子命令,用于完成特定的集群维护任务。

但是,这些子命令需要安装插件,然后才能够使用。比如 kubectl cert-manager 子命令,需要安装 kubectl-cert_manager 插件。

该笔记将记录:在 Kubernetes 中,如何为 kubectl 安装插件,扩展 kubectl 命令的方法,相关问题的处理方法。

解决方案

插件的安装方法有两种:通过手动安装;通过 krew 安装;

鉴于网络环境受限,很难说哪种插件管理方法更便捷。

关于编写插件

参考 Extend kubectl with plugins 插件,获取插件开发方法。

方法一、通过手动安装

鉴于插件文档通常都会介绍自身的安装方法,因此我们不再详细介绍该方法。

方法二、通过 krew 安装

第一步、安装 krew 扩展

# wget https://github.com/kubernetes-sigs/krew/releases/latest/download/krew.tar.gz
...

# tar zxvf krew.tar.gz 
./LICENSE
./krew-darwin_amd64
./krew-darwin_arm64
./krew-linux_amd64
./krew-linux_arm
./krew-linux_arm64
./krew-windows_amd64.exe

# ./krew-linux_amd64 install krew
WARNING: To be able to run kubectl plugins, you need to add
the following to your ~/.bash_profile or ~/.bashrc:

    export PATH="${PATH}:${HOME}/.krew/bin"

and restart your shell.
...

# echo 'export PATH="${PATH}:${HOME}/.krew/bin"' >> ~/.bashrc
# source ~/.bashrc

# kubectl krew help
krew is the kubectl plugin manager.
You can invoke krew through kubectl: "kubectl krew [command]..."
...

第二步、通过 krew 安装其他插件

// 搜索插件

# kubectl krew search cert-manager
NAME          DESCRIPTION                                        INSTALLED
cert-manager  Manage cert-manager resources inside your cluster  no

// 安装插件
// 在安装插件的最后步骤中,极会因为网络原因而导致下载失败
// 此时只能求助于手动安装,或者网络加速(配置环境变量即可)

# kubectl krew install cert-manager
Updated the local copy of plugin index.
Installing plugin: cert-manager
Installed plugin: cert-manager
\
 | Use this plugin:
 |      kubectl cert-manager
 | Documentation:
 |      https://github.com/jetstack/cert-manager
/


// 插件升级

# kubectl krew upgrade cert-manager
Updated the local copy of plugin index.
Upgrading plugin: cert-manager
F0917 01:22:06.322701  391258 root.go:79] failed to upgrade plugin "cert-manager": can't upgrade, the newest version is already installed

参考文献

kubernetes-sigs/krew: 📦 Find and install kubectl plugins
Kubectl plugins available · Krew
Extend kubectl with plugins | Kubernetes