yavarin.tech Everyone is saying how amazing Kubernetes is these days and yes I can not agree more. But I want to shed light for a moment on the fantastic kubectl. The great command line tool that gives you not only the ability to talk to your K8s setup but also lots of flexibility to do complicated stuff in a single line of command. You don’t have to be a K8s guru to know about the kubectl command but for those of you who are new to this world let’s first get to know more about this tool.

As official documents say, “The Kubernetes command-line tool, kubectl, allows you to run commands against Kubernetes clusters. You can use kubectl to deploy applications, inspect and manage cluster resources, and view logs.” You can see how to install it here and more information and details are here

So in short you can not do anything with your cluster if you do not have kubectl installed it is the actual wheel for the Kubernetes.

He looks cool because he knows how to make it fun!

kubectl add-ons

But there are ways to make using kubectl even more easier and fun! Add-ons and plugins are there to help you get more work done easier and in even shorter commands.

For example, there is an add-on called ns you can use this add-on to check what namespaces you have available in your cluster, and switch between them in a snap. To give you a better idea of how easier it is to switch namespaces using this plugin compared to using only kubectl command let’s have a look below.

First, you need to check what namespaces are available

kubectl get namespaces
NAME              STATUS   AGE
namespace-1       Active   164d
namespace-2       Active   99d
namespace-3       Active   156d
namespace-4       Active   141d
default           Active   164d
kube-node-lease   Active   164d
kube-public       Active   164d
kube-system       Active   164d
nginx-ingress     Active   136d

Then you should modify the current context configuration to switch to namespace-2

kubectl config set-context –current –namespace=namespace-2

It is a bit time-consuming and takes some practice to really memorize the commands right?

But how is it using the use of ns plugin? Well, you just type the command kns then you can choose the namespace you want with arrow keys and hit enter. en voilà! you just switched the working namespace.

Isn’t that amazing? Okay, now that I have your attention let’s see how you can install, manage and use these plugins. I will also share some of the plugins that I am using regularly.

Actually, installation of these plugins is even easier than using them. You can find the repository of the namespace switcher plugin here. You see it’s not that complicated just run the command based on your OS.

Managing add-ons with “krew”

Eventually, you will get more and more of these plugins installed and it is not an easy task to manage them all believe me! Also, who has the time to search for them? There must be a better way to manage and find these out there. Yes, my friends the is a better way! It is krew

He looks cool because he knows how to make it fun!

krew is a plugin manager for kubectl that makes it easy to discover and install plugins from a centralized repository. With krew, you can easily find and install plugins that have been vetted by the community and are known to work well with kubectl. Follow the steps here to install it on your machine. I really recommend using krew over installing the plugins from any repository you find. Because the maintainers of krew do security checks and also the community vote for their compatibility. So unless you need the plugin very badly and you can not find it with krew stick with krew for worry-free use of plugins.

After installing the krew you can use kubectl krew search to get a long list of available plugins. You sure will find what you need there. If you need more info on a plugin in the list use kubectl krew info <plugin-name>

kubectl krew info ns
NAME: ns
INDEX: default
URI: https://github.com/ahmetb/kubectx/archive/v0.9.5.tar.gz
SHA256: c94392fba8dfc5c8075161246749ef71c18f45da82759084664eb96027970004
VERSION: v0.9.5
HOMEPAGE: https://github.com/ahmetb/kubectx
DESCRIPTION:
Also known as "kubens", a utility to set your current namespace and switch
between them.
CAVEATS:
\
| If fzf is installed on your machine, you can interactively choose
| between the entries using the arrow keys, or by fuzzy searching
| as you type.
/

From more available commands use kubectl krew help

Usefull kubectl add-ons

And finally, some useful kubectl plugins that can make your life easier include:

  • kubectx: switch between Kubernetes contexts and namespaces.
  • kubens: switch between Kubernetes namespaces.
  • krew: install and manage kubectl plugins.
  • access-matrix: display a matrix of RBAC permissions for a given Kubernetes resource.
  • ketall: describe a list of Kubernetes resources in a tabular format.
  • kubeseal: encrypt Kubernetes secrets using asymmetric encryption.
  • kail: tail logs from multiple pods at the same time.
  • kubesec: ensure Kubernetes manifests and Docker images are secure.
  • kubefwd: forward Kubernetes services to your local machine for testing.
  • k9s: a terminal UI for Kubernetes that lets you view and manage resources in real-time.