Install kubectl plugins in Kubernetes using Krew

Posted on 227 views

Krew is a kubectl command-line tool used to discover, manage and install kubectl plugins. It works in a similar way like yum, brew and apt. There are over 100 kubectl plugins supported by Krew.

For developers, you can use Krew to package and distribute your plugins on different platforms and make them available to users through a centralized repository.

In this post, we’ll cover how to install and use Krew to install kubectl plugins for your Kubernetes cluster. But you first need to have a running Kubernetes cluster with kubectl installed.

Krew can work across platforms, i.e, Windows, Linux and Mac and supports a v1.12 and above of kubectl. We shall cover installation on Linux, Mac and Windows.

Installation of krew

In this section we discuss krew installation steps on Linux, macOS and Windows systems.

Install krew on Linux/Mac

  1. Install Git
## macOS
$ brew install git

## Debian/Ubuntu
sudo apt install git-all

## RHEL/CentOS
sudo dnf install git-all

2. Install Krew with the command below on your terminal:

(
  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. After a successful installation, you will see an output similar to the sample below:

......
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. Include $HOME/ .krew/bin to your PATH environment with the following command:

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

Install krew on Windows

  1. Install Git
  • Download git from here and install.

2. Download and install krew.exe here to a directory

3. Launch cmd as administrator and navigate to the directory you downloaded krew.exe

4. Install krew using the command below:

krew install krew

5. Include %USERPROFILE%\.krew\bin dir to your PATH environment variable using the method on this link

How to Use Krew

Now that we have installed Krew, let us now explore how to use it.

Below are some of the commands that we will use to get started with Krew.

  1. To download Krew plugin list, which can be compared to updating repo cache using apt or yum:
$ kubectl krew update

2. Discover available Krew plugins:

$ 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. Install plugin from 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. Use the plugin you have installed – you can now execute the plugin using 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:         v20200715-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. To update plugins use the command below:

[email protected]:~# 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. List installed plugins

$ kubectl krew list

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

7. To remove a plugin using Krew:

$ kubectl krew remove pod-logs

Uninstalled plugin pod-logs

Conclusion

Krew can be very handy for management of kubernetes clusters using kubectl. It makes is way easy to get packaged plugins in a centralized place.

Feel free to try to set it up on your cluster as it also has very simple steps.

Gravatar Image
A systems engineer with excellent skills in systems administration, cloud computing, systems deployment, virtualization, containers, and a certified ethical hacker.