Installing and Managing kubectl Plugins with Krew

Lukas Gentele
Aniket Bhattacharyea
9 min read

kubectl is the de facto standard interface for interacting with a Kubernetes cluster. However, there may be situations where the default behavior of kubectl is not enough. It’s possible to use a kubectl plugin to extend the behavior of kubectl and perform complex tasks. There are many situations where plugins can make life easier. For example, you can perform bulk operations on resources, run assertions, or dry run commands before applying them to your cluster. These are just a few examples, and the possibilities are limitless.

Krew is a tool that makes finding, installing, managing, and publishing kubectl plugins easy. In this tutorial, you’ll learn how to use Krew to install and manage kubectl plugins.

#Kubectl Series

#What Is Krew?

Krew describes itself as a “package manager for kubectl plugins.” Typically, kubectl plugins are executable and can be written and packaged in any format. This means that cross-platform development can be difficult since you need to provide different packages for different operating systems. Krew provides a cross-platform way of packaging plugins so that you don’t have to worry about cross-platform compatibility as a plugin developer.

As a plugin user, you can also benefit from Krew’s online, searchable plugin index. It also manages the installation, updates, and removal of plugins, thus making your life easier.

Note: The plugins hosted via Krew are not audited for security. You should install and run plugins at your own risk, since these are essentially just arbitrary programs.

To follow this tutorial, you’ll need:

  • A Kubernetes cluster. You can create a local cluster with something like minikube. This article uses Kubernetes 1.25.3 with minikube 1.28.0.
  • kubectl configured to interact with your cluster.

#CrowKrew’s Request

For the purposes of this tutorial, you’re going to use the example of a smart farming company called CrowKrew LLC that must run assertions on Kubernetes clusters to ensure the different components are working. The company wants to assert:

  • Whether a certain resource (pod, deployment, etc.) with specific properties exists. For example, is there a pod in the “Running” status that has the label “nginx”?
  • Whether there are certain numbers of resources present. For example, are there exactly three pods in the “Running” stage?
  • Whether certain resources are in the expected state. For example, are all the pods in the “backend” namespace running?

Doing this with only kubectl can be difficult, so CrowKrew LLC hired you as a Kubernetes expert who can implement the above requirements in a simpler, cleaner way. You’ll use a plugin like KubeAssert to achieve the goals above, and you’ll use Krew to manage the plugin lifecycle.

#Installing Krew

Krew’s installation steps depend on your OS (Windows, macOS, or Linux) and what shell you’re running (Bash, ZSH, or Fish). You can find instructions for all these cases in the installation documentation.

After the installation, you can check whether or not it was successful by running kubectl krew. You should see the following output:

$ kubectl krew

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

Usage:
  kubectl krew [command]

Available Commands:
  completion  generate the autocompletion script for the specified shell
  help    	Help about any command
  index   	Manage custom plugin indexes
  info    	Show information about an available plugin
  install 	Install kubectl plugins
  list    	List installed kubectl plugins
  search  	Discover kubectl plugins
  uninstall   Uninstall plugins
  update  	Update the local copy of the plugin index
  upgrade 	Upgrade installed plugins to newer versions
  version 	Show krew version and diagnostics

Flags:
  -h, --help  	help for krew
  -v, --v Level   number for the log level verbosity

Use "kubectl krew [command] --help" for more information about a command.

#Using Krew

Krew can be invoked through the kubectl krew command. Krew has a wide range of uses, including the following examples that you’ll learn how to do in this article:

  • Searching plugins
  • Installing and using plugins
  • Upgrading plugins
  • Listing all installed plugins
  • Uninstalling plugins

#Searching Plugins

As mentioned, Krew hosts a repository of plugins known as the krew-index. This repository stores a list of all available plugins and their metadata. When a plugin developer wishes to publish a new plugin or wants to release a new version of an existing plugin, they need to make a pull request to the krew-index detailing their changes. Each plugin has its own YAML file in the plugins folder. For example, the KubeAssert plugin has a corresponding assert.yaml file:

apiVersion: krew.googlecontainertools.github.com/v1alpha2
kind: Plugin
metadata:
  name: assert
spec:
  homepage: https://github.com/morningspace/kubeassert
  shortDescription: "Assert Kubernetes resources"
  version: "v0.2.0"
  description: |
    Provides a set of assertions that can be used to quickly
    assert Kubernetes resources from the command line against
    your working cluster.    
  platforms:
  - selector:
      matchExpressions:
      - key: "os"
        operator: "In"
        values:
        - darwin
        - linux
    uri: https://github.com/morningspace/kubeassert/archive/v0.2.0.tar.gz
    sha256: a35b62a111212a74c954f2991fdfa7b4cad8e92b9318773f87c9ff8c12a5ea52
    bin: kubectl-assert.sh
    files:
    - from: "/kubeassert-*/kubectl-assert.sh"
      to: "."
    - from: "/kubeassert-*/LICENSE"
      to: "."

As you can see, this file provides information about the plugin, like its homepage, description, and release URL. Krew uses this information to install the plugin.

The list of all available plugins can also be found online. You can also use kubectl krew search KEYWORD to search for a plugin from the command line:

$ kubectl krew search assert

NAME        	DESCRIPTION                                    	INSTALLED
assert      	Assert Kubernetes resources                    	no
ssm-secret  	Import/export secrets from/to AWS SSM param store  no
whisper-secret  Create secrets with improved privacy           	no

Note that Krew keeps a local copy of the krew-index, and the search command returns information from this local copy. To sync this local copy with the remote index, you can run kubectl krew update:

$ kubectl krew update
Updated the local copy of plugin index.

You can get more information about a plugin using the kubectl krew info command:

$ kubectl krew info assert

NAME: assert
INDEX: default
URI: https://github.com/morningspace/kubeassert/archive/v0.2.0.tar.gz
SHA256: a35b62a111212a74c954f2991fdfa7b4cad8e92b9318773f87c9ff8c12a5ea52
VERSION: v0.2.0
HOMEPAGE: https://github.com/morningspace/kubeassert
DESCRIPTION:  
Provides a set of assertions that can be used to quickly  
assert Kubernetes resources from the command line against  
your working cluster.

To list all available plugins, you need to run kubectl krew search without any arguments.

#Installing and Using Plugins

To install a plugin, you need to run kubectl krew install PLUGIN_NAME. For example, to install the assert plugin, you’d run:

$ kubectl krew install assert

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

Every installed plugin exposes a kubectl subcommand that corresponds to the name of the plugin. In this case, the assert plugin creates the kubectl assert command. Note that it’s kubectl assert and not kubectl krew assert. You don’t need to invoke the plugins through Krew, as it does the heavy lifting for you and makes the plugins available to kubectl directly.

Next, you should ensure that the assert plugin works correctly. From the GitHub page:

KubeAssert is designed as a kubectl plugin to provide a set of assertions that can be used to quickly assert Kubernetes resources from the command line against your working cluster.

Create a deployment:

kubectl create deployment hello-node --image=registry.k8s.io/e2e-test-images/agnhost:2.39 -- /agnhost netexec --http-port=8080

deployment.apps/hello-node created

Now use the assert plugin to verify that the deployment exists:

$ kubectl assert exist deployment hello-node

ASSERT deployment hello-node should exist.
INFO   Found 1 resource(s).
deployment.apps/hello-node
ASSERT PASS

#Upgrading Plugins

To upgrade all installed plugins to their latest versions, you need to run kubectl krew upgrade. Since Krew manages itself as a plugin, the above command will also upgrade Krew itself if it’s not already on the latest version.

You can also specify specific plugins to upgrade by providing their names:

kubectl krew upgrade assert ca-cert

#Listing All Installed Plugins

All installed plugins can be listed with kubectl krew list:

$ kubectl krew list

PLUGIN  VERSION
assert  v0.2.0
krew	v0.4.3

#Uninstalling Plugins

A plugin can be uninstalled with the kubectl krew uninstall command. This command expects the name of the plugin and removes it from your system:

$ kubectl krew uninstall assert
Uninstalled plugin: assert

#Using a Different Index

While Krew comes with the default krew-index, sometimes you might want to use a plugin that isn’t available there—for example, a private plugin developed by your company. It’s possible to add a custom plugin index to Krew with the following command:

kubectl krew index add INDEX_NAME INDEX_URL

In the above command, INDEX_NAME is a unique name that you need to give the index to identify it in your system. For example, you can install the AppsCode index with the following command:

kubectl krew index add appscode https://github.com/appscode/krew-index.git

You can check the list of all indexes using kubectl krew index list. You should see the following output:

INDEX     URL
appscode  https://github.com/appscode/krew-index.git
default   https://github.com/kubernetes-sigs/krew-index.git

After installing an index, run kubectl krew update to fetch a local copy of the newly installed index.

To install a plugin from a specific index, you need to prefix the plugin name with the index name. The following example installs the dba plugin from the AppsCode index:

kubectl krew install appscode/dba

If you don’t mention an index name, the default index will be used.

If two indexes contain two plugins with the same name—for example, foo/bar and baz/bar—you can only install one of them. So, at any point, each of your installed plugins will have a unique name. This means when you want to uninstall a plugin, specifying an index name is not necessary since the installed plugins have unique names.

You can remove a custom index using the kubectl krew index remove INDEX_NAME command. If you want, you can remove the default index as well and install another index with the default name. Again, if you omit the index name from the Krew commands, the default index will be used.

#Conclusion

Plugins can extend the functionalities of kubectl to perform complex tasks. With Krew, finding, installing, and managing plugins becomes simple and easy. In this tutorial, you learned how to search plugins, install and update plugins, and delete plugins, as well as how to use a custom index in Krew.

Sign up for our newsletter

Be the first to know about new features, announcements and industry insights.