k9s：你没看错，这是一个加速 k8s 集群管理的工具
Kubernetes k8s k9s

> 
> 看看这个很酷的 Kubernetes 管理的终端 UI。
> 
> 
> 


![](/data/attachment/album/202005/25/104742pqjmiroc44honcs5.jpg)


通常情况下，我写的关于 Kubernetes 管理的文章中用的都是做集群管理的 `kubectl` 命令。然而最近，有人给我介绍了 [k9s](https://github.com/derailed/k9s) 项目，可以让我快速查看并解决 Kubernetes 中的日常问题。这极大地改善了我的工作流程，我会在这篇教程中告诉你如何上手它。 


它可以安装在 Mac、Windows 和 Linux 中，每种操作系统的说明可以在[这里](https://github.com/derailed/k9s)找到。请先完成安装，以便能够跟上本教程。


我会使用 Linux 和 Minikube，这是一种在个人电脑上运行 Kubernetes 的轻量级方式。按照[此教程](https://opensource.com/article/18/10/getting-started-minikube)或使用[该文档](https://kubernetes.io/docs/tasks/tools/install-minikube/)来安装它。


### 设置 k9s 配置文件


安装好 `k9s` 应用后，从帮助命令开始总是很好的起点。



```
$ k9s help
```

正如你在帮助信息所看到的，我们可以用 `k9s` 来配置很多功能。我们唯一需要进行的步骤就是编写配置文件。而 `info` 命令会告诉我们该应用程序要在哪里找它的配置文件。



```
$ k9s info
 ____  __.________
|    |/ _/   __   \______
|      < \____    /  ___/
|    |  \   /    /\___ \
|____|__ \ /____//____  >
        \/            \/

Configuration:   /Users/jess/.k9s/config.yml
Logs:            /var/folders/5l/c1y1gcw97szdywgf9rk1100m0000gn/T/k9s-jess.log
Screen Dumps:    /var/folders/5l/c1y1gcw97szdywgf9rk1100m0000gn/T/k9s-screens-jess
```

如果要添加配置文件，该配置目录不存在的话就创建它，然后添加一个配置文件。



```
$ mkdir -p ~/.k9s/
$ touch ~/.k9s/config.yml
```

在这篇介绍中，我们将使用 `k9s` 版本库中推荐的默认 `config.yml`。维护者请注意，这种格式可能会有变化，可以[在这里查看](https://github.com/derailed/k9s#k9s-configuration)最新版本。



```
k9s:
  refreshRate: 2
  headless: false
  readOnly: false
  noIcons: false
  logger:
    tail: 200
    buffer: 500
    sinceSeconds: 300
    fullScreenLogs: false
    textWrap: false
    showTime: false
  currentContext: minikube
  currentCluster: minikube
  clusters:
    minikube:
      namespace:
        active: ""
        favorites:
        - all
        - kube-system
        - default
      view:
        active: dp
  thresholds:
    cpu:
      critical: 90
      warn: 70
    memory:
      critical: 90
      warn: 70
```

我们设置了 `k9s` 寻找本地的 minikube 配置，所以我要确认 minikube 已经上线可以使用了。



```
$ minikube status
host: Running
kubelet: Running
apiserver: Running
kubeconfig: Configured
```

### 运行 k9s 来探索一个 Kubernetes 集群


有了配置文件，并指向我们的本地集群，我们现在可以运行 `k9s` 命令了。



```
$ k9s
```

启动后，会弹出 `k9s` 的基于文本的用户界面。在没有指定命名空间标志的情况下，它会向你显示默认命名空间中的 Pod。


![K9s screenshot](/data/attachment/album/202005/25/104848zllsmdm3ql647l4r.png "K9s screenshot")


如果你运行在一个有很多 Pod 的环境中，默认视图可能会让人不知所措。或者，我们可以将注意力集中在给定的命名空间上。退出该应用程序，运行 `k9s -n <namespace>`，其中 `<namespace>` 是已存在的命名空间。在下图中，我运行了 `k9s -n minecraft`，它显示了我损坏的 Pod：


![K9s screenshot](/data/attachment/album/202005/25/104849bl11m9jlhhm4lw4h.png "K9s screenshot")


所以，一旦你有了 `k9s` 后，有很多事情你可以更快地完成。 


通过快捷键来导航 `k9s`，我们可以随时使用方向键和回车键来选择列出的项目。还有不少其他的通用快捷键可以导航到不同的视图。


* `0`：显示在所有命名空间中的所有 Pod ![K9s screenshot](/data/attachment/album/202005/25/104855hnzmdbpuzq75z5l2.png "K9s screenshot")
* `d`：描述所选的 Pod ![K9s screenshot](/data/attachment/album/202005/25/104859k6cc2z2gb9wddcgd.png "K9s screenshot")
* `l`：显示所选的 Pod 的日志 ![Using k9s to show Kubernetes pod logs](/data/attachment/album/202005/25/104909zpcjjepmjm4oe499.png "Using k9s to show Kubernetes pod logs")


你可能会注意到 `k9s` 设置为使用 [Vim 命令键](https://opensource.com/article/19/3/getting-started-vim)，包括使用 `J` 和 `K` 键上下移动等。Emacs 用户们，败退吧 :)


### 快速查看不同的 Kubernetes 资源


需要去找一个不在 Pod 里的东西吗？是的，我也需要。当我们输入冒号（`:`）键时，可以使用很多快捷方式。从那里，你可以使用下面的命令来导航。


* `:svc`：跳转到服务视图 ![K9s screenshot](/data/attachment/album/202005/25/104911lzjkeeisqkkgoj42.png "K9s screenshot")
* `:deploy`：跳转到部署视图 ![K9s screenshot](/data/attachment/album/202005/25/104914bqd6vddnjt1zzqqz.png "K9s screenshot")
* `:rb`：跳转到角色绑定视图，用于 [基于角色的访问控制（RBAC）](https://kubernetes.io/docs/reference/access-authn-authz/rbac/)管理 ![K9s screenshot](/data/attachment/album/202005/25/104920po5nov990nzvz599.png "K9s screenshot")
* `:namespace`：跳转到命名空间视图 ![K9s screenshot](/data/attachment/album/202005/25/104922yvooon0ro1bn0or0.png "K9s screenshot")
* `:cj`：跳转到 cronjob 视图，查看集群中计划了哪些作业。 ![K9s screenshot](/data/attachment/album/202005/25/104924cnrggikg3x7nrbii.png "K9s screenshot")


这个应用最常用的工具是键盘；要在任何页面往上或下翻页，请使用方向键。如果你需要退出，记得使用 Vim 绑定键，键入 `:q`，然后按回车键离开。


### 用 k9s 排除 Kubernetes 的故障示例


当出现故障的时候，`k9s` 怎么帮忙？举个例子，我让几个 Pod 由于配置错误而死亡。下面你可以看到我那个可怜的 “hello” 部署死了。当我们将其高亮显示出来，可以按 `d` 运行 `describe` 命令，看看是什么原因导致了故障。


![K9s screenshot](/data/attachment/album/202005/25/104927sixjpcxs5osjx11t.png "K9s screenshot")


![K9s screenshot](/data/attachment/album/202005/25/104936lpk81mzhmu8qw1kc.png "K9s screenshot")


草草掠过那些事件并不能告诉我们故障原因。接下来，我按了 `esc` 键，然后通过高亮显示 Pod 并输入`shift-l` 来检查日志。


![K9s screenshot](/data/attachment/album/202005/25/104938uvie7qyvs9qsbvyv.png "K9s screenshot")


不幸的是，日志也没有提供任何有用的信息（可能是因为部署从未正确配置过），而且 Pod 也没有出现。


然后我使用 `esc` 退了出来，我看看删除 Pod 是否能解决这个问题。要做到这一点，我高亮显示该 Pod，然后使用 `ctrl-d`。幸好 `k9s` 在删除前会提示用户。 


![K9s screenshot](/data/attachment/album/202005/25/104941vi0jsc80ckd1los1.png "K9s screenshot")


虽然我确实删除了这个 Pod，但部署资源仍然存在，所以新的 Pod 会重新出现。无论什么原因（我们还不知道），它还会继续重启并死掉。


在这里，我会重复查看日志，描述资源，甚至使用 `e` 快捷方式来编辑运行中的 Pod 以排除故障行为。在这个特殊情况下，失败的 Pod 是因为没有配置在这个环境下运行。因此，让我们删除部署来停止崩溃接着重启的循环。


我们可以通过键入 `:deploy` 并点击回车进入部署。从那里我们高亮显示并按 `ctrl-d` 来删除。


![K9s screenshot](/data/attachment/album/202005/25/104943aj9hnjzn0s111nr0.png "K9s screenshot")


![K9s screenshot](/data/attachment/album/202005/25/104946qg4jntnufntanb21.png "K9s screenshot")


这个有问题的部署被干掉了! 只用了几个按键就把这个失败的部署给清理掉了。


### k9s 是极其可定制的


这个应用有很多自定义选项、乃至于 UI 的配色方案。这里有几个可编辑的选项，你可能会感兴趣。


* 调整你放置 `config.yml` 文件的位置（这样你就可以把它存储在[版本控制](https://opensource.com/article/19/3/move-your-dotfiles-version-control)中）。
* 在 `alias.yml` 文件中添加[自定义别名](https://k9scli.io/topics/aliases/)。
* 在 `hotkey.yml` 文件中创建[自定义热键](https://k9scli.io/topics/hotkeys/)。
* 探索现有的[插件](https://github.com/derailed/k9s/tree/master/plugins)或编写自己的插件。


整个应用是在 YAML 文件中配置的，所以定制化对于任何 Kubernetes 管理员来说都会觉得很熟悉。


### 用 k9s 简化你的生活


我倾向于以一种非常手动的方式来管理我团队的系统，更多的是为了锻炼脑力，而不是别的。当我第一次听说 `k9s` 的时候，我想，“这只是懒惰的 Kubernetes 而已。”于是我否定了它，然后回到了到处进行人工干预的状态。实际上，当我在处理积压工作时就开始每天使用它，而让我震惊的是它比单独使用 `kubectl` 快得多。现在，我已经皈依了。 


了解你的工具并掌握做事情的“硬道理”很重要。还有一点很重要的是要记住，就管理而言，重要的是要更聪明地工作，而不是更努力。使用 `k9s`，就是我践行这个目标的方法。我想，我们可以把它叫做懒惰的 Kubernetes 管理，也没关系。




---


via: <https://opensource.com/article/20/5/kubernetes-administration>


作者：[Jessica Cherry](https://opensource.com/users/cherrybomb) 选题：[lujun9972](https://github.com/lujun9972) 译者：[wxy](https://github.com/wxy) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
Usually, my articles about Kubernetes administration are full of kubectl commands for administration for your clusters. Recently, however, someone pointed me to the [k9s](https://github.com/derailed/k9s) project for a fast way to review and resolve day-to-day issues in Kubernetes. It's been a huge improvement to my workflow and I'll show you how to get started in this tutorial.

Installation can be done on a Mac, in Windows, and Linux. Instructions for each operating system can be found [here](https://github.com/derailed/k9s). Be sure to complete installation to be able to follow along.

I will be using Linux and Minikube, which is a lightweight way to run Kubernetes on a personal computer. Install it following [this tutorial](https://opensource.com/article/18/10/getting-started-minikube) or by using the [documentation](https://kubernetes.io/docs/tasks/tools/install-minikube/).

## Setting the k9s configuration file

Once you've installed the k9s app, it's always good to start with the help command.

```
$ k9s help
K9s is a CLI to view and manage your Kubernetes clusters.
Usage:
k9s [flags]
k9s [command]
Available Commands:
help Help about any command
info Print configuration info
version Print version/build info
Flags:
-A, --all-namespaces Launch K9s in all namespaces
--as string Username to impersonate for the operation
--as-group stringArray Group to impersonate for the operation
--certificate-authority string Path to a cert file for the certificate authority
--client-certificate string Path to a client certificate file for TLS
--client-key string Path to a client key file for TLS
--cluster string The name of the kubeconfig cluster to use
-c, --command string Specify the default command to view when the application launches
--context string The name of the kubeconfig context to use
--demo Enable demo mode to show keyboard commands
--headless Turn K9s header off
-h, --help help for k9s
--insecure-skip-tls-verify If true, the server's caCertFile will not be checked for validity
--kubeconfig string Path to the kubeconfig file to use for CLI requests
-l, --logLevel string Specify a log level (info, warn, debug, error, fatal, panic, trace) (default "info")
-n, --namespace string If present, the namespace scope for this CLI request
--readonly Disable all commands that modify the cluster
-r, --refresh int Specify the default refresh rate as an integer (sec) (default 2)
--request-timeout string The length of time to wait before giving up on a single server request
--token string Bearer token for authentication to the API server
--user string The name of the kubeconfig user to use
Use "k9s [command] --help" for more information about a command.
```


As you can see, there is a lot of functionality we can configure with k9s. The only step we need to take place to get off the ground is to write a configuration file. The **info **command will point us to where the application is looking for it.

```
$ k9s info
____ __.________
| |/ _/ __ \______
| < \____ / ___/
| | \ / /\___ \
|____|__ \ /____//____ >
\/ \/
Configuration: /Users/jess/.k9s/config.yml
Logs: /var/folders/5l/c1y1gcw97szdywgf9rk1100m0000gn/T/k9s-jess.log
Screen Dumps: /var/folders/5l/c1y1gcw97szdywgf9rk1100m0000gn/T/k9s-screens-jess
```


To add a file, make the directory if it doesn't already exist and then add one.

```
$ mkdir -p ~/.k9s/
$ touch ~/.k9s/config.yml
```


For this introduction, we will use the default config.yml recommendations from the k9s repository. The maintainers note that this format is subject to change, so we can [check here](https://github.com/derailed/k9s#k9s-configuration) for the latest version.

```
k9s:
refreshRate: 2
headless: false
readOnly: false
noIcons: false
logger:
tail: 200
buffer: 500
sinceSeconds: 300
fullScreenLogs: false
textWrap: false
showTime: false
currentContext: minikube
currentCluster: minikube
clusters:
minikube:
namespace:
active: ""
favorites:
- all
- kube-system
- default
view:
active: dp
thresholds:
cpu:
critical: 90
warn: 70
memory:
critical: 90
warn: 70
```


We set k9s to look for a local minikube configuration, so I'm going to confirm minikube is online and ready to go.

```
$ minikube status
host: Running
kubelet: Running
apiserver: Running
kubeconfig: Configured
```


## Running k9s to explore a Kubernetes cluster

## With a configuration file set and pointing at our local cluster, we can now run the **k9s** command.

`$ k9s`


Once you start it up, the k9s text-based user interface (UI) will pop up. With no flag for a namespace, it will show you the pods in the default namespace.

![K9s screenshot K9s screenshot](https://opensource.com/sites/default/files/uploads/k9s_1.png)

If you run in an environment with a lot of pods, the default view can be overwhelming. Alternatively, we can focus on a given namespace. Exit the application and run **k9s -n <namespace>** where *<namespace>* is an existing namespace. In the picture below, I ran **k9s -n minecraft,** and it shows my broken pod

![K9s screenshot K9s screenshot](https://opensource.com/sites/default/files/uploads/k9s_2.png)

So once you have k9s up and running, there are a bunch of things you can do quickly.

Navigating k9s happens through shortcut keys. We can always use arrow keys and the enter key to choose items listed. There are quite a few other universal keystrokes to navigate to different views:

**0**—Show all pods in all namespaces

![K9s screenshot K9s screenshot](https://opensource.com/sites/default/files/uploads/k9s_3.png)

**d**—Describe the selected pod

![K9s screenshot K9s screenshot](https://opensource.com/sites/default/files/uploads/k9s_5_0.png)

**l**—Show logs for the selected pod pod

![Using k9s to show Kubernetes pod logs Using k9s to show Kubernetes pod logs](https://opensource.com/sites/default/files/uploads/k9s-show-logs-opensourcedotcom.png)

You may notice that k9s is set to use [Vim command keys](https://opensource.com/article/19/3/getting-started-vim), including moving up and down using **J** and **K** keys. Good luck exiting, emacs users :)

## Viewing different Kubernetes resources quickly

Need to get to something that's not a pod? Yea I do too. There are a number of shortcuts that are available when we enter a colon (":") key. From there, you can use the following commands to navigate around there.

**:svc**—Jump to a services view.

![K9s screenshot K9s screenshot](https://opensource.com/sites/default/files/uploads/k9s_5.png)

**:deploy**—Jump to a deployment view.

![K9s screenshot K9s screenshot](https://opensource.com/sites/default/files/uploads/k9s_6.png)

**:rb**—Jump to a Rolebindings view for[role-based access control (RBAC)](https://kubernetes.io/docs/reference/access-authn-authz/rbac/)management.

![K9s screenshot K9s screenshot](https://opensource.com/sites/default/files/uploads/k9s_7.png)

**:namespace**—Jump back to the namespaces view.

![K9s screenshot K9s screenshot](https://opensource.com/sites/default/files/uploads/k9s_8.png)

**:cj**—Jump to the cronjobs view to see what jobs are scheduled in the cluster.

![K9s screenshot K9s screenshot](https://opensource.com/sites/default/files/uploads/k9s_9.png)

The most used tool for this application will be the keyboard; to go up or down on any page, use the arrow keys. If you need to quit, remember to use Vim keybindings. Type **:q** and hit enter to leave.

## Example of troubleshooting Kubernetes with k9s

How does k9s help when something goes wrong? To walk through an example, I let several pods die due to misconfiguration. Below you can see my terrible hello deployment that's crashing. Once we highlight it, we press **d** to run a *describe* command to see what is causing the failure.

![K9s screenshot K9s screenshot](https://opensource.com/sites/default/files/uploads/k9s_10.png)

![K9s screenshot K9s screenshot](https://opensource.com/sites/default/files/uploads/k9s_11.png)

Skimming the events does not tell us a reason for the failure. Next, I hit the **esc** key and go check the logs by highlighting the pod and entering **<shift-l>**.

![K9s screenshot K9s screenshot](https://opensource.com/sites/default/files/uploads/k9s_12.png)

Unfortunately, the logs don't offer anything helpful either (probably because the deployment was never correctly configured), and the pod will not come up.

I then **esc** to step back out, and I will see if deleting the pod will take care of this issue. To do so, I highlight the pod and use **<ctrl-d>**. Thankfully, k9s prompts users before deletion.

![K9s screenshot K9s screenshot](https://opensource.com/sites/default/files/uploads/k9s_13.png)

While I did delete the pod, the deployment resource still exists, so a new pod will come back up. It will also continue to restart and crash for whatever reason (we don't know yet).

Here is the point where I would repeat reviewing logs, describing resources, and use the **e** shortcut to even edit a running pod to troubleshoot the behavior. In this particular case, the failing pod is not configured to run in this environment. So let's delete the deployment to stop crash-then-reboot loop we are in.

We can get to deployments by typing **:deploy **and clicking enter. From there we highlight and press **<ctrl-d> **to delete.

![K9s screenshot K9s screenshot](https://opensource.com/sites/default/files/uploads/k9s_14.png)

![K9s screenshot K9s screenshot](https://opensource.com/sites/default/files/uploads/k9s_15.png)

And poof the deployment is gone! It only took a few keystrokes to clean up this failed deployment.

## k9s is incredibly customizable

So this application has a ton of customization options, down to the color scheme of the UI. Here are a few editable options you may be interested in:

- Adjust where you put the config.yml file (so you can store it in
[version control](https://opensource.com/article/19/3/move-your-dotfiles-version-control)) - Add
[custom aliases](https://k9scli.io/topics/aliases/)to an**alias.yml**file - Create
[custom hotkeys](https://k9scli.io/topics/hotkeys/)in a**hotkey.yml**file - Explore available
[plugins](https://github.com/derailed/k9s/tree/master/plugins)or write your own

The entire application is configured in YAML files, so customization will feel familiar to any Kubernetes administrator.

## Simplify your life with k9s

I'm prone to administrating over my team's systems in a very manual way, more for brain training than anything else. When I first heard about k9s, I thought, "This is just lazy Kubernetes," so I dismissed it and went back to doing my manual intervention everywhere. I actually started using it daily while working through my backlog, and I was blown away at how much faster it was to use than kubectl alone. Now I'm a convert.

It's important to know your tools and master the "hard way" of doing something. It is also important to remember, as far as administration goes, it's important to work smarter, not harder. Using k9s is the way I live up to that objective. I guess we can call it lazy Kubernetes administration, and that's okay.

## 1 Comment