An overview of k9s - an advanced terminal interface for Kubernetes





K9s 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, has existed for more than a year and a half: the first commit was made on February 1, 2019. At the time of this writing, there are 9000+ stars on GitHub 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 and is easiest to run as a Docker image:



docker run --rm -it -v $KUBECONFIG:/root/.kube/config quay.io/derailed/k9s


For some Linux distributions and other operating systems, there are also packages 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/bin


There are no specific requirements for the K8s cluster itself. Based on the reviews, the app works with older versions of Kubernetes such as 1.12.



The application is launched using the standard config .kube/config- in the same way as it does kubectl.



Navigation



By default, a window opens with the standard namespace specified for the context. That is, if you have registered kubectl config set-context --current --namespace=test, then the namespace will open test. (See below about changing contexts / namespaces.) You can







switch to command mode by pressing ":". You can then control the operation of k9s using commands - for example, to view a list of StatefulSets (in the current namespace), you can enter :sts.







For some other Kubernetes resources:



  • :ns - Namespaces;
  • :deploy - Deployments;
  • :ing - Ingresses;
  • :svc - Services.


To display a complete list of the 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: just click on "?"







Also in k9s there is a search mode , to switch to which it is enough to enter "/". It is used to search for the contents of the current "window". Let's say, if you entered before :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 for labels, you can choose all pod'y in the right namespace, and then enter, for example / -l app=whoami. We will get a list of pods with this label:







Search works in all kinds of windows, including logs, viewing YAML manifests, and describefor resources - see below for details on these features.



What is the overall navigation sequence?



With the command :ctx, you can select Context:







To select namespace'a have already mentioned the team :ns, and then you can 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 be displayed for it: how many pods are running with brief information about them.







Only pods may be of interest - then just enter :pod. In the case of ConfigMaps (:cm- for a list of these resources), you can select the object of interest and click on "u", after which K9s will tell you exactly who is using it (this CM).



Another handy feature for viewing resources is their XRay view . Such a mode is invoked by a command :xray RESOURCEand ... it's easier to show how it works than to explain. Here's an illustration for StatefulSets:





(Each of these resources can be edited, modified, done describe.)



And here is a Deployment with Ingress:







Working with resources



Information about each resource can be obtained in YAML or describeby pressing the corresponding keyboard shortcuts ("y" and "d", respectively). There are, of course, even more basic operations: their list and keyboard shortcuts are always in sight thanks to the 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 ( export EDITOR=vim), opens .



And here is how the detailed description of the resource ( describe) looks like :







Such an output (or the output of viewing the YAML manifest of a resource) can be saved using the usual 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!


From the created backup files, you can also restore resources, after removing the 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 required RS ( :rsfor their list):







... and rollback with Ctrl + l. We should receive a notification that everything went well:



k9s/whoami-5cfbdbb469 successfully rolled back


And to scale replicas, just click on "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 possibilities



Of course, viewing the logs ("l" for the selected resource) is also supported. And to watch new logs, there is no need to constantly press Enter: it is enough to mark ("m"), and then track only new messages.







Also, in the same window, you can select the time range for displaying logs:



  • key "1" - in 1 minute;
  • "2" - 5 minutes;
  • "3" - 15 minutes;
  • "4" - 30 minutes;
  • "5" - 1 hour;
  • "0" - for the entire lifetime of the pod.


The special Pulse mode of operation (command :pulse) shows general information about the Kubernetes cluster:







You can see the number of resources and their state in it (those that have a status are shown in green Running).



Another cool feature of the K9s is called Popeye . It checks all resources for certain correctness criteria and displays the resulting "rating" with explanations. For example, you can see that there are not enough probes 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 hey was built into K9s - it's a simple HTTP server load generator, an alternative to the more famous ab (ApacheBench).



To enable it, you need port-forward activation in the pod. To do this, select pod and press Shift + f, go to the port-forward submenu with the alias "pf".







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









To change the benchmark's configuration, you need to create a file $HOME/.k9s/bench-<my_context>.yml(defined for each cluster).



NB: It is important that the extension of all YAML files in the directory is .k9sexactly .yml( .yamldoes 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 column view for resource lists 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 a lack of a column for labels, for which there is an issue in the project .



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 container lifetime;
  • Shift + t - by the number of restarts;
  • Shift + r - by readiness status;
  • Shift + c - by CPU consumption;
  • Shift + m - by memory consumption.


If someone doesn't like the default color scheme, K9s even supports skins . Ready examples (7 pieces) are available here . Here is an example of one of these skins (in the navy) :







Plugins



Finally, plugins allow you to extend the capabilities of K9s. I myself used only one of them in my work - kubectl get all -n $namespace.



It looks like this. Create a file $HOME/.k9s/plugin.ymlwith the following content:



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 stern .



Conclusion



To my taste, the K9s turned out to be very convenient to work with: with it it is pretty quick to get used to looking for everything you need without using it kubectl. I was pleased with the viewing of logs and their saving, quick editing of resources, the speed of work in general *, the Popeye mode turned out to be useful. A separate 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 work of K9s. At such moments the utility β€œate” 2 cores from Intel Xeon E312xx and could even freeze.



What is missing at the moment? Fast rollback to the previous version (it's not about RS) without going to the directory. Plus, recovery only happens for everything.resource: if you deleted the annotation or label, you will have to delete and restore the entire resource (here you will need to go to the directory). Another trifle is that there is not enough date for such saved "backups".



PS



Read also on our blog:



  • " Overview of graphical interfaces for Kubernetes Β»;
  • " Announcement of the Kubernetes Web View web interface (and a brief overview of other web UIs for Kubernetes) ";
  • " Kubebox and other console shells for Kubernetes ".



All Articles