Introduction

A while ago, we published an overview post on useful Kubernetes command-line tools to get to know, where we mentioned the tool called K9s.

This guide is a more in-depth look at some of the features of K9s that help you increase your productivity with managing your Kubernetes clusters.

While you can achieve all the functions that K9s gives you through kubectl commands, some users will find the more visual layout presented by K9s much more approachable. Handily, you also don't have to remember the order of arguments, and which arguments a particular kubectl command requires!

You can follow along with this guide by starting up a Kubernetes cluster on Civo. If you have not yet signed up to Civo, you can get that out of the way first. Then, have a look at Anaïs's guide to setting up your first cluster. Once you have your cluster's kubeconfig file saved, return to this guide.

Note that this is all advice for day to day usage and administration of Kubernetes clusters. If you are preparing for the Certified Kubernetes Administrator (CKA) exam, for example, you will need to know kubectl inside out.

K9s installation

K9s is open-source software and available for various operating systems with instructions on this official repository. If you use a package manager, K9s will be available through it.

On Mac OS, you can install K9s with brew:

brew install k9s

On Windows, either via scoop or chocolatey:

scoop install k9s
choco install k9s

On Linux, you may find your package manager contains K9s. If not, you can always get the latest binaries from the K9s repository releases page.

K9s usage

K9s runs in your terminal, and if you have installed it according to the instructions for your operating system, you should be able to start it with the k9s command. It will by default read your current KUBECONFIG, which in turn is by default read from $HOME/.kube/config. If your Civo cluster configuration is saved there, you should see it be loaded when you start K9s.

If you ever get lost in K9s, you can use ? on any screen to get a help screen. And if you want to close k9s, the easiest way to do that is to use control-c to kill the process and return back to your shell.

Viewing resources

If you are familiar with vim commands, you will be right at home with K9s. The way to change what you are seeing is by prefixing the type of resource with a colon. So, in order to view pods in your cluster once you are running K9s, you would type :pods and press enter. To switch to viewing nodes, type in :node and press enter. You can see suggestions populated in the command box as you type, so you can take advantage of tab-completion and save on keystrokes. So, to view all the namespaces in your cluster you might only have to type in:

:na <tab> <enter>

Searching

If you have a lot of resources of a particular type in a cluster, you can quickly search through them using the / command. If, for example, you wanted to filter all namespaces you retrieved in the previous section down to just the ones that contain kube in their name, you would type:

/kube <enter>

You might get something like this:

Searching with the slash command

Obviously, this is an example with only a few potential items to sift through, but if you had hundreds of namespaces or custom resources, it would be a lifesaver. This saves you from having to chain grep commands into kubectl output!

Once again, if you are familiar with vim you will be able to move about the resources using j for down and k for up. Handily for those of us who never got the hang of vim navigation, the arrow keys work just as well!

You can select the resource you want and press d to describe it. In the image below, I am viewing the description of a node in my cluster, which I found with the following commands:

:node
kkd

K9s describe node view

That's quite a lot of keystrokes saved compared to kubectl describe node <nodename> and that's assuming you have the node name handy! You may have to first get it with kubectl get nodes -o=name.

Logs of Kubernetes resources with K9s

You can view logs of any pod, job or other Kubernetes resource using the l command when you have that resource selected. However, what about viewing past events, if a resource has been replaced by a new instance of itself that is now writing its own logs? K9s allows you to view previous logs for that resource with the p command which is incredibly handy for tracing errors or other issues.

If you are viewing logs and want to see exactly when an event occurred, you can use t to automatically insert timestamps:

Timestamps on logs

Operations on Kubernetes resources with K9s

With K9s you can also edit configurations on the fly. If you press e on any given resource you will be presented with your default editor (likely vim if you have not specified another) to edit the configuration yaml of that particular pod, deployment, or other resource.

Obviously, if you end up editing these configurations, they will affect your cluster immediately but may be overwritten by subsequent CI/CD deployments if you use them.

You can perform node-level operations on K9s as well. For example, you could cordon a node, or even drain it so that you can remove it from your pool or perform other maintenance on it.

If you have a pod that is misbehaving, you can delete it using K9s too. Navigate to it in the :pods view and press control-d. You will be prompted to confirm the deletion of the pod:

Pod deletion confirmation in K9s

And as soon as you do, the Kubernetes cluster API will schedule another pod to take its place:

Pod deletion triggers the initiation of a new pod

You can also configure port-forwarding from a specific pod by using shift-f and picking a local port, like 3000. You can then navigate to localhost:3000 and view the resource.

Alternatively, you can even open up a shell into a running pod if it supports it! Select the pod you want and press s. This is the equivalent of running kubectl exec --stdin --tty <podname> -- /bin/bash. Again, a much quicker way of doing things!

In my case, I picked the main Gitea pod that had been created when I selected it from the Kubernetes marketplace. Voila:

<<K9s-Shell>> Pod: gitea/gitea-0 | Container: gitea 
bash-5.0# ls
app    bin    data   dev    etc    home   lib    media  mnt    opt    proc   root   run    sbin   srv    sys    tmp    usr    var
bash-5.0# 

To exit from the shell, you can run exit which takes you back to the K9s screen.

K9s monitoring tools

K9s has an interesting set of monitoring tools aside from a periodic polling of the selected resource (pod, node, etc) that you get on the main screen. One of the most interesting visualisations is the :pulses mode. Give it a try:

:pulses

You should see live-updating, colour-coded graphs of the main aspects of your cluster:

K9s Pulses view

You can navigate to drill down to each pulse shown by using Tab or selecting the number corresponding to the particular resource in the K9s top bar. In this image, I've scrolled down to Events to look at the 13 warnings, pressed enter and scrolled down the list:

K9s events log view

If you were to see event warnings such as this, you could then scroll to the specific one you wanted to view, and view information about it by pressing enter or d to describe it.

In this case, they are expected warnings, as the eventually consistent nature of Kubernetes means the Kubernetes API has to re-try certain events until they succeed, as they may depend on other components running. This was a cluster that was in the process of starting, so the events were all happening at once.

You can back out of any view by pressing Esc until you are at the screen you wish to see again.

(More) Advanced K9s usage

Aliases

Just like with your terminal shell, you can create aliases in K9s to speed up common commands you run. I am including this in the advanced usage as the above instructions all use non-custom commands.

To create aliases for K9s, you need to create a file in the .k9s/ directory. By default this is under your home directory, so you can navigate to it by running cd $HOME/.k9s.

When you are in the correct directory, you need to create a file called alias.yml and edit it with your favourite text editor. The format of alias definitions is in the form of alias:group/version/resource. So, if you wanted to make an alias dep to view all deployments, you would need to define it in alias.yml as:

alias:
  dep: apps/v1/deployments

Note that the file has to start with alias: at the top, with your alias definitions nested underneath, each on their line.

Now, when you start K9s, you will be able to type in :dep <enter> from the main screen and see your deployments. Neat!

As ever, refer to the official documentation for more detail on the structure and usage of the alias file.

Popeye scanning

K9s bundles in the Popeye Kubernetes sanitizing tool. You can use it to get a handle on how your cluster's configuration compares to best practice. You can open this view by using the :popeye command. This will open up a scan summary page giving you an overall score and the components Popeye has analyzed:

Popeye scan summary of a Civo Kubernetes cluster

You can drill down to any component, such as daemonsets, to see what the reasons for the results are:

Detailed Popeye results

If you want to mitigate any of these configuration issues detected by the Popeye scan, you can look up the error message. For example, in the above screenshot, the "(POP-101) Image tagged "latest" in use" message has to do with the fact that the latest image tag is not immutable, and may be something you want to avoid.

Wrapping up

For getting information at-a-glance, quickly and at your fingertips in your terminal, K9s is an invaluable tool. The fact that you can view custom resource definitions alongside any other Kubernetes cluster objects means that K9s adapts to your cluster and its configuration.

You can rapidly perform operations on your cluster without convoluted kubectl commands that chain multiple terminal commands to get the output you want. In fact, every kubectl command you can target your cluster with has a corresponding K9s method, which you can make aliases for at your leisure. Additionally, K9s brings its own observability and analysis features like the Pulse and Popeye views, which help you keep an eye on your cluster overall.

We find K9s absolutely worth getting familiar with, and look forward to new features that will make using it even better. If you have used K9s and think we haven't covered a feature that should have been, give me a shout on Twitter and I will update this guide and give you credit for it!

Alternatively, if you have a favourite Kubernetes management tool that runs in the console, let me know about it! I love CLI tools and am always keen to try out new ones. You can find me on Twitter as above, or on the Civo community Slack.