Getting familiar with Krew plugins. Part I.
Have you ever wondered how much less problematic accessing your Kubernetes API server would be if you had some easy-to-use tools designed to simplify your work that would set you free from using plain kubectl commands? Have you ever thought that your annoyance at jotting down and forgetting long kubectl commands could already have been wiped away, thanks to software written by someone having the same problems as you? Have you ever wanted to know where to find a place full of programs that can save you time, money and frustration, while working with your Kubernetes clusters? If you answered ‘yes’ to any of these questions, this article will definitely be of interest to you. If you are still unsure whether it is worth embracing Kubernetes within your organization, I encourage you to dive deeper and read more about its current features and bright future. You can find its documentation here.
Should we be solely reliant on kubectl?
Kubectl is an indispensible command-line tool for Kubernetes. It allows you to access the Kubernetes API server also known as kube-apiserver, which lies at the heart of your Kubernetes cluster as it exposes its API and interacts with other cluster components. Therefore, kubectl can be used for most of the day-to-day tasks that you may come across while managing your clusters. In some cases, however, using it is not quite as easy as shooting fish in a barrel. For example, sometimes you may need to check if you are authorized to create or delete API-resources as a specific user, group, or service account on a given cluster, and you want the output to be clear at the same time. Kubectl gives you limited possibilities for this as you need to write multiple, long commands to check and change the kubeconfig, then query things one by one, and you still cannot obtain the output in a straightforward format. As a consequence, although kubectl can do much, it sometimes even lacks features that you may regard as essential in your everyday work. But fear not, since Krew will come to the rescue!
What is Krew?
Krew is a tool that supplies the easiest way to discover, install, update and use extensions to kubectl that we call plugins. Such plugins ought to strive for providing additional functionality that is not a part of the Kubernetes command-line distribution. There are a lot of really useful kubectl plugins you can install and use in a consistent way to enhance your Kubernetes experience.
Krew is a Kubernetes Special Interest Group (SIG) project that belongs to the same group (CLI) as kubectl or kustomize, uses krew-index to distribute standardized kubectl plugins, and serves as the Kubernetes package manager.
Krew keeps the information about all available plugins in indexes. It comes with its own centralized plugin index called default, which contains plugins pinned to the krew-index repository. Krew is not limited to only one index and allows custom plugin indexes to be added. They are git repositories that contain manifests of all included plugins (YAML configuration files that determine URIs for download, version number, and description of each plugin). Thus, it is possible to write and host your own plugins in the form of public or private repositories that can be managed by Krew. Even though hosting your own plugin index is not recommended, developing your own plugins and maintaining them in other indexes may turn out to be a big deal in the event that they are not accepted into krew-index or you need to have complete control over their development in your organization.
Krew is a cross-platform package manager that works just like apt, brew or snap, with the exception that it can be installed on bash, zsh, and fish shells, as well as Windows. Here you can find instructions on how to install it on your operating system. The installation of Krew should not rely on any OS package managers as this introduces maintenance overheads and leads to various errors that need to be taken care of. It is also advisable to use Krew for the installation of each kubectl plugin, rather than downloading them manually or via other package managers. It’s also worth mentioning that Krew makes it possible to write a plugin in any programming or scripting language that allows you to write command-line commands.
All Krew plugins are nothing more than executables whose names begin with kubectl- and should be put in the user’s PATH. However, Krew offers really easy plugin usage and maintenance, so there’s no need to deal with binaries at all. This is because plugins are standalones that Krew manages to make compatible with the kubectl tool. It adds their features as subcommands while maintaining a cohesive interaction with Kubernetes. You can list all installed plugins, including those not installed via Krew, with the command kubectl plugin list.
Krew as a Krew plugin
Yes, that means exactly what it says — Krew is itself a Krew plugin! It bootstraps itself and is compiled as a single binary in order to reduce the disk space, total maintenance and setup procedure. Since it’s self-managed, it’s also designed to upgrade itself to the latest version. Subcommands for managing plugins (e.g. install, upgrade) work with custom indexes as well. Things could hardly be any better than this, could they?
In the first instance, after downloading the plugin manager, you should update the local copy of the default plugin index. You can use the kubectl krew index command to interact with your plugin indexes.
As you can see, the default index (based on the official krew-index) is the only one you can access when you install the pure version of Krew. You need to have an URL of the repository to add it as the custom plugin index.
If you want to synchronize your local copies of plugin manifests with plugin repositories, you will need to run kubectl krew update.
In addition to this, thanks to Krew, you are able to discover all available kubectl plugins hosted in repositories that serve as plugin indexes. To see all plugins included in them, you should use the kubectl krew search command.
Remember that when the above command is used, all plugins from all the added indexes are listed. Here, I use the pipe operator and a grep command to filter plugins based on a specific string. This rather nifty technique helps you to avoid getting lost amid a multitude of plugins. Although it’s also possible to use a keyword after the search subcommand, I (personally) would not recommend it, as it often returns fewer — or less appropriate — rows than the grep approach.
Provided you want to use a plugin, you need to install it first. This can be done in a couple of ways. The most obvious method is to use kubectl krew install, which downloads the plugin, verifies its integrity, and installs it. You can also feed the command with a newline-delimited requirements type of file.
Notice the fact that updating your indexes isn’t really needed since the install command runs the update silently every time you install a new plugin. The same procedure is performed when you upgrade your Krew environment with kubectl krew upgrade. It tries to bump up the versions of all (by default) or selected (if given) plugins according to your local index. It is also used to update Krew as it is considered a separate plugin.
Installing a specific version of a plugin is currently unsupported and Krew will likely not support such a feature any time soon, owing to its simplicity and design. It’s possible to provide the install command with an outdated manifest file or keep older versions of manifests in separate, non-updateable indexes, however. You can refer to plugins from custom indexes in the format <index name>/<plugin name>. When a plugin does not have an explicit index name, it refers to a plugin from the default index, as default/ is always prepended.
Pay close attention to the fact that all previously installed plugins are skipped and that the installation does not stop even if one of the plugins is unable to be installed. If the installation is successful, your plugins are available as kubectl subcommands.
If you added the plugin folder to your PATH environment variable (usually it is $HOME/.krew/bin), you could also interact with it using the binary called kubectl-<plugin name>. In this example, its name would be kubectl-sql.
Furthermore, if you want to see more information about the plugin, such as its description (obtained from the manifest) or version, you can use kubectl krew info.
You should always check this to make sure the plugin is not going to do something undesired. If you’re interested in a summary of all installed Krew plugins, along with their respective versions, kubectl krew list is the way to go. There’s also a command kubectl krew uninstall, which provides a quick way of getting rid of unneeded plugins from your system.
To Krew or not to Krew?
Thanks to continuous development on the part of the creative community, more and more tools, scripts, and plugins are constantly moving to Krew. As of the time of writing this article, there are 143 plugins available in the default index. Krew offers some statistics to track download analytics for the package manager and its plugins over time, which can be found here. As you may see, the popularity of Krew is clearly rising with every new version that comes out.
There are a bunch of other useful Kubernetes-related tools you can install freely without Krew, such as K9s, Kuberbox, Kubetail, or Kubeless, to name just a few. On some occasions, these are more developed than the plugins, but in many cases, they do not provide anything that you would be unable to accomplish via Krew.
There are also many independent Kubernetes add-ons or applications, backed by huge companies and available in paid versions only. However, since this article focuses mostly on Krew, which offers freeware solutions, we won’t go into them here.
Plugins tend to offer either new features or shortcuts to kubectl. If you want something that improves the user experience of its original commands, there is a kubectl-aliases script that generates and sets many kubectl aliases. Personally, I’m not the kind of guy who likes remembering long, peculiar strings like the one below, but if you are an alias-lover, you should give it a whirl.
alias ksysgdepwslowidel=’kubectl — namespace kube-system get deployment — watch — show-labels -o wide -l’
Also, if you’ve made up your mind and want to utilize Krew, make sure that you have fzf installed and configured, as it’s an awesome interactive command-line search tool that saves you time typing. Some kubectl plugins from krew-index are fzf-compatible, so it’s worth installing fzf just to fully exploit their power. Such integration proves that Krew promotes up-to-date add-ons from the mighty Kubernetes community.
In this article, I’ve given a brief introduction to Krew. You should now understand what it really is, what makes it so handy, and how it can simplify everyday life. I’d also like to invite you to check out the second part of my upcoming article, where I will run through my list of the most useful Krew plugins you ought to know. I promise you won’t regret it, as they prove to be great time-savers for anyone.