What is k9s?
As per official documentation
K9s is a terminal-based UI to interact with your Kubernetes clusters. K9s is an open-source project and is written in GO language. The way it works, it continuously monitors your Kubernetes cluster for any changes and provides a shortcut command to interact with Kubernetes resources.
Installation of K9s is pretty straightforward, and binaries for various distribution are available on the release page.
# Download the binaries(In this use case Linux) wget https://github.com/derailed/k9s/releases/download/v0.24.2/k9s_Linux_x86_64.tar.gz # Move it to the desired path sudo cp k9s /usr/local/bin #Change the permission sudo chmod +x /usr/local/bin/k9s
Start using k9s
To start using k9s, execute the k9s command on your console. It will use the standard kubeconfig file(~/.kube/config) same as kubectl.
As you can see, this is showing the default pod running in the default namespace. This is equivalent of
kubectl get pod
kubectl get pod
- To see all the pod in all namespaces (press 0). This is equivalent of
kubectl get pod -A
- If you are looking for a specific pod for e.g., kube-proxy, there is a search mode to do that. Type / followed the resource you are searching (/kube-proxy)
- Similarly, if you want to perform a search based on labels, type -l followed by the name of the label(-l app=nginx)
- To go back to the default namespace(press 1)
Note: This dashboard refreshes itself every 2 sec. To modify the default configuration, modify the k9s configuration file.
K9s configuration file
For e.g., To modify the default k9s refresh interval, edit the config file and change the refreshRate to the desired value.
- To check all the replicaset running in your cluster, type colon followed by rs(:rs)
Note: In case if you want to rollback to the previous version, type ctrl+l.
- Now to check all the deployment running in your cluster type colon followed by deploy(:deploy) and then enter.
This is similar to
kubectl get deployment
- Now to get all configmap, press: followed by cm(:cm)
Now select the configmap and then press u to get the list of resources that use it.
- Some other useful commands
:svc - Services(equivalent kubectl command: kubectl get svc)
:ns - namespaces(equivalent kubectl command: kubectl get ns)
:ing - ingress(equivalent kubectl command: kubectl get ingress)
- To get the complete help of all the commands just type ?
- press d for describe(equivalent kubectl command: kubectl describe pod <pod name>)
- Press l for logs(equivalent kubectl command: kubectl logs <pod name>). To select the time range to display these logs
0: for the entire lifecycle of the pod
1: last 1 min
2: last 5 min
3: last 15 min
4: last 30 min
5: last 1 hour
- press y for yaml(equivalent kubectl command: kubectl get pod <pod name> -o yaml)
- If you want to save the output of the yaml manifest file, press ctrl + s. As you can see at the bottom of the screen, it’s storing the output in /tmp/k9s-screens-prashant/kind-my-test-cluster/YAML-1612243106814649920.yml file).
- This is one of the most common debugging practices where you save the output of your manifest to a file, modify it(by fixing the issue or add some configuration) and then apply it. If you want to follow the same practice using k9s type :dir and then the location where you save the manifests(:dir /tmp). Drill down till you reach the location where manifest is saved(/tmp/k9s-screens-prashant/kind-my-test-cluster/YAML-1612243106814649920.yml), and to apply that configuration, press a(a for apply).
- press e to edit the deployments(equivalent kubectl command: kubectl edit pod <pod name>)
- NOTE: This will open the text editor based on your environment variable(to set vim as the default editor export EDITOR=vim)
- ctrl +d to delete the resource (equivalent kubectl command: kubectl delete pod <pod name>)
- ctrl + k to kill the resource or forceful deletion(equivalent kubectl command: kubectl delete pod <pod name> — force — grace-period=0)
- To get the shell access press s (equivalent kubectl command: kubectl exec -it nginx-6799fc88d8-wpq7w — sh)
- If you want to switch to a particular context type : followed by ctx(:ctx)
- If you want to dig into your cluster resources and see it dependencies, type :xray followed by a resource(:xray pod)
- If you want to see the entire state of your cluster with the help of the dashboard, then type a colon followed by pulse(:pulse).
- Another useful feature provided by k9s is called Popeye. Popeye is a great analysis tool. It performs the resource scan and gives your report about any misconfiguration or operation issues. Some common errors reported by Popeye are container running as root, missing probes, and resource limits.
- If you further drill down by just typing an enter, you will see something like this.
- k9s also support basic helm command; e.g., you can run
helm repo list or helm list
K9s comes with a lot of handy features and shortcut which is easy to work with. I personally love this tool as I now don’t need to type the lengthy kubectl command. It also supports features like custom plugins and benchmark(using Hey), which I still need to explore. If you like any feature or believe a K9s feature is missing in the blog, please post in the comment section.