完整思维导图请参考:“K8s 工具思维导图”
什么是 K9s?
K9s (https://k9scli.io/) 是一个终端 UI 工具,用于与你的 K8s 集群进行交互。K9s 的目的是使你更轻松地浏览、观察和管理你的应用程序。K9s 会不断地监视你的 K8s 集群中的更改,并提供相应的命令来与你观察的资源进行交互。
K9s 有以下优点:
- 提供标准的集群管理命令,如“日志记录”、“扩展”、“端口转发”和“重启”。
- 允许你定义自己的命令快捷方式,以通过命令别名和快捷键进行快速导航。
- 提供了一个强大的过滤模式,允许用户深入查看与工作负载相关的资源。
- 支持查看 RBAC 规则,如“集群/角色”及其相关的绑定。反向查找断言用户/组或 ServiceAccount 在集群上可以执行什么操作。
- 实时跟踪在你的 Kubernetes 集群中运行的资源活动。
以下是列出集群中所有 Pod 的示例:
图片来源:k9s
如何安装 K9s
K9s 可在 Linux、macOS 和 Windows 平台上使用。
二进制安装
对于二进制安装(作为 tarballs),你可以从以下 URL 下载软件包(https://github.com/derailed/k9s/releases):
这里我将演示 v0.26.7
的 Linux 安装。安装步骤如下:
下载源代码:
$ wget https://github.com/derailed/k9s/releases/download/v0.25.18/k9s_Linux_x86_64.tar.gz
--2023-01-16 15:18:52-- https://github.com/derailed/k9s/releases/download/v0.25.18/k9s_Linux_x86_64.tar.gz
Resolving github.com (github.com)... 140.82.113.4
...
2023-01-16 15:13:39 (89.9 MB/s) - ‘k9s_Linux_x86_64.tar.gz’ saved [17653727/17653727]
解压并安装:
$ tar -xzvf k9s_Linux_x86_64.tar.gz
LICENSE
README.md
k9s
启动它:
$ k9s
截图如下:
点击“Enter”并连接到你的集群:
其他安装方法
Homebrew
$ brew install k9s
Arch Linux
$ pacman -S k9s
OpenSUSE
$ zypper install k9s
Golang
$ go install github.com/derailed/k9s@latest
更多安装方法和命令请访问:https://github.com/derailed/k9s
K9s 命令行
以下是一些常用的命令行参数:
# List all available CLI options
$ k9s help
K9s is a CLI to view and manage your Kubernetes clusters.
Usage:
k9s [flags]
k9s [command]
Available Commands:
completion generate the autocompletion script for the specified shell
help Help about any command
info Print configuration info
version Print version/build info
....
# To get info about K9s runtime (logs, configs, etc..)
$ k9s info
| |/ _/ __ \______
| < \____ / ___/
| | \ / /\___ \
|____|__ \ /____//____ >
\/ \/
Configuration: /mnt/efs/data/home/txu/.config/k9s/config.yml
Logs: /tmp/k9s-txu.log
Screen Dumps: /tmp/k9s-screens-txu
# To run K9s in a given namespace
$ k9s -n mycoolns
# Start K9s in an existing KubeConfig context
$ k9s --context coolCtx
# Start K9s in readonly mode - with all cluster modification commands disabled
$ k9s --readonly
K9s 将其日志保留到特定位置。从 k9s info
命令输出中,你可以检查日志文件位置。对于进一步的故障排除,你也可以查看 k9s 日志:
$ less /tmp/k9s-txu.log
配置 K9s
K9s 将其配置保存在 k9s
目录中,其位置取决于你的操作系统。K9s 利用 XDG 加载其各种配置文件。你可以使用 k9s info
命令来检查配置文件的位置。
# $XDG_CONFIG_HOME/k9s/config.yml
k9s:
# Represents ui poll intervals. Default 2secs
refreshRate: 2
# Number of retries once the connection to the api-server is lost. Default 15.
maxConnRetry: 5
# Enable mouse support. Default false
enableMouse: true
# Set to true to hide K9s header. Default false
headless: false
# Set to true to hide K9s crumbs. Default false
crumbsless: false
# Indicates whether modification commands like delete/kill/edit are disabled. Default is false
readOnly: false
# Toggles whether k9s should exit when CTRL-C is pressed. When set to true, you will need to exist k9s via the :quit command. Default is false.
noExitOnCtrlC: false
# Toggles icons display as not all terminal support these chars.
noIcons: false
# Toggles whether k9s should check for the latest revision from the Github repository releases. Default is false.
skipLatestRevCheck: false
# Logs configuration
logger:
# Defines the number of lines to return. Default 100
tail: 200
# Defines the total number of log lines to allow in the view. Default 1000
buffer: 500
# Represents how far to go back in the log timeline in seconds. Setting to -1 will show all available logs. Default is 5min.
sinceSeconds: 300
# Go full screen while displaying logs. Default false
fullScreenLogs: false
# Toggles log line wrap. Default false
textWrap: false
# Toggles log line timestamp info. Default false
showTime: false
# Indicates the current kube context. Defaults to current context
currentContext: minikube
# Indicates the current kube cluster. Defaults to current context cluster
currentCluster: minikube
# Persists per cluster preferences for favorite namespaces and view.
clusters:
coolio:
namespace:
active: coolio
# With this set, the favorites list won't be updated as you switch namespaces
lockFavorites: false
favorites:
- cassandra
- default
view:
active: po
featureGates:
# Toggles NodeShell support. Allow K9s to shell into nodes if needed. Default false.
nodeShell: false
# Provide shell pod customization of feature gate is enabled
shellPod:
# The shell pod image to use.
image: killerAdmin
# The namespace to launch to shell pod into.
namespace: fred
# The resource limit to set on the shell pod.
limits:
cpu: 100m
memory: 100Mi
# The IP Address to use when launching a port-forward.
portForwardAddress: 1.2.3.4
kind:
namespace:
active: all
favorites:
- all
- kube-system
- default
view:
active: dp
# The path to screen dump. Default: '%temp_dir%/k9s-screens-%username%' (k9s info)
screenDumpDir: /tmp
你也可以定义命令别名来访问你的资源。要定义别名,请在 $HOME/.config/k9s
中创建一个名为 alias.yml
的文件。以下是别名文件的示例:
# $XDG_CONFIG_HOME/k9s/alias.yml
alias:
pp: v1/pods
crb: rbac.authorization.k8s.io/v1/clusterrolebindings
使用此别名文件,你现在可以键入 pp/crb
来分别列出 Pods 或 ClusterRoleBindings。
K9s 节点 Shell 功能
K9s 允许你进入集群节点的 Shell。你可以通过将 nodeShell
设置为 true
来启用此功能,例如:
# $XDG_CONFIG_HOME/k9s/config.yml
k9s:
clusters:
# Configures node shell on cluster blee
blee:
featureGates:
# You must enable the nodeShell feature gate to enable shelling into nodes
nodeShell: true
# You can also further tune the shell pod specification
shellPod:
image: cool_kid_admin:42
namespace: blee
limits:
cpu: 100m
memory: 100Mi
一旦你启用了 shellPod
,你就会在节点视图中有一个新的 s
用于 shell
菜单选项。例如:
现在,如果你想进入 Nginx pod 的 shell,你只需键入 s
键:
结论
K9s 还有许多其他不错的功能,例如支持快捷键、快速转发、插件、对你的应用程序进行基准测试等等。在一篇文章中无法涵盖所有内容,请访问 https://k9scli.io/ 了解更多。
译自:https://tonylixu.medium.com/devops-in-k8s-k9s-terminal-based-ui-to-manage-your-cluster-85b4f147e209
评论(0)