How To Install K9s on Ubuntu 22.04 LTS
In this tutorial, we will show you how to install K9s on Ubuntu 22.04 LTS. Kubernetes has revolutionized container orchestration, but managing clusters through the command line can be challenging even for experienced system administrators. Fortunately, K9s offers a powerful terminal-based UI that simplifies Kubernetes management. This comprehensive guide will walk you through various methods to install K9s on Ubuntu 22.04 LTS, explore its features, and help you master this essential tool for efficient Kubernetes administration.
Understanding K9s and Its Capabilities
K9s is an open-source, terminal-based user interface designed specifically for interacting with Kubernetes clusters. Unlike traditional command-line tools that require memorizing numerous commands and flags, K9s provides an intuitive, interactive dashboard that displays real-time information about your Kubernetes resources.
Key Features and Benefits
K9s offers numerous capabilities that make Kubernetes management more accessible and efficient:
- Real-time monitoring of cluster resources such as pods, deployments, services, and nodes
- Terminal-based interface for quick navigation and command execution without leaving your terminal
- Resource management capabilities for easy editing, deletion, and creation of Kubernetes objects
- Log streaming functionality that allows viewing logs from multiple containers simultaneously
- Plugin support that extends functionality through custom scripts and integrations
- Customizable views and keyboard shortcuts for increased productivity
What sets K9s apart from standard kubectl commands is its ability to provide a comprehensive overview of your cluster’s state while facilitating rapid response to issues. System administrators and DevOps engineers benefit from reduced context-switching, faster troubleshooting, and a more intuitive approach to Kubernetes operations.
Prerequisites for Installing K9s
Before proceeding with K9s installation on your Ubuntu 22.04 LTS system, ensure you have the following prerequisites in place:
- A functional Ubuntu 22.04 LTS installation (desktop or server) with sufficient resources
- Terminal access with administrator privileges (sudo)
- A working Kubernetes environment (either a local development cluster or connection to a remote cluster)
- kubectl installed and properly configured to communicate with your Kubernetes cluster
- Active internet connection to download necessary packages and dependencies
System Requirements
K9s has minimal system requirements, but for optimal performance, your Ubuntu system should have:
- At least 2GB of RAM for smooth operation
- 1GB or more of free disk space
- A recent CPU (dual-core or better)
- A terminal with color support for best visual experience
Having a properly configured Kubernetes environment is crucial, as K9s is designed to interact with Kubernetes clusters through your kubeconfig file.
Method 1: Installing K9s Using Snap Package Manager
The Snap package manager provides one of the simplest ways to install K9s on Ubuntu 22.04 LTS. Snap packages are self-contained applications that include all dependencies, ensuring consistent behavior across different Linux distributions.
Step 1: Ensure Snap is Installed
Ubuntu 22.04 comes with Snap pre-installed, but if it’s not available on your system, install it with:
sudo apt update
sudo apt install snapd
Step 2: Install K9s via Snap
Once Snap is ready, installing K9s is as simple as running:
sudo snap install k9s
The command downloads and installs the latest stable version of K9s from the Snap store.
Step 3: Verify the Installation
Confirm that K9s installed correctly by checking its version:
k9s version
This should display the current version number and build information, indicating a successful installation.
The primary advantage of using Snap for K9s installation is automatic updates. Snap will periodically check for new versions and update K9s without requiring manual intervention, ensuring you always have the latest features and security patches.
Method 2: Installing K9s Using Homebrew
Homebrew, originally developed for macOS, is now available for Linux systems including Ubuntu 22.04, providing another convenient way to install and manage K9s.
Step 1: Install Homebrew on Ubuntu
If you don’t already have Homebrew installed, run the following commands:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
After installation completes, add Homebrew to your PATH by running the commands suggested in the installation output, typically:
echo 'eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"' >> ~/.profile
eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"
Step 2: Install K9s via Homebrew
With Homebrew configured, install K9s by running:
brew install k9s
Step 3: Verify the Installation
Confirm the installation was successful:
k9s version
Using Homebrew for K9s installation offers several benefits, including simplified updates (via brew upgrade k9s
) and easy management of multiple packages through a consistent interface. However, it does require more initial setup compared to using Snap.
Method 3: Installing K9s via Direct Binary Download
For users who prefer manual control over their software installations or work in environments where package managers are restricted, installing K9s directly from the binary release is an excellent option.
Step 1: Download the Latest K9s Binary
Create a directory for the download and navigate to it:
mkdir ~/k9s-installation
cd ~/k9s-installation
Download the latest K9s release for Linux:
curl -LO https://github.com/derailed/k9s/releases/download/v0.32.4/k9s_Linux_amd64.tar.gz
Note: Replace v0.32.4
with the latest version available on the K9s GitHub releases page.
Step 2: Extract and Install the Binary
Extract the downloaded archive:
tar xf k9s_Linux_amd64.tar.gz
Move the K9s executable to a directory in your PATH:
sudo mv k9s /usr/local/bin
This makes the K9s command available system-wide.
Step 3: Verify the Installation
Confirm that K9s is correctly installed:
k9s version
The binary installation method gives you complete control over when and how you update K9s. This approach is particularly useful in environments where internet access is limited, as you can download the binary once and deploy it to multiple systems.
Method 4: Installing K9s via Debian Package
Another straightforward method for Ubuntu users is installing K9s using the Debian package (.deb) format, which integrates well with Ubuntu’s package management system.
Step 1: Download the K9s Debian Package
Download the latest Debian package from the K9s GitHub releases:
wget https://github.com/derailed/k9s/releases/download/v0.32.5/k9s_linux_amd64.deb
Note: Check the K9s GitHub releases page for the most recent version and update the command accordingly.
Step 2: Install the Debian Package
Install the downloaded package using apt:
sudo apt install ./k9s_linux_amd64.deb
Step 3: Verify the Installation
Confirm the installation was successful:
k9s version
Step 4: Clean Up
After installation, you can remove the downloaded package file:
rm k9s_linux_amd64.deb
The Debian package installation method integrates K9s with Ubuntu’s package management system, making it easier to manage alongside other system packages and potentially allowing for updates through your system’s regular update process.
Method 5: Building K9s from Source Code
For developers and advanced users who want complete control or need to make modifications to the source code, building K9s from source is a viable option.
Step 1: Install Go Programming Language
K9s is written in Go, so you’ll need to install the Go compiler:
sudo apt update
sudo apt install golang-go
Verify the Go installation:
go version
Ensure you have Go version 1.16 or higher.
Step 2: Clone the K9s Repository
Clone the K9s source code from GitHub:
mkdir -p ~/go/src/github.com/derailed
cd ~/go/src/github.com/derailed
git clone https://github.com/derailed/k9s.git
cd k9s
Step 3: Build and Install K9s
Build the application:
make build
Install it to your system:
sudo make install
Alternatively, you can build and install in one step:
go install
Step 4: Verify the Installation
Check that K9s was installed correctly:
k9s version
Building from source provides several advantages, including access to the latest development features before they’re released, the ability to customize the code to your specific needs, and learning opportunities by exploring the source code.
Initial Configuration and Setup
After installing K9s, you can configure it to match your workflow and preferences through a configuration file.
Creating and Configuring K9s
K9s uses a YAML configuration file located at ~/.k9s/config.yml
. If this file doesn’t exist, create it:
mkdir -p ~/.k9s
touch ~/.k9s/config.yml
Open the file in your preferred text editor and customize it according to your needs:
# ~/.k9s/config.yml
k9s:
refreshRate: 2 # Screen refresh rate in seconds
maxConnRetry: 5 # Connection retry threshold
enableMouse: true # Enable mouse support
headless: false # Disable UI animations
logoless: false # Hide K9s logo
crumbsless: false # Hide resource path crumbs
readOnly: false # Sets read-only mode
noIcons: false # Disable icons
logger:
tail: 100 # Max number of log lines
buffer: 5000 # Max number of logs kept
sinceSeconds: 60 # Only display logs for last n seconds
currentContext: default # Current Kubernetes context
currentCluster: default # Current Kubernetes cluster
clusters:
default:
namespace:
active: default # Active namespace
favorites: # Favorite namespaces
- default
- kube-system
view:
active: pods # Active Kubernetes resource
This configuration provides a starting point that you can modify to suit your specific requirements. Save the file after making changes.
Basic Usage Guide for K9s on Ubuntu
Once K9s is installed and configured, you can start exploring its features and capabilities.
Launching K9s
To start K9s, simply open a terminal and type:
k9s
This launches the K9s interface, connecting to your current Kubernetes context as defined in your kubeconfig file.
Essential Keyboard Commands
K9s is designed to be navigated primarily using keyboard commands:
:
– Activate command mode for entering commands/
– Filter resources by nameEsc
– Clear filters or exit command mode?
– Display help with all available keyboard shortcutsCtrl+C
orq
– Exit K9s
Viewing Different Resource Types
To view specific Kubernetes resources:
:pods
or:po
– View pods:deployments
or:dp
– View deployments:services
or:svc
– View services:nodes
or:no
– View nodes:namespaces
or:ns
– View namespaces
Navigating and Interacting
Once viewing a resource list:
- Use arrow keys to navigate up and down
- Press
Enter
to view details about the selected resource - Press
d
to describe the selected resource - Press
l
to view logs (for pods) - Press
s
to view the YAML specification - Press
e
to edit the resource - Press
Ctrl+d
to delete the selected resource
K9s provides a wealth of keyboard shortcuts that make navigating and managing your Kubernetes clusters efficient and intuitive. Exploring these commands is key to mastering the tool and enhancing your productivity.
Advanced Features and Customization
K9s offers numerous advanced features and customization options that can significantly enhance your Kubernetes management experience.
Custom Resource Views
K9s allows you to create custom views for frequently used resources:
:alias my-pods :po -l app=myapp
– Create an alias named “my-pods” that shows pods with label “app=myapp”:alias my-deps :dp -n production
– Create an alias for deployments in the production namespace
These aliases persist between sessions, providing quick access to frequently used resource views.
Customizing the UI with Skins
K9s supports custom skins to change its appearance. Create a skin file at ~/.k9s/skin.yml
:
# Example dark theme
k9s:
body:
fgColor: dodgerblue
bgColor: black
logoColor: orange
prompt:
fgColor: cadetblue
bgColor: black
suggestColor: dodgerblue
info:
fgColor: orange
sectionColor: white
Restart K9s to apply the new skin.
Plugin Development
K9s can be extended with plugins. Create a directory for plugins:
mkdir -p ~/.k9s/plugins
Create a plugin script (for example, a script to analyze pod resource usage):
#!/bin/bash
# ~/.k9s/plugins/resource-usage.sh
kubectl top pod $1 -n $2
Make the script executable:
chmod +x ~/.k9s/plugins/resource-usage.sh
Configure the plugin in ~/.k9s/plugins.yml
:
plugins:
resource-usage:
shortCut: Shift-U
description: Analyze pod resource usage
scopes:
- pods
command: ~/.k9s/plugins/resource-usage.sh
background: false
args:
- $NAME
- $NAMESPACE
This plugin will be available when viewing pods by pressing Shift+U
.
Troubleshooting Common Installation Issues
Even with straightforward installation methods, you might encounter issues. Here are solutions for common problems when installing K9s on Ubuntu 22.04.
Missing Dependencies
If you encounter dependency errors during installation:
sudo apt update
sudo apt install -y curl wget tar
For source builds, ensure all Go dependencies are installed:
sudo apt install -y golang-go build-essential git
Permission Issues
If you experience permission denied errors:
# For binary installation
sudo chmod +x /usr/local/bin/k9s
# For Snap installation issues
sudo snap refresh core
Kubernetes Configuration Problems
If K9s can’t connect to your cluster:
- Verify your kubeconfig is correctly set up:
kubectl cluster-info
- Check if kubectl can access your cluster:
kubectl get nodes
- Ensure your kubeconfig is in the default location or set the KUBECONFIG environment variable:
export KUBECONFIG=~/path/to/your/kubeconfig
Display Issues
If K9s shows rendering problems:
- Verify your terminal supports colors:
echo $TERM
- Try setting a different terminal type:
export TERM=xterm-256color
Logging for Debugging
K9s logs are stored in:
~/.k9s/logs
Review these logs for detailed error information when troubleshooting issues.
Updating and Maintaining K9s
Keeping K9s updated ensures you have the latest features, bug fixes, and security patches.
Checking Current Version
Determine your currently installed version:
k9s version
Update Procedures for Each Installation Method
For Snap installations:
sudo snap refresh k9s
For Homebrew installations:
brew update
brew upgrade k9s
For binary installations:
- Download the latest release
- Replace the existing binary in
/usr/local/bin/
For Debian package installations:
- Download the latest .deb package
- Install it with
sudo apt install ./k9s_linux_amd64.deb
For source installations:
cd ~/go/src/github.com/derailed/k9s
git pull
make build
sudo make install
Backup Configuration Before Updates
Before updating, backup your configurations:
cp -r ~/.k9s ~/.k9s.backup
This allows you to restore your settings if anything goes wrong during the update.
Congratulations! You have successfully installed K9s. Thanks for using this tutorial for installing the K9s Kubernetes CLI on the Ubuntu system. For additional help or useful information, we recommend you check the official K9s website.