3 min read

Mastering K9s: Unleashing the Power of Command-Line Kubernetes Management

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 provides a command-line interface that allows for quick and efficient interaction with Kubernetes clusters. Unlike Lens, which has a graphical user interface (GUI), K9s allows for rapid execution of commands, making it ideal for seasoned developers and administrators who prefer the command-line experience.
  2. Lightweight and Efficient: K9s is designed to be lightweight and resource-efficient, making it suitable for managing Kubernetes clusters on machines with limited resources. This advantage becomes particularly significant when dealing with large-scale deployments or resource-constrained environments.
  3. Seamless Navigation: K9s offers an intuitive navigation system that allows users to effortlessly explore and interact with their Kubernetes resources. With its comprehensive view of the cluster hierarchy, users can quickly navigate through namespaces, deployments, services, and other resources, saving valuable time and effort.
  4. Enhanced Insights: K9s provides insightful real-time information about your Kubernetes cluster, including resource utilization, pod status, and container logs. These features enable administrators and developers to gain valuable insights into the cluster's health and performance, aiding in troubleshooting and optimization efforts.
  5. Resource Management: With K9s, you can perform various actions on resources, such as creating, editing, and deleting deployments, pods, services, and more. Utilize the intuitive command syntax and shortcuts provided by K9s for efficient resource management.
  6. Customizing the View: K9s allows you to customize the view according to your preferences. You can configure the displayed columns, sorting options, and apply 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

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.
  • Shift + L: Filter resources by labels.
  • Shift + D: Delete a selected resource.
  • Shift + E: Edit a selected resource.
  • Shift + S: Sort resources based on specific columns.
  • Shift + F: Add or remove columns from the resource view.

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

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.