K9s for Cluster Management



Managing Kubernetes clusters efficiently can be challenging, especially when working with complex deployments, logs, and troubleshooting tasks. This is where K9s, a robust terminal-based UI for Kubernetes, comes in handy. K9s provides a simple, interactive way to navigate, inspect, and manage Kubernetes resources without manually running kubectl commands.

In this guide, we will explore how to install, configure, and use K9s for effective Kubernetes cluster management. By the end, we'll be comfortable using K9s to monitor workloads, manage pods, and debug issues in our cluster.

Key Features of K9s

  • Real-time monitoring of cluster resources.
  • Interactive UI for managing Pods, Services, Deployments, and more.
  • Built-in shortcuts for quick navigation and operations.
  • Advanced filtering and searching capabilities.
  • Customizable views and layouts.

Installing K9s

On Linux (Ubuntu/Debian-based systems):

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

Alternatively, using apt:

$ sudo apt install k9s

On macOS (Using Homebrew):

$ brew install k9s

On Windows (Using Scoop or Chocolatey):

$ scoop install k9s

Or with Chocolatey:

$ choco install k9s

Verifying Installation

After installation, we can confirm that K9s is correctly installed by running:

k9s version

Output

____  __ ________       
|    |/  /   __   \______
|       /\____    /  ___/
|    \   \  /    /\___  \
|____|\__ \/____//____  /
\/           \/ 

Version:    v0.40.10
Commit:     35361bb23822761200fcc977653818faf054adad
Date:       2025-03-16T16:01:55Z

Using K9s for Cluster Management

Launching K9s

To start K9s, simply run:

$ k9s

Output

Context: kubernetes-admin@kubernetes
Cluster: kubernetes
User:    kubernetes-admin
K9s Rev: v0.40.10
K8s Rev: v1.31.6
CPU:     n/a
MEM:     n/a

┌────────────────────────────────── ✍️  v1/pods(default)[0] ──────────────────────────────────┐
│ NAME↑      PF       READY       STATUS             RESTARTS IP       NODE       AGE        │
└────────────────────────────────────────────────────────────────────────────────────────────┘

K9s will automatically connect to the currently configured Kubernetes context and display the running pods in the default namespace.

Navigating K9s

Once inside K9s, we can navigate using the keyboard. Here are some key interactions:

Command Description
? Show help menu
/ Search resources
q Quit K9s
:ns Change namespace
:ctx Change context
d Describe resource
l View logs of a pod
s Shell into a pod
x Delete a pod

Managing Workloads with K9s

Viewing Pods

By default, K9s starts by displaying pods in the current namespace. To view all pods across namespaces, press 0 (zero) or enter:

:pods -A

Output

NAMESPACE        NAME                                      PF    READY    STATUS↑        RESTARTS IP              NODE             AGE
kube-flannel     kube-flannel-ds-b77pc                     ●     1/1      Running               0 172.16.12.5     controlplane     28m
kube-flannel     kube-flannel-ds-ptthh                     ●     1/1      Running               0 172.16.12.6     node01           28m
kube-system      coredns-7c65d6cfc9-w54hj                  ●     1/1      Running               0 10.244.1.4      node01           28m
kube-system      coredns-7c65d6cfc9-zxvsv                  ●     1/1      Running               0 10.244.1.6      node01           28m
kube-system      etcd-controlplane                         ●     1/1      Running               0 172.16.12.5     controlplane     28m
kube-system      hostpath-provisioner-5558658586-fdczv     ●     1/1      Running               0 10.244.1.3      node01           28m
kube-system      kube-apiserver-controlplane               ●     1/1      Running               0 172.16.12.5     controlplane     28m
kube-system      kube-controller-manager-controlplane      ●     1/1      Running               0 172.16.12.5     controlplane     28m
kube-system      kube-proxy-dkkbf                          ●     1/1      Running               0 172.16.12.6     node01           28m
kube-system      kube-proxy-hnsq2                          ●     1/1      Running               0 172.16.12.5     controlplane     28m
kube-system      kube-scheduler-controlplane               ●     1/1      Running               0 172.16.12.5     controlplane     28m
kube-system      kubelet-csr-approver-5cd65b5d5f-49j7z     ●     1/1      Running               0 10.244.1.5      node01           28m
kube-system      kubelet-csr-approver-5cd65b5d5f-xff2s     ●     1/1      Running               0 10.244.1.2      node01           28m

Inspecting a Pod

To describe a pod, highlight it using the arrow keys and press "-d". This provides detailed information about the pod, including labels, node placement, and container statuses.

Output

─────────────────────────────────────── Describe(kube-system/hostpath-provisioner-5558658586-fdczv) ───────────────────────────────────────
Name:             hostpath-provisioner-5558658586-fdczv
Namespace:        kube-system
Priority:         0
Service Account:  hostpath-provisioner
Node:             node01/172.16.12.6
Start Time:       Sun, 30 Mar 2025 11:07:10 +0000
Labels:           app.kubernetes.io/instance=hostpath-provisioner
app.kubernetes.io/name=hostpath-provisioner
pod-template-hash=5558658586
Annotations:      <none>
Status:           Running
IP:               10.244.1.3
IPs:              
IP: 10.244.1.3
Controlled By:    ReplicaSet/hostpath-provisioner-5558658586

Viewing Logs

To view the logs of a pod, navigate to it and press "-l". We can use Shift + l to view logs from all containers in a multi-container pod.

Output

───────────────────────────── Logs(kube-system/kubelet-csr-approver-5cd65b5d5f-xff2s:kubelet-csr-approver)[tail] ─────────────────────────────
Autoscroll:On      FullScreen:Off     Timestamps:Off     Wrap:Off
{"level":"INFO","ts":"2025-03-30T11:07:28.321Z","caller":"cmd/cmd.go:60","msg":"Kubelet-CSR-Approver controller starting.","commit":"db4eca0
{"level":"INFO","ts":"2025-03-30T11:07:28.327Z","caller":"manager/server.go:83","msg":"starting server","name":"health probe","addr":"[::]:8
I0330 11:07:28.328086       1 leaderelection.go:257] attempting to acquire leader lease kube-system/kubelet-csr-approver...
{"level":"INFO","ts":"2025-03-30T11:07:28.327Z","logger":"controller-runtime.metrics","caller":"server/server.go:208","msg":"Starting metric
{"level":"INFO","ts":"2025-03-30T11:07:28.328Z","logger":"controller-runtime.metrics","caller":"server/server.go:247","msg":"Serving metrics

Executing a Shell in a Pod

For troubleshooting, we can simply open a shell inside a pod. Navigate to the pod and press "-s" to start an interactive shell session.

Output

<<K9s-Shell>> Pod: kube-system/etcd-controlplane | Container: etcd
sh-5.2#

Deleting a Pod

If a pod needs to be deleted (for example, to restart it), we can navigate to it and press Shift + x. K9s will automatically delete the selected pod.

Resource Management with K9s

Switching Namespaces

By default, K9s starts in the default namespace. To switch to another namespace, we can use:

:ns kube-system

Output

┌─────────────────────────────────────────────────── ✍️  v1/pods(kube-system)[11] ──────────────────────────────────────────────────────────
NAME↑                                       PF      READY      STATUS             RESTARTS IP                NODE                AGE
coredns-7c65d6cfc9-5lkms                    ●       1/1        Running                   0 10.244.1.3        node01              98m
coredns-7c65d6cfc9-pqrt6                    ●       1/1        Running                   0 10.244.1.2        node01              98m
etcd-controlplane                           ●       1/1        Running                   0 172.16.0.5        controlplane        98m
hostpath-provisioner-5558658586-2pmhg       ●       1/1        Running                   0 10.244.1.4        node01              98m
kube-apiserver-controlplane                 ●       1/1        Running                   0 172.16.0.5        controlplane        98m
kube-controller-manager-controlplane        ●       1/1        Running                   0 172.16.0.5        controlplane        98m

Alternatively, pressing 0 lists all available namespaces.

Changing Contexts

If we work with multiple Kubernetes clusters, we can switch contexts within K9s using:

:ctx

Output

┌──────────────────────────────────────────────────────────── ✍️  contexts(all)[1] ─────────────────────────────────────────────────────────────┐
│ NAME↑                                            CLUSTER                      AUTHINFO                           NAMESPACE                   │
│ kubernetes-admin@kubernetes(*)                   kubernetes                   kubernetes-admin                                               │
│                                                                                                                             

This will display a list of available contexts, allowing us to switch interactively.

Viewino Deployments, Services, and Nodes

  • Deployments: Press d or enter :deploy to list deployments.
  • Services: Press sv or enter :svc to list services.
  • Nodes: Press n or enter :nodes to see all nodes in the cluster.

Troubleshooting with K9s

Checking Cluster Health

To get an overview of cluster health, we can check events using:

:events

This will display real-time events across namespaces.

Resetting K9s Configurations

If K9s behaves unexpectedly, we can reset its configuration by deleting the config file:

$ rm -rf ~/.k9s

Conclusion

K9s is an indispensable tool for Kubernetes cluster management, offering an intuitive interface that simplifies pod monitoring, troubleshooting, and resource management. By leveraging its powerful features, we can reduce the complexity of interacting with Kubernetes, making our daily operations smoother and more efficient.

Advertisements