Skip to content
Kubernetes tools · Chapter 7 of 20 Day 5: k9s

Day 5: k9s

Goal: Browse the cluster in the terminal instead of typing commands: views and command mode, filters, XRay, logs, port forwarding, custom plugins, hotkeys and aliases, read-only mode and export · Duration: approx. 75 min · Prerequisite: Day 4

Briefing

  • k9s is a terminal UI for Kubernetes. It shows resources as lists that update continuously and maps the most common kubectl actions to single keys: l for logs, d for describe, e for edit, s for a shell.
  • For everyday work, k9s is often faster than kubectl: find a Pod, read its logs, jump to the associated Service, forward a port, all without typing a single name.
  • k9s works with your kubeconfig and your permissions. One keystroke really deletes. That is why the read-only mode (Drill 6) belongs with every production access.
Key or command Effect
:pods, :deploy, :svc, :dp (alias) switch view
0, 1, 2 … namespace: all, recently used
/text filter the list, Esc clears it
l, d, y, e, s logs, describe, YAML, edit, shell
Shift-F, :pf forward a port, show forwards
:xray deploy laden tree of Deployments, Pods, containers, Secrets
Ctrl-S save the current list as CSV
:q or Ctrl-C quit

Tested: The screens in this chapter are real captures from a terminal of 150 × 32 characters, with empty lines and right margins trimmed.

Drill 1: Installing and Starting

DARCH=$(dpkg --print-architecture)
curl -sL https://github.com/derailed/k9s/releases/download/v0.51.0/k9s_Linux_$DARCH.tar.gz | tar xz -C /tmp k9s
sudo install /tmp/k9s /usr/local/bin/k9s
k9s version -s

Expected:

Version              v0.51.0
Commit               558caafe7ba067467de46b320cc22ef11fef9c34
Date                 2026-06-06T14:04:25Z

k9s info shows where k9s looks for its files. Important for later: plugins, hotkeys and aliases are under ~/.config/k9s/, logs and exports under ~/.local/state/k9s/.

k9s -n laden

Expected (capture, shortened):

 Context: kind-werkzeuge [RW]                      <0> all       <a>       Attach       <ctrl-k>  Kill
 Cluster: kind-werkzeuge                           <1> laden     <ctrl-d>  Delete       <l>       Logs
 User:    kind-werkzeuge                           <2> default   <d>       Describe     <p>       Logs Previous
 K9s Rev: v0.51.0                                                <e>       Edit         <shift-f> Port-Forward
 K8s Rev: v1.37.0                                                <?>       Help         <z>       Sanitize
 CPU:     n/a                                                    <shift-j> Jump Owner   <s>       Shell
 MEM:     n/a
┌──────────────────────────────────── pods(laden)[7] ─────────────────────────────────────┐
│ NAME↑                          PF        READY        STATUS                 RESTARTS IP
│ api-7d5f77c79b-725kc           ●         1/1          Running                       0 10.244.2.1
│ api-7d5f77c79b-gjmtc           ●         1/1          Running                       0 10.244.1.9
│ cache-55b4555575-xkp72         ●         1/1          Running                       0 10.244.2.1
│ web-67649cfddb-6d48k           ●         1/1          Running                       0 10.244.2.2
│ web-67649cfddb-fzrd6           ●         1/1          Running                       0 10.244.2.1
│ web-67649cfddb-tggck           ●         1/1          Running                       0 10.244.1.6
│ werkbank                       ●         1/1          Running                       0 10.244.2.1

Top left: context, cluster and versions; [RW] means read and write. Next to it, the namespaces under number keys and the keys of the current view. CPU and MEM show n/a because no metrics-server is running (Day 9 changes that).

Save your progress? Sign up for free

Drill 2: Views, Namespaces, Filters

: starts command mode. Type :deploy and Enter, then 0 for all namespaces:

Expected (capture after :deploy, Enter, 0):

┌─────────────────────────────── deployments(all)[9] ────────────────────────────────┐
│ NAMESPACE                      NAME↑                                          READY
│ laden                          api                                              2/2
│ laden                          cache                                            1/1
│ kube-system                    cilium-operator                                  1/1
│ kube-system                    coredns                                          2/2
│ kube-system                    hubble-relay                                     1/1
│ kube-system                    hubble-ui                                        1/1
│ local-path-storage             local-path-provisioner                           1/1
│ lager                          packer                                           1/1
│ laden                          web                                              3/3

/ filters the list, also with regular expressions. /cache|packer and Enter:

Expected:

┌────────────────────── deployments(all)[2] </cache|packer> ──────────────────────┐
│ NAMESPACE                 NAME↑                                   READY
│ laden                     cache                                     1/1
│ lager                     packer                                    1/1

The filter is shown in the title. Esc clears it. With Enter on a row, k9s goes one level deeper, from the Deployment to its Pods, from the Pod to its containers.

Save your progress? Sign up for free

Drill 3: XRay

What is attached to a Deployment? :xray deploy laden shows the tree down to the Secrets and ServiceAccounts:

Expected (capture, shortened):

┌──────────────────── Xray-Deployments(laden)[4] ────────────────────┐
│ 🪂 deployments(1)
│ └──🗂  laden(3)
│    ├──🪂 api(1) [2/2/0]
│    │  └──🗂  laden(2)
│    │     ├──🚛 api-7d5f77c79b-725kc(2) [1/1]
│    │     │  ├──💳 api
│    │     │  └──🐳 whoami(1)
│    │     │     └──🔒 db-zugang
│    ├──🪂 cache(1) [1/1/0]
│    │  └──🗂  laden(1)
│    │     └──🚛 cache-55b4555575-xkp72(2) [1/1]
│    │        ├──💳 default
│    │        └──🐳 redis
│    └──🪂 web(1) [3/3/0]

Per Pod the ServiceAccount (💳), and per container the Secrets it uses (🔒). Without a tool, this would be a jq query across Pods and Deployments. tree from Day 4 goes the other way: it follows the owners, not the references.

Save your progress? Sign up for free

Drill 4: Logs and Port Forwarding

:pods lager, Enter, /packer, Enter, then l on the Pod:

Expected (capture, shortened):

┌──────────── Logs(lager/packer-5867bb6b7d-g8gdf:packer)[tail] ────────────┐
│          Autoscroll:On      ColumnLock:Off     FullScreen:Off     Timestamps:Off     Wrap:Off
│ 11:45:51 Paket gepackt: 24365
│ 11:45:56 Paket gepackt: 12233
│ 11:46:01 Paket gepackt: 26166

The logs follow live. In the log view, s toggles autoscroll, t timestamps and w line wrapping, and 0 to 5 select the time range.

Port forwarding on a frontend Pod: :pods laden, /web-, Enter, arrow to a Pod, Shift-F:

Expected (capture):

┌───────────────────<PortForward>────────────────────┐
│                                                    │
│             laden/web-67649cfddb-6d48k             │
│                                                    │
│ Container Port: nginx::80                          │
│ Local Port:     80                                 │
│ Address:        localhost                          │
│                                                    │
│                   OK     Cancel                    │

k9s suggests the same port locally. A normal user may not open ports below 1024: Tab to the field “Local Port”, delete with Backspace, type 8080, Tab twice to OK, Enter. In a second terminal:

curl -s localhost:8080 | grep -o "<title>.*</title>"

Expected:

<title>Welcome to nginx!</title>

:pf lists all forwards of this k9s session, with counters for requests and status codes:

Expected (capture):

┌────────────────────────── portforwards(laden)[1] ──────────────────────────┐
│ NAME                           CONTAINER          PORTS             URL
│ web-67649cfddb-6d48k           nginx              8080:80           http://localhost:8080/

The forward ends with k9s. For permanent forwards, kubectl port-forward in its own terminal remains the way to go.

Save your progress? Sign up for free

Drill 5: Plugins, Hotkeys, Aliases

k9s calls arbitrary commands as plugins, with variables for the selected resource. The neat plugin from Day 4 as a keyboard shortcut:

mkdir -p ~/.config/k9s
cat > ~/.config/k9s/plugins.yaml <<'EOF'
plugins:
  neat:
    shortCut: Ctrl-Y
    description: YAML ohne Ballast
    scopes: [all]
    command: bash
    background: false
    args:
    - -c
    - kubectl get $RESOURCE_NAME $NAME -n $NAMESPACE --context $CONTEXT -o yaml | kubectl neat | less
EOF
cat > ~/.config/k9s/hotkeys.yaml <<'EOF'
hotKeys:
  shift-1:
    shortCut: Shift-1
    description: Laden
    command: pods laden
  shift-2:
    shortCut: Shift-2
    description: Lager
    command: pods lager
EOF
cat > ~/.config/k9s/aliases.yaml <<'EOF'
aliases:
  laden: v1/pods laden
  dp: apps/v1/deployments
EOF

Restart k9s, :dp, Enter, /cache, Enter, Ctrl-Y:

Expected (capture, beginning):

apiVersion: apps/v1
kind: Deployment
metadata:
  annotations:
    deployment.kubernetes.io/revision: "1"
  labels:
    app: cache
  name: cache
  namespace: laden

q quits less, and k9s is back. Shift-1 now jumps to the Pods in laden, Shift-2 to lager.

Tested: The first attempt with shortCut: Shift-N showed “Plugins load failed!” at the top right. The log ~/.local/state/k9s/k9s.log names the reason: duplicate plugin key found for "Shift-N". k9s uses capital letters itself, for sorting by columns. You find free shortcuts with ?, and the log is the first place to look when a customization has no effect.

Save your progress? Sign up for free

Drill 6: Read Only

k9s -n laden --readonly

Expected (capture, first lines):

 Context: kind-werkzeuge [R]                       <0> all       <d>       Describe        <o>      Show Node
 Cluster: kind-werkzeuge                           <1> lager     <?>       Help            <f>      Show PortForward

[R] instead of [RW], and the keys for Delete, Kill and Edit are missing; Ctrl-D no longer does anything. For production contexts, the switch belongs in an alias (alias k9p='k9s --readonly') or in the per-context configuration under ~/.local/share/k9s/clusters/.

Save your progress? Sign up for free

Exercise: Speed Drills

In k9s, without kubectl:

  1. How many Pods run in the namespace lager? Target time: 15 s
  2. Which Secret does the container whoami use? Target time: 30 s
  3. What did the last runs of the CronJob inventur (stocktaking) print? Target time: 45 s

Solution

  1. Shift-2 (hotkey from Drill 5) or :pods lager: the number is in the title.
┌──────────────────── pods(lager)[3] ────────────────────┐
│ NAME↑                          PF       READY       STATUS
│ inventur-29925330-rc99b        ●        0/1         Completed
│ inventur-29925340-22kqb        ●        0/1         Completed

Three Pods: the packer and two completed runs of inventur. 2. :xray deploy laden: db-zugang under whoami. 3. In the same list, select an inventur-… Pod and press l: “Inventur: … alles gezählt”.

Save your progress? Sign up for free

Challenge

The boss wants a list of all Deployments with labels as a table for a presentation, and she wants it now. kubectl and jq are forbidden, only k9s.

Hint: In the help (?), under “General”, there is a key for saving.

Solution

:dp, Enter, 0 for all namespaces, then Ctrl-S:

Expected (bottom line):

😎 File saved successfully: "deployments-all-1795521087806920510.csv"

The file is stored per cluster and context under the screen dumps:

find ~/.local/state/k9s/screen-dumps -type f
head -4 $(find ~/.local/state/k9s/screen-dumps -type f | head -1)

Expected (shortened):

/home/ubuntu/.local/state/k9s/screen-dumps/kind-werkzeuge/kind-werkzeuge/deployments-all-1795521087806920510.csv
NAMESPACE,NAME,VS,READY,UP-TO-DATE,AVAILABLE,LABELS,VALID,AGE
kube-system,cilium-operator,n/a,1/1,1,1,"app.kubernetes.io/managed-by=Helm,...",,87m
kube-system,hubble-ui,n/a,1/1,1,1,"app.kubernetes.io/managed-by=Helm,...",,87m
laden,web,n/a,3/3,3,3,"app=web,tier=frontend",,29m

A CSV with more columns than the screen view, including all labels. It can be loaded directly into a spreadsheet. What gets saved is always the current view with the active filter.

Save your progress? Sign up for free

Checkpoint

ls ~/.config/k9s/
k9s version -s | head -1

Expected:

aliases.yaml  config.yaml  hotkeys.yaml  plugins.yaml  skins
Version              v0.51.0

k9s creates config.yaml and skins itself on first start.

Interview Note

  • k9s or kubectl? k9s for exploring and ad hoc troubleshooting, kubectl for everything that has to be reproducible: scripts, runbooks, documentation.
  • How do you make k9s safe for production? --readonly or readOnly in the context configuration, plus a user with few permissions. k9s itself does not check permissions; the API server does.

Enterprise Note

  • Plugins, hotkeys and aliases are team knowledge: the files from ~/.config/k9s/ belong in the team repo, just like shell aliases.
  • A k9s plugin can run anything the shell can. Read plugin files from third-party sources before using them, like any script.
  • Other terminal UIs (kdash, ktop, Lens in the terminal) are listed as profiles in Appendix C; k9s is the standard most teams know.

Contents

More