4 min read

Manage Kubernetes Clusters with K9S

K9s provides developers, administrators, and DevOps teams with a comprehensive view of their Kubernetes resources, enabling them to navigate and interact with clusters seamlessly

Kubernetes has become the de facto standard for container orchestration, and efficient management of Kubernetes clusters is crucial for smooth operations. Among the various tools available, K9s has gained popularity for its powerful command-line interface and intuitive features.


Table of contents

In this article, we will explore the advantages of using K9s over other tools like Lens, delve into its basic usage, including essential shortcuts, and discover how to configure K9s for optimal Kubernetes cluster management.

What is k9s

K9s, short for "Kubernetes CLI To the Stars," is a powerful command-line tool designed to streamline the management and monitoring of Kubernetes clusters. It serves as an efficient alternative to graphical user interface (GUI) tools like Lens, offering a lightweight and intuitive approach to Kubernetes administration.

K9s provides developers, administrators, and DevOps teams with a comprehensive view of their Kubernetes resources, enabling them to navigate and interact with clusters seamlessly. With its command-line simplicity and robust feature set, K9s empowers users to efficiently monitor, troubleshoot, and manage their Kubernetes infrastructure.

Advantages of K9s over Lens

  1. Command-Line Simplicity: K9s offers a fast command-line interface for efficient Kubernetes management, making it perfect for experienced users who prefer terminal commands over a GUI like Lens.
  2. Lightweight and Efficient: K9s is lightweight and efficient, ideal for managing Kubernetes on resource-limited machines, especially in large-scale or constrained environments.
  3. Seamless Navigation: K9s provides intuitive navigation, enabling quick access to Kubernetes resources like namespaces, deployments, and services, streamlining cluster management.
  4. Enhanced Insights: K9s offers real-time insights into resource usage, pod status, and logs, helping administrators and developers monitor, troubleshoot, and optimize their clusters efficiently.
  5. Resource Management: K9s enables quick resource management with commands and shortcuts, allowing users to create, edit, and delete deployments, pods, and services efficiently.
  6. Customizing the View: K9s offers customizable views, letting you adjust columns, sorting, and filters to focus on specific resources or namespaces.

Installation

Installing K9s on Linux is a straightforward process that allows you to quickly set up this powerful Kubernetes management tool on your local machine. Follow the steps below to install K9s on your machine, for more details and other operating systems head over to Installation guide

MacOs

 # Via Homebrew
 brew install derailed/k9s/k9s
 # Via MacPort
 sudo port install k9s

Linux

 # Via LinuxBrew
 brew install derailed/k9s/k9s
 # Via PacMan
 pacman -S k9s

Windows

# Via scoop
scoop install k9s
# Via chocolatey
choco install k9s

Basic Usage, Shortcuts, and Configuration

Launch K9s and use the cluster context or configuration file to connect to your Kubernetes cluster. K9s will automatically detect and authenticate with the cluster. You should have your config files placed in KUBECONFIG_PATH or ~/.kube/

# Below command will connect k9s with ~/.kube/config cluster
k9s

# Below command will start k9s using mentioned kubeconfig file
k9s --kubeconfig ~/.kube/prod_config

After Opening K9S you should first focus on the Header part on top of the screen which will show you all the commands and shortcuts required.

💡
On Every object screen you should focus on Header part where all the required Shortcuts are displayed
All Shortcuts in K9S Header

Once connected, K9s presents an interactive command-line interface with a live view of your cluster's resources. Use the following shortcuts to navigate and interact with different Kubernetes objects:

  • Arrow Keys: Move between different resources or containers.
  • Enter Key: View detailed information about a selected resource.
  • / : Filter resources based on a specific keyword or expression.
  • Ctrl + D: Delete a selected resource.
  • e: Edit a selected resource.
  • d: Describe the resource.
  • l: Check logs for the Pod.
  • r: Restart a Pod.
  • Shift + Column Initial: Sort resources based on specific columns.
  • Shift + F: Forward a port for particular pod.

For listing all pod resources use :pod command similarly for other resources you can use resource type prefixed with colon

  • :svc or :services - for listing out all services in the namespace
  • :ep or :endpoints - for listing out all endpoints in the namespace
  • :ns or :namespace - for listing out all namespaces in the cluster

For searching for particular resource from the list you can use / key and type the name of resource

For simplicity k9s gives all the command shortcuts on the particular view at top header, you can:

  • Change namespaces based on give number shortcuts
  • Manage resources with shortcuts
  • Organize list of resources in ascending/descending order with help of column name

Sort by Column

If you want to sort any view (Pod/Services) based on some exact column - you can just press Shift+Column Initial

e.g. If you want to sort items by column Age - Just press Shift + A

K9S Sort by Age Column by Pressing Shift + A

Select Multiple Rows

When you want to select multiple rows and do some action on selected items you can press Space for selection and the action shortcut.

e.g. If you want to delete multiple pods then you can select these pods with Space and then do the action like Deletion with Ctrl+D

K9S Selecting Multiple Rows

Conclusion

K9s offers a powerful command-line interface for managing Kubernetes clusters, providing a lightweight, efficient, and intuitive alternative to tools like Lens. Its command-line simplicity, seamless navigation, enhanced insights, and customizability make it a valuable asset for developers and administrators seeking efficient Kubernetes management.

By mastering K9s and leveraging its extensive features, including the handy shortcuts, you can streamline your Kubernetes operations and unlock the full potential of your infrastructure. Remember, K9s is a versatile tool, and exploring its advanced features.