如何使用Krew在Kubernetes中安装kubectl插件

时间:2020-02-23 14:31:43  来源:igfitidea点击:

Krew是一个 kubectl用于发现,管理和安装kubectl插件的命令行工具。
它的工作方式类似于yum,brew和apt。
Krew支持超过100个kubectl插件。

对于开发人员,我们可以使用Krew在不同平台上打包和分发插件,并通过集中式存储库将其提供给用户。

在本文中,我们将介绍如何安装和使用Krew为Kubernetes集群安装kubectl插件。
但是首先我们需要安装一个正在运行的Kubernetes集群并安装kubectl。

Krew可以在Windows,Linux和Mac等平台上工作,并支持v1.12及更高版本的kubectl。
我们将介绍在Linux,Mac和Windows上的安装。

安装Krew

在本节中,我们讨论Linux,macOS和Windows系统上的krew安装步骤。

在Linux/Mac上安装Krew

安装Git

## Mac
$brew install git
## Debian/Ubuntu
$sudo apt install git-all
## RHEL/CentOS
$sudo dnf install git-all

2.在终端上使用以下命令安装Krew:

(
  set -x; cd "$(mktemp -d)" &&
  curl -fsSLO "https://github.com/kubernetes-sigs/krew/releases/latest/download/krew.tar.gz" &&
  tar zxvf krew.tar.gz &&
  KREW=./krew-"$(uname | tr '[:upper:]' '[:lower:]')_$(uname -m | sed -e 's/x86_64/amd64/' -e 's/arm.*$/arm/')" &&
  "$KREW" install krew
)

3.成功安装后,我们将看到类似于以下示例的输出:

......
Installing plugin: krew
Installed plugin: krew
\
 | Use this plugin:
 | 	kubectl krew
 | Documentation:
 | 	https://krew.sigs.k8s.io/
 | 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
 |  | 
 |  | To list krew commands and to get help, run:
 |  |   $kubectl krew
 |  | For a full list of available plugins, run:
 |  |   $kubectl krew search
 |  | 
 |  | You can find documentation at
 |  |   https://krew.sigs.k8s.io/docs/user-guide/quickstart/.
 | /
/

4.包括 $HOME/.krew/bin使用以下命令访问PATH环境:

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

在Windows上安装Krew

从这里安装GitDownload git并安装2.
在此处下载krew.exe并将其安装到目录3.
以管理员身份启动cmd,然后导航到我们下载的krew.exe4目录。
使用以下命令安装krew:

krew install krew

5.包括 %USERPROFILE%\.krew\bin目录到 PATH使用此链接上的方法的环境变量

如何使用Krew

现在我们已经安装了Krew,让我们探索如何使用它。
下面是一些我们将要开始使用Krew的命令。

$kubectl krew update

2.发现可用的Krew插件:

$kubectl krew search
NAME                            DESCRIPTION                                         INSTALLED
access-matrix                   Show an RBAC access matrix for server resources     yes
advise-psp                      Suggests PodSecurityPolicies for cluster.           no
allctx                          Run commands on contexts in your kubeconfig         no
apparmor-manager                Manage AppArmor profiles for cluster.               no
auth-proxy                      Authentication proxy to a pod or service            no
bd-xray                         Run Black Duck Image Scans                          no
bulk-action                     Do bulk actions on Kubernetes resources.            no
ca-cert                         Print the PEM CA certificate of the current clu...  no
capture                         Triggers a Sysdig capture to troubleshoot the r...  no
cert-manager                    Manage cert-manager resources inside your cluster   no
change-ns                       View or change the current namespace via kubectl.   no
cilium                          Easily interact with Cilium agents.                 no
cluster-group                   Exec commands across a group of contexts.           no
config-cleanup                  Automatically clean up your kubeconfig              no
config-registry                 Switch between registered kubeconfigs               no
creyaml                         Generate custom resource YAML manifest              no
......

3.从Krew安装插件

$kubectl krew install pod-logs
Updated the local copy of plugin index.
Installing plugin: pod-logs
Installed plugin: pod-logs
\
 | Use this plugin:
 | 	kubectl pod-logs
 | Documentation:
 | 	https://github.com/danisla/kubefunc
/
WARNING: You installed plugin "pod-logs" from the krew-index plugin repository.
   These plugins are not audited for security by the Krew maintainers.
   Run them at your own risk.

4.使用我们已安装的插件–我们现在可以使用kubectl执行该插件

$kubectl pod-logs
1)   nginx-ingress-1605802027-controller-ch6ct                  default               Running
2)   dashboard-metrics-scraper-79c5968bdc-6p4fv                 kubernetes-dashboard  Running
3)   kubernetes-dashboard-665f4c5ff-bgrm2                       kubernetes-dashboard  Running  
4)   faas-idler-674fd4544c-tz72x                                openfaas              Running
5)   gateway-689746ff97-jqrst                                   openfaas              Running
6)   nats-cdc589ff7-z8zf2                                       openfaas              Running
Select a Pod: 1
I1119 16:09:26.542094       6 flags.go:205] Watching for Ingress class: nginx
------------------------------------------------------------------------------
NGINX Ingress controller
  Release:       v0.34.1
  Build:         v20170715-ingress-nginx-2.11.0-8-gda5fa45e2
  Repository:    https://github.com/kubernetes/ingress-nginx
  nginx version: nginx/1.19.1
------------------------------------------------------------------------------
W1119 16:09:26.544149       6 flags.go:250] SSL certificate chain completion is disabled (--enable-ssl-chain-completion=false)
W1119 16:09:26.544237       6 client_config.go:552] Neither --kubeconfig nor --master was specified.  Using the inClusterConfig.  This might not work.
I1119 16:09:26.544553       6 main.go:231] Creating API client for https://10.96.0.1:443
I1119 16:09:26.554426       6 main.go:275] Running in Kubernetes cluster version v1.19 (v1.19.4) - git (clean) commit d360454c9bcd1634cf4cc52d1867af5491dc9c5f - platform linux/amd64
I1119 16:09:26.609756       6 main.go:87] Validated default/nginx-ingress-1605802027-default-backend as the default backend.
....

5.要更新插件,请使用以下命令:

Hyman@theitroad:~# kubectl krew upgrade
Updated the local copy of plugin index.
Upgrading plugin: access-matrix
Skipping plugin access-matrix, it is already on the newest version
Upgrading plugin: krew
Skipping plugin krew, it is already on the newest version
Upgrading plugin: ns
Skipping plugin ns, it is already on the newest version
Upgrading plugin: pod-logs
Skipping plugin pod-logs, it is already on the newest version

6.列出已安装的插件

$kubectl krew list
PLUGIN         VERSION
access-matrix  v0.4.5
krew           v0.4.0
ns             v0.9.1
pod-logs       v1.0.1

7.要使用Krew删除插件,请执行以下操作:

$kubectl krew remove pod-logs
Uninstalled plugin pod-logs