Effortless Management with the K9S Kubernetes CLI

Published:8 June 2022 - 7 min. read

Azure Cloud Labs: these FREE, on‑demand Azure Cloud Labs will get you into a real‑world environment and account, walking you through step‑by‑step how to best protect, secure, and recover Azure data.

K9s is a terminal-based UI that aims to simplify Kubernetes management. With the ability to edit and apply manifests coupled with a handy context menu, you hardly need to remember any explicit kubectl commands.

Want to know how K9S works? Read on as you interact with a Kubernetes cluster in style in this K9s Kubernetes CLI tutorial.

Prerequisites

This tutorial will be a hands-on demonstration. If you’d like to follow along, you’ll need the following requirements.

  • You’ll need a Linux machine to host the Kubernetes cluster. This tutorial uses Fedora 35.
  • Your Linux computer must have Minikube already installed for your local Kubernetes. This tutorial uses the latest version of this writing, v1.25.2.

Installing the K9s Kubernetes CLI

You’ll now install the K9s Kubernetes CLI with all the prerequisites in place. There are several ways to install K9s, including from source, downloading the binary, through some Linux distro’s package manager.

The most convenient and universal way is via webi, which you’ll be doing in this example. To do so, follow these steps.

1. Log in to your Linux server using your preferred SSH client.

2. Next, run the below command to install K9s by downloading the webi script and piping it to Bash.

curl -sS https://webinstall.dev/k9s | bash

As you can see below, the script installed k9s to the /home/<user>/.local/bin folder.

Installing the K9s Kubernetes CLI
Installing the K9s Kubernetes CLI

3. Run the below command to check your version of K9s and if the command works.

k9s version

The latest version as of this writing is v0.25.18.

Viewing the K9s Kubernetes Terminal version
Viewing the K9s Kubernetes Terminal version

4. After confirming that K9s is working, set the default text editor on your Linux computer by running the below command. Replace vi if you prefer to set a different default editor, such as nano or emacs.

This step is crucial for K9s to know which text editor to use when editing configuration files within the K9s terminal UI.

export EDITOR=vi

Starting a Cluster and Creating a Deployment

The K9s Kubernetes tool is ready for use, but it will not be useful if you do not have a cluster to manage. In this section, you will start a Kubernetes cluster and deploy a web service.

1. Execute the following minikube command to start a Minikube cluster as follows.

minikube start

The response in your terminal window should resemble the screenshot below

Starting a Minikube Cluster
Starting a Minikube Cluster

2. Next, run the following command to deploy an Apache web server to your Kubernetes cluster.

# (deployment) Deployment name    = httpd
# (--image) Base image            = httpd:latest
# (--replicas) Number of replicas = 2
kubectl create deployment httpd --image=httpd:latest --replicas=2

You will see an output similar to the screenshot below, confirming the command was successful.

Creating a Deployment

3. Lastly, run the below command to enable the metrics-server addon. This step ensures metrics collection for your cluster and deployments.

minikube addons enable metrics-server
Enabling the metrics-server addon
Enabling the metrics-server addon

Monitoring Your Cluster via Pulses

Now that you’ve created the test deployment and enabled metrics collection, you’ll learn how to monitor them in K9s.

K9S has a handy cluster-wide dashboard feature called Pulses, which shows you charts and statistics for deployments, events, pods, etc. Follow the steps in the following paragraphs to know how to monitor your cluster with Pulses.

1. Connect to the Kubernetes cluster in default mode by running the below command.

k9s

You should see a list of deployments in all namespaces. Your deployments list would be different than the ones you see below. Focus on the httpd deployment, which is the one you created earlier.

Launching the K9S Kubernetes CLI
Launching the K9S Kubernetes CLI

2. Type the:pulses command and press Enter to activate the Pulses view.

Activating the Pulses view
Activating the Pulses view

The Pulses view displays the pertinent statistics about your cluster and the resources running within it. For example, the screenshot below shows that the cluster has 5 Deployments, 5 Replicasets, and 12 Pods.

Exploring the K9 Kubernetes Pulses Dashboard
Exploring the K9 Kubernetes Pulses Dashboard

3. To select a metric, press the corresponding number for that object. For example, press 4 to select Pods and press Enter to switch to the Pods view, as shown below.

Switching to the Pods from Pulses
Switching to the Pods from Pulses

You should see the list of running pods in all namespaces. The list includes the pods’ status and resource statistics, such as CPU and Memory usage.

Listing Pods in All Namespaces
Listing Pods in All Namespaces

4. Press Escape to go back to the Pulses view.

Discovering and Managing Your Pods

Well done! You now have a list of all pods running in your cluster. In this section, you will isolate and perform management tasks on the pods in your httpd deployment.

1. Switch to the Pods view by entering the :pod command.

Switching to the Pods view
Switching to the Pods view

You should see the list of all the pods in all namespaces on your screen.

Pods list
Pods list

2. Press the corresponding number for the default menu to filter the list and show only the pods under the default namespace. After pressing the namespace number, you’ve effectively filtered the pods’ list, as shown below.

Filtering the pods by namespace
Filtering the pods by namespace

3. Now that you’ve filtered the pods by their namespace, you can manage each pod, knowing in which namespace they belong and eliminating any possible confusion.

For example, highlight the first pod in your list and press d to show the pod’s details on the screen. This action is equivalent to running the kubectl describe command.

Displaying a pod’s information inside the K9s Kubernetes CLI
Displaying a pod’s information inside the K9s Kubernetes CLI

4. To go back to the Pods list, press Esc.

Scaling a Deployment

When you need to increase or decrease the number of replicas in your deployment, you’d typically run the kubectl scale command or manually edit the configuration. The K9s CLI makes scaling your deployment more convenient with a menu-driven interface.

1. Type the below command in K9s and press Enter to switch to the Deployments view.

:dp

2. Highlight your httpd deployment and press s to bring up the Scale configuration box.

Initiating a scaling operation
Initiating a scaling operation

3. Change the number of Replicas in the deployment from 2 to 4. Next, move the cursor to OK using the arrow keys and press Enter to apply the change.

Setting the number of replicas
Setting the number of replicas

Your httpd deployment should now have 4 pods after applying the scaling change.

Viewing the replica count
Viewing the replica count

Exposing a Deployment for External Access

One of the most consistent ways to expose a Kubernetes deployment is to apply a service manifest file. The following steps will show you how to apply a manifest file inside the K9s Kubernetes CLI.

1. Open another terminal window and create a YAML file named /tmp/httpd-np.yml using your preferred editor. This example uses vi as the editor. Once the vi editor is open, press i to enter the INSERT mode.

vi /tmp/httpd-np.yml

2. Copy and paste the following YAML directives into the file. This directive exposes the service to the 31337 external port.

apiVersion: v1
kind: Service
metadata:
  # Set a name below to be used to refer to the service that will be created
  name: httpd
spec:
  type: NodePort
  selector:
    # Supply the name of the deployment below
    app: httpd
  ports:
      # By default and for convenience, the `targetPort` is set to the same value as the `port` field.
    - port: 80
      targetPort: 80
      # Optional field
      # If omitted the Kubernetes control plane will allocate a port from a range (default: 30000-32767)
      nodePort: 31337

Your file should resemble the screenshot below at this point—press Escape exiting the INSERT mode before you do the next step.

Creating a service manifest file
Creating a service manifest file

3. Type the :wq command and press Enter to save and exit the editor.

4. Switch back to your K9S terminal and type :dir /tmp/. Doing so switches K9s to the directory view. As you can see, the file httpd-np.yml you created is on the list.

Listing a directory in K9s
Listing a directory in K9s

5. Move the cursor to the httpd-np.yml file, and press a to apply the manifest.

Applying a manifest in K9s Kubernetes
Applying a manifest in K9s Kubernetes

6. Finally, test the service by opening the web address http://<node_ip>:external_port in your browser. In this example, the web address is http://192.168.49.2:31337.

Accessing the web service via the web browser
Accessing the web service via the web browser

As an alternative, run the below command in another terminal window.

curl http://192.168.49.2:31337

You should get a similar result to the image below.

Testing the web service with cURL
Testing the web service with cURL

Digging Deeper with XRay

Exploring your cluster one resource class at a time is good. But, K9S’ Xray feature allows you to explore all the resources that constitute another resource in a single tree view.

Enter the :xray command to view all services (svc) and their constituent resources.

:xray svc 

At a glance, you will notice two services in the default namespace; httpd and kubernetes. You also see that four httpd-based containers are connected to the httpd service.

Displaying Services tree with Xray
Displaying Services tree with Xray

Type a ? to get help on Xray. You at least get to understand what all the icons mean for the uninitiated. You’ll also see the GENERAL and NAVIGATION key bindings.

Viewing help
Viewing help

Viewing Cluster Resources Logs and Events

Logs and events are a potent way of understanding the state of a running cluster either for capacity planning or troubleshooting. Beginning at the Xray view, you will, in this section, explore K9S’ ability to view logs, events, and cluster metrics.

1. Move the cursor to the first pod under the httpd tree node.

Selecting a Pod
Selecting a Pod

2. Hit l to view the container’s logs, as shown below. You will see log entries that arrived within the last minute [1m].

Viewing the pod’s logs in default mode
Viewing the pod’s logs in default mode

3. Press 0 to switch to tail mode to show all the recent log entries.

Switching to the tail log mode
Switching to the tail log mode

Describing the Cluster’s Current State

You can also find further information on the condition of your cluster by describing it in the following steps.

1. Type :node and press Enter to switch to the Nodes view.

2. Press d to describe the currently highlighted cluster on the list.

Describing a Cluster
Describing a Cluster

You should see a list of cluster-specific parameters and their states.

Viewing the cluster details
Viewing the cluster details

3. Scroll down to the Conditions section to view the state of critical metrics such as Disk and Memory Pressure. In the below screenshot, you will notice the cluster has no alarming conditions.

Viewing the state of crucial resource metrics
Viewing the state of crucial resource metrics

4. Scroll to Events to have a summary log of events on your cluster. This example shows that there’s one Warning about the port binding. You can see how this feature can be an excellent entry point when troubleshooting cluster resource issues.

Viewing events
Viewing events

Conclusion

By making it to the end of this tutorial, you have learned to install, configure, and operate the K9S Kubernetes CLI to manage your Kubernetes clusters with as few keystrokes as possible. Like any new tool, there’s always a learning curve, which is true with K9s.

But in the long run, K9s’ menu-driven graphical interface surely beats running commands manually in the terminal. Now, perhaps you can deploy Postgres to a Kubernetes cluster and flex your K9S muscles to manage that deployment.

Hate ads? Want to support the writer? Get many of our tutorials packaged as an ATA Guidebook.

Explore ATA Guidebooks

Looks like you're offline!