
provides a terminal user interface for interacting with Kubernetes clusters. The goal of this Open Source project is to make it easy to navigate, monitor, and manage applications in K8s. K9s constantly monitors changes in Kubernetes and offers quick commands to work with monitored resources.
The project is written in Go and has been around for over a year and a half: the first commit was made on February 1, 2019. At the time of writing, there are 9000+ stars on and about 80 contributors. Let's see what k9s can do?
Installation and launch
This is a client (in relation to the Kubernetes cluster) application that is easiest to run as a Docker image:
docker run --rm -it -v $KUBECONFIG:/root/.kube/config quay.io/derailed/k9sFor some Linux distributions and other operating systems, there are also ready-to-install . In general, for Linux systems, you can install a binary file:
sudo wget -qO- https://github.com/derailed/k9s/releases/download/v0.22.0/k9s_Linux_x86_64.tar.gz | tar zxvf - -C /tmp/
sudo mv /tmp/k9s /usr/local/binThere are no specific requirements for the K8s cluster itself. Judging by the reviews, the application also works with older versions of Kubernetes such as 1.12.
The application is launched using the standard config .kube/config - similar to how it does kubectl.
Navigation
By default, a window opens with the default namespace specified for the context. That is, if you wrote kubectl config set-context --current --namespace=test, then the namespace will open test. (See below for changing contexts/namespaces.)

Transition into command mode is done by pressing ":". After that, you can control the operation of k9s using commands - for example, to view the list of StatefulSets (in the current namespace), you can enter :sts.
![]()
For some other Kubernetes resources:
-
:ns— namespaces; -
:deploy— Deployments; -
:ing- Ingress; -
:svc— services.
To display a complete list of resource types available for viewing, there is a command :aliases.
It is also convenient to view the list of commands available by hot key combinations within the current window: to do this, just click on "?".

Also in k9s there is search mode, to go to which it is enough to enter "/". With it, a search is performed on the contents of the current "window". Let's say if you previously entered :ns, you have a list of namespaces open. If there are too many of them, then in order not to scroll down for a long time, it is enough to enter in the window with namespaces /mynamespace.
To search by labels, you can select all pods in the desired namespace, then enter, for example, / -l app=whoami. We will get a list of pods with this label:
![]()
The search works in all kinds of windows, including logs, viewing YAML manifests, and describe for resources - see below for more details on these features.
What does the overall navigation flow look like?
With the command :ctx you can choose a context:

To select a namespace, there is the already mentioned command :ns, and then you can use the search for the desired space: /test.
If we now select the resource we are interested in (for example, the same StatefulSet), the corresponding information will appear for it: how many pods are running with brief information about them.
![]()
Only pods may be of interest - then it is enough to enter :pod. In the case of ConfigMaps (:cm - for the list of these resources), you can select the object of interest and click on "u", after which K9s will tell you who specifically uses it (this CM).
Another handy feature for viewing resources is their "x-ray" (Xray view). This mode is called by the command :xray RESOURCE and ... it's easier to show how it works than to explain. Here is an illustration for StatefulSets:

(Each of these resources can be edited, changed, made describe.)
And here is Deployment with Ingress:

Working with resources
You can get information about each resource in YAML or its describe by pressing the corresponding keyboard shortcuts (“y” and “d”, respectively). Of course, there are even more basic operations: their list and keyboard shortcuts are always visible thanks to a convenient “header” in the interface (hidden by pressing Ctrl + e).

When editing any resource ("e" after its selection), the text editor defined in the environment variables is opened (export EDITOR=vim).
And here is what the detailed description of the resource looks like (describe):

This output (or the output of viewing the YAML resource manifest) can be saved using the familiar keyboard shortcut Ctrl + s. Where it will be saved will be known from the K9s message:
Log /tmp/k9s-screens-root/kubernetes/Describe-1601244920104133900.yml saved successfully! You can also restore resources from the created backup files, after removing system labels and annotations. To do this, you need to go to the directory with them (:dir /tmp), then select the desired file and apply apply.
By the way, at any time you can roll back to the previous ReplicaSet if there are problems with the current one. To do this, select the desired RS (:rs for their list):

... and rollback with Ctrl + l. We should get a notification that everything went well:
k9s/whoami-5cfbdbb469 successfully rolled backAnd to scale the replicas, just click on the "s" (scale) and select the desired number of instances:

You can enter any of the containers using the shell: to do this, go to the desired pod, click on "s" (shell) and select the container.
Other features
Of course, viewing logs is also supported ("l" for the selected resource). And in order to watch new logs, there is no need to constantly press Enter: it is enough to mark (“m”), and then only track new messages.

Also in the same window, you can select the time range for the output of logs:
- key "1" - for 1 minute;
- "2" - 5 minutes;
- "3" - 15 minutes;
- "4" - 30 minutes;
- "5" - 1 hour;
- "0" - for the entire lifetime of the pod.
Special operating mode Pulse (command :pulse) shows general information about the Kubernetes cluster:

In it you can see the number of resources and their status (green shows those that have the status Running).
Another cool feature of K9s is called Popeye. It checks all resources for certain criteria of correctness and displays the resulting "rating" with explanations. For example, you can see that there are not enough samples or limits, and some container can run as root ...

There is basic Helm support. For example, this is how you can see the releases deployed in the cluster:
:helm all # все
:helm $namespace # в конкретном пространстве имен
Benchmark
Even built into K9s is a simple HTTP server load generator, an alternative to the better known ab (ApacheBench).
To enable it, you need to enable port-forward in the pod. To do this, select the pod and press Shift + f, go to the port-forward submenu using the "pf" alias.

After selecting the port and pressing Ctrl + b, the benchmark itself will start. The results of his work are stored in /tmp and are available for later viewing in K9s.


To change the configuration of the benchmark, you need to create a file $HOME/.k9s/bench-<my_context>.yml (determined for each cluster).
NB: It is important that the extension of all YAML files in a directory .k9s it was exactly .yml (.yaml does not work correctly).
Configuration example:
benchmarks:
defaults:
# Количество потоков
concurrency: 2
# Количество запросов
requests: 1000
containers:
# Настройки для контейнера с бенчмарком
# Контейнер определяется как namespace/pod-name:container-name
default/nginx:nginx:
concurrency: 2
requests: 10000
http:
path: /
method: POST
body:
{"foo":"bar"}
header:
Accept:
- text/html
Content-Type:
- application/json
services:
# Можно проводить бенчмарк на сервисах типа NodePort и LoadBalancer
# Синтаксис: namespace/service-name
default/nginx:
concurrency: 5
requests: 500
http:
method: GET
path: /auth
auth:
user: flant
password: s3cr3tp455w0rd
Interface
The appearance of columns for lists of resources is modified by creating a file $HOME/.k9s/views.yml. An example of its content:
k9s:
views:
v1/pods:
columns:
- AGE
- NAMESPACE
- NAME
- IP
- NODE
- STATUS
- READY
v1/services:
columns:
- AGE
- NAMESPACE
- NAME
- TYPE
- CLUSTER-IP
True, there is not enough column for labels, for which there is .
Sorting by columns is carried out by keyboard shortcuts:
- Shift + n - by name;
- Shift + o - by nodes;
- Shift + i - by IP;
- Shift + a - by the lifetime of the container;
- Shift + t - by the number of restarts;
- Shift + r - by readiness status;
- Shift + c - by CPU consumption;
- Shift + m - by memory consumption.
If someone does not like the default color scheme, K9s even supports . Ready-made examples (7 pieces) are available . Here is an example of one of these skins (in the navy):

Plugins
Finally, the allow you to expand the capabilities of K9s. I myself have used only one of them in my work - kubectl get all -n $namespace.
It looks like this. Create a file $HOME/.k9s/plugin.yml with content like this:
plugin:
get-all:
shortCut: g
confirm: false
description: get all
scopes:
- all
command: sh
background: false
args:
- -c
- "kubectl -n $NAMESPACE get all -o wide | less"Now you can go to the namespace and press "g" to execute with the corresponding command:

Among the plugins there are, for example, integrations with kubectl-jq and a utility for viewing logs .
Conclusion
For my taste, K9s turned out to be very convenient to work with: you can quickly get used to looking for everything you need without using it. kubectl. I was pleased with the view of the logs and their saving, quick editing of resources, the speed of work in general *, the Popeye mode turned out to be useful. A special mention is the ability to create plugins and modify the application to fit your needs.
* Although, with a large volume of logs, I also noticed the slow operation of K9s. At such moments, the utility "ate" 2 cores from the Intel Xeon E312xx and could even freeze.
What is missing at the moment? Quick rollback to the previous version (we are not talking about RS) without going to the directory. In addition, recovery occurs only for total resource: if you delete an annotation or label, you will have to delete and restore the entire resource (this is where you need to go to the directory). Another trifle - there is not enough date for such saved "backups".
P.S.
Read also on our blog:
- «";
- «";
- «».
Source: habr.com
