Speed up administration of Kubernetes clusters with k9s

Check out this cool terminal UI for Kubernetes administration.
100 readers like this.
Top Linux Games of 2016

NASA on the Commons and Internet Archive Book Images. Modified by Opensource.com. CC BY-SA 4.0

Usually, my articles about Kubernetes administration are full of kubectl commands for administration for your clusters. Recently, however, someone pointed me to the k9s project for a fast way to review and resolve day-to-day issues in Kubernetes. It's been a huge improvement to my workflow and I'll show you how to get started in this tutorial. 

Installation can be done on a Mac, in Windows, and Linux. Instructions for each operating system can be found here. Be sure to complete installation to be able to follow along.

I will be using Linux and Minikube, which is a lightweight way to run Kubernetes on a personal computer. Install it following this tutorial or by using the documentation.

Setting the k9s configuration file

Once you've installed the k9s app, it's always good to start with the help command.

$ k9s help
K9s is a CLI to view and manage your Kubernetes clusters.

  k9s [flags]
  k9s [command]

Available Commands:
  help        Help about any command
  info        Print configuration info
  version     Print version/build info

  -A, --all-namespaces                 Launch K9s in all namespaces
      --as string                      Username to impersonate for the operation
      --as-group stringArray           Group to impersonate for the operation
      --certificate-authority string   Path to a cert file for the certificate authority
      --client-certificate string      Path to a client certificate file for TLS
      --client-key string              Path to a client key file for TLS
      --cluster string                 The name of the kubeconfig cluster to use
  -c, --command string                 Specify the default command to view when the application launches
      --context string                 The name of the kubeconfig context to use
      --demo                           Enable demo mode to show keyboard commands
      --headless                       Turn K9s header off
  -h, --help                           help for k9s
      --insecure-skip-tls-verify       If true, the server's caCertFile will not be checked for validity
      --kubeconfig string              Path to the kubeconfig file to use for CLI requests
  -l, --logLevel string                Specify a log level (info, warn, debug, error, fatal, panic, trace) (default "info")
  -n, --namespace string               If present, the namespace scope for this CLI request
      --readonly                       Disable all commands that modify the cluster
  -r, --refresh int                    Specify the default refresh rate as an integer (sec) (default 2)
      --request-timeout string         The length of time to wait before giving up on a single server request
      --token string                   Bearer token for authentication to the API server
      --user string                    The name of the kubeconfig user to use

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

As you can see, there is a lot of functionality we can configure with k9s. The only step we need to take place to get off the ground is to write a configuration file. The info command will point us to where the application is looking for it.

$ k9s info
 ____  __.________
|    |/ _/   __   \______
|      < \____    /  ___/
|    |  \   /    /\___ \
|____|__ \ /____//____  >
        \/            \/

Configuration:   /Users/jess/.k9s/config.yml
Logs:            /var/folders/5l/c1y1gcw97szdywgf9rk1100m0000gn/T/k9s-jess.log
Screen Dumps:    /var/folders/5l/c1y1gcw97szdywgf9rk1100m0000gn/T/k9s-screens-jess

To add a file, make the directory if it doesn't already exist and then add one.

$ mkdir -p ~/.k9s/
$ touch ~/.k9s/config.yml

For this introduction, we will use the default config.yml recommendations from the k9s repository. The maintainers note that this format is subject to change, so we can check here for the latest version.

  refreshRate: 2
  headless: false
  readOnly: false
  noIcons: false
    tail: 200
    buffer: 500
    sinceSeconds: 300
    fullScreenLogs: false
    textWrap: false
    showTime: false
  currentContext: minikube
  currentCluster: minikube
        active: ""
        - all
        - kube-system
        - default
        active: dp
      critical: 90
      warn: 70
      critical: 90
      warn: 70

We set k9s to look for a local minikube configuration, so I'm going to confirm minikube is online and ready to go. 

$ minikube status
host: Running
kubelet: Running
apiserver: Running
kubeconfig: Configured

Running k9s to explore a Kubernetes cluster

With a configuration file set and pointing at our local cluster, we can now run the k9s command.

$ k9s

Once you start it up, the k9s text-based user interface (UI) will pop up. With no flag for a namespace, it will show you the pods in the default namespace.

K9s screenshot

If you run in an environment with a lot of pods, the default view can be overwhelming. Alternatively, we can focus on a given namespace. Exit the application and run k9s -n <namespace> where <namespace> is an existing namespace. In the picture below, I ran k9s -n minecraft, and it shows my broken pod

K9s screenshot

So once you have k9s up and running, there are a bunch of things you can do quickly. 

Navigating k9s happens through shortcut keys. We can always use arrow keys and the enter key to choose items listed. There are quite a few other universal keystrokes to navigate to different views:

  • 0—Show all pods in all namespaces

K9s screenshot
  • d—Describe the selected pod

K9s screenshot
  • l—Show logs for the selected pod pod

Using k9s to show Kubernetes pod logs

You may notice that k9s is set to use Vim command keys, including moving up and down using J and K keys. Good luck exiting, emacs users :)

Viewing different Kubernetes resources quickly

Need to get to something that's not a pod? Yea I do too. There are a number of shortcuts that are available when we enter a colon (":") key. From there, you can use the following commands to navigate around there.

  • :svc—Jump to a services view.

K9s screenshot
  • :deploy—Jump to a deployment view.

K9s screenshot

K9s screenshot
  • :namespace—Jump back to the namespaces view.

K9s screenshot
  • :cj—Jump to the cronjobs view to see what jobs are scheduled in the cluster.

K9s screenshot

The most used tool for this application will be the keyboard; to go up or down on any page, use the arrow keys. If you need to quit, remember to use Vim keybindings. Type :q and hit enter to leave.

Example of troubleshooting Kubernetes with k9s

How does k9s help when something goes wrong? To walk through an example, I let several pods die due to misconfiguration. Below you can see my terrible hello deployment that's crashing. Once we highlight it, we press d to run a describe command to see what is causing the failure.

K9s screenshot

K9s screenshot

Skimming the events does not tell us a reason for the failure. Next, I hit the esc key and go check the logs by highlighting the pod and entering <shift-l>.

K9s screenshot

Unfortunately, the logs don't offer anything helpful either (probably because the deployment was never correctly configured), and the pod will not come up.

I then esc to step back out, and I will see if deleting the pod will take care of this issue. To do so, I highlight the pod and use <ctrl-d>. Thankfully, k9s prompts users before deletion. 

K9s screenshot

While I did delete the pod, the deployment resource still exists, so a new pod will come back up. It will also continue to restart and crash for whatever reason (we don't know yet).

Here is the point where I would repeat reviewing logs, describing resources, and use the e shortcut to even edit a running pod to troubleshoot the behavior. In this particular case, the failing pod is not configured to run in this environment. So let's delete the deployment to stop crash-then-reboot loop we are in.

We can get to deployments by typing :deploy and clicking enter. From there we highlight and press <ctrl-d> to delete.

K9s screenshot

K9s screenshot

And poof the deployment is gone! It only took a few keystrokes to clean up this failed deployment.

k9s is incredibly customizable

So this application has a ton of customization options, down to the color scheme of the UI. Here are a few editable options you may be interested in:

The entire application is configured in YAML files, so customization will feel familiar to any Kubernetes administrator.

Simplify your life with k9s

I'm prone to administrating over my team's systems in a very manual way, more for brain training than anything else. When I first heard about k9s, I thought, "This is just lazy Kubernetes," so I dismissed it and went back to doing my manual intervention everywhere. I actually started using it daily while working through my backlog, and I was blown away at how much faster it was to use than kubectl alone. Now I'm a convert. 

It's important to know your tools and master the "hard way" of doing something. It is also important to remember, as far as administration goes, it's important to work smarter, not harder. Using k9s is the way I live up to that objective. I guess we can call it lazy Kubernetes administration, and that's okay.

What to read next
User profile image.
Tech nomad, working in about anything I can find. Evangelist of silo prevention in the IT space, the importance of information sharing with all teams. Believer in educating all and open source development. Lover of all things tech. All about K8s, chaos and anything new and shiny I can find! Mastodon ID

1 Comment

Thank you ! This is a cool tool. I started using it last week and it already improved my workflow.

Creative Commons LicenseThis work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License.