概述
k9s,GitHub,是用于管理k8s集群的CLI,提供一个终端UI来与k8s集群进行交互。通过封装kubectl功能,k9s会以特定时间间隔监控k8s的变化,默认为2秒,并提供后续命令来与k8s资源进行交互,k9s可让开发者快速查看并解决运行k8s时的日常问题。k9s操作逻辑基于Vim。
功能特性:
- 跟踪在k8s集群运行的资源的实时活动
- 处理k8s标准资源和自定义资源
- 跟踪与资源相关联的实时指标,如pods、容器和节点
- 支持定制外观、皮肤
- 支持多资源视图
- 支持查看RBAC规则
- 支持遍历k8s资源及其相关资源
- 支持插件扩展k9s来创建个性化的集群操作管理命令
- 强大的过滤模式,允许用户向下钻取并查看与工作负载相关的资源
安装
k9s采用Go语言开发,支持Linux,macOS和Windows平台。
Mac:brew install derailed/k9s/k9s
Linux:pacman -S k9s
Build:
git clone https://github.com/derailed/k9s.git
make build && ./execs/k9s
Docker:
# 指定k8s的配置文件路径
$ docker run --rm -it -v $KUBECONFIG:/root/.kube/config derailed/k9s
# k8s配置文件的默认路径
$ docker run --rm -it -v ~/.kube/config:/root/.kube/config derailed/k9s
和kubectl一样,默认使用环境变量KUBECONFIG指定的k9s配置,支持指定:kubeconfig k9s --kubeconfig=xx
。
命令行
命令行,按照个人理解,可分为两类,一类是不进入交互式终端UI的,比如:
k9s help
k9s info
k9s info logs
k9s info configs
k9s info
输出:
Version: v0.32.5
Config: /root/.config/k9s/config.yaml
Custom Views: /root/.config/k9s/views.yaml
Plugins: /root/.config/k9s/plugins.yaml
Hotkeys: /root/.config/k9s/hotkeys.yaml
Aliases: /root/.config/k9s/aliases.yaml
Skins: /root/.config/k9s/skins
Context Configs: /root/.local/share/k9s/clusters
Logs: /root/.local/state/k9s/k9s.log
Benchmarks: /root/.local/state/k9s/benchmarks
ScreenDumps: /root/.local/state/k9s/screen-dumps
另一类是进入交互式终端的,用得较少,最后还是会变成终端UI方式:
k9s -n observe
# 以只读模式启动k9s,禁用所有修改命令
k9s --readonly
# 进入k9s的调试模式
k9s -l debug
基本使用
输入k9s命令,进入终端UI:
快捷键:
- enter:进入,执行等多种含义和用途;
- esc:退出;
:ctx
:选择k8s集群,即上面截图左上角最上面的Context,点击enter键进入到Pods页;:pod
:类似于k8s命令行下pod等同于pods,输入:pods
也能进入Pods页,其他资源类似,不再赘述;:deploy
:进入Deployments终端页;k9s内置命令补全,输入:de
后,按Tab即可自动补全,其他资源类似;
:svc
:进入Services终端页;:statefulset
:进入Statefulsets终端页;y
:选中某个Pod、Deployment、Service、DaemonSet后,y
可查看该资源的YAML描述文件;d
:同上,选择某个资源后,d
可查看该资源的详细信息,类似于kubectl describe
命令;e
:进入Vim来修改资源的Yaml定义,退出命令则是Vim的:q
或:wq
等;几乎所有资源类型都能进行编辑。如果修改后的语法不正确,k9s会提示修改失败,修改不会生效;l
:即log,在任何资源页都可使用该命令,顶多是没有日志输出。在查看容器日志时,按0代表查看日志的尾部,即最新日志,按w代表日志自动换行。
s
:在Deployments页面,s
表示Scale,触发修改副本数操作(即Replicas):
在Pods页面,s
表示Shell,会进入到容器的Shell命令行,输入exit
或ctrl-d
退出,注意ctrl-c
并不能退出容器;如果一个Pod包含多个Container,则会进入Container界面让你选择要进入的Container:
- 数字:如上截图所示,在任何一个终端页,输入数字则仅展示该数字对应的命名空间下的资源;
:ns
:切换命名空间;/filter
:如果指定命名空间后,Pod还是太多,可类似于Vim编辑器里输入/
进行过滤,可过滤命名空间、资源名称和状态;/!filter
:逆过滤器,去除所有匹配的资源。不支持日志过滤;/-f filter
:模糊匹配过滤;j
:等同于方向键↓
,向下移到被选择的资源;k
:等同于方向键↑
,向上移到被选择的资源;p
:查看之前的Container的日志,这个功能貌似没啥用,Container重启后,日志丢失,看不到日志;
ctrl-d
:删除某个资源,有弹窗二次确认,选择OK后执行删除,触发自动重启;
ctrl-k
:删除某个Pod,没有二次弹窗确认,自动触发重启;:rb
:进入Rolebindings,角色绑定视图,用于基于角色的访问控制(RBAC)管理;:cj
:跳转到Cronjobs视图,查看集群中的计划作业;:q
:退出k9s;ctrl-c
:退出k9s;
总结:
- 充分利用Tab补全;
PortForward
别名
编辑/root/.config/k9s/aliases.yaml
文件:
aliases:
dp: deployments
sec: v1/secrets
jo: jobs
cr: clusterroles
crb: clusterrolebindings
ro: roles
rb: rolebindings
np: networkpolicies
热键
允许用户定义自己的热键来激活他们最喜欢的资源视图。编辑/root/.config/k9s/hotkeys.yaml
文件:
hotKey:
# Hitting Shift-0 navigates to your pod view
shift-0:
shortCut: Shift-0
description: Viewing pods
command: pods
# Hitting Shift-1 navigates to your deployments
shift-1:
shortCut: Shift-1
description: View deployments
command: dp
进阶
配置文件
编辑/root/.config/k9s/config.yaml
文件:
k9s:
liveViewAutoRefresh: false
screenDumpDir: /root/.local/state/k9s/screen-dumps
refreshRate: 2
# Set to true to hide K9s header. Default false
headless: false
# Toggles icons display as not all terminal support these chars.
noIcons: false
maxConnRetry: 5
readOnly: false
noExitOnCtrlC: false
ui:
enableMouse: false
headless: false
logoless: false
crumbsless: false
reactive: false
noIcons: false
defaultsToFullScreen: false
skipLatestRevCheck: false
disablePodCounting: false
shellPod:
image: busybox:1.35.0
namespace: default
limits:
cpu: 100m
memory: 100Mi
imageScans:
enable: false
exclusions:
namespaces: []
labels: {}
logger:
tail: 100
buffer: 5000
sinceSeconds: -1
textWrap: false
# Go full screen while displaying logs. Default false
fullScreenLogs: false
showTime: false
thresholds:
cpu:
critical: 90
warn: 70
memory:
critical: 90
warn: 70
皮肤
可根据自己的外观和风格来配置k9s,放在/root/.config/k9s/skins
目录下:
# Skin InTheNavy...
k9s:
# General K9s styles
body:
fgColor: dodgerblue
bgColor: '#ffffff'
logoColor: '#0000ff'
# ClusterInfoView styles.
info:
fgColor: lightskyblue
sectionColor: steelblue
# Help panel styles
help:
fgColor: white
bgColor: black
keyColor: cyan
numKeyColor: blue
sectionColor: gray
frame:
# Borders styles.
border:
fgColor: dodgerblue
focusColor: aliceblue
# MenuView attributes and styles.
menu:
fgColor: darkblue
keyColor: cornflowerblue
# Used for favorite namespaces
numKeyColor: cadetblue
# CrumbView attributes for history navigation.
crumbs:
fgColor: white
bgColor: steelblue
activeColor: skyblue
# Resource status and update styles
status:
newColor: '#00ff00'
modifyColor: powderblue
addColor: lightskyblue
errorColor: indianred
highlightcolor: royalblue
killColor: slategray
completedColor: gray
# Border title styles.
title:
fgColor: aqua
bgColor: white
highlightColor: skyblue
counterColor: slateblue
filterColor: slategray
views:
# TableView attributes.
table:
fgColor: blue
bgColor: darkblue
cursorColor: aqua
# Header row styles.
header:
fgColor: white
bgColor: darkblue
sorterColor: orange
# YAML info styles.
yaml:
keyColor: steelblue
colonColor: blue
valueColor: royalblue
# Logs styles.
logs:
fgColor: white
bgColor: black
插件
k9s允许您通过插件定义集群命令来扩展您的命令行和工具。
# k9s目前提供额外的环境变量来自定义您的插件参数,可用的环境变量如下:
$RESOURCE_GROUP-- 选定的资源组
$RESOURCE_VERSION-- 选择的资源api版本
$RESOURCE_NAME-- 选择的资源名称
$NAMESPACE-- 选择的资源命名空间
$NAME-- 选择的资源名称
$CONTAINER-- 当前容器(如果适用)
$FILTER-- 当前过滤器(如果有)
$KUBECONFIG-- KubeConfig 位置。
$CLUSTER -- 活动集群名称
$CONTEXT -- 活动上下文名称
$USER -- 活跃用户
$GROUPS -- 活跃的群体
$POD -- 在容器视图中
$COL-<RESOURCE_COLUMN_NAME> -- 为查看的资源使用给定的列名。 必须以 COL-!
# 例如, 此处定义一个插件实现 `ctrl-l` 快捷方式来在 pod 视图中跟踪日志。
# /root/.config/k9s/views.yaml
plugin:
fred:
shortCut: Ctrl-L
confirm: false
description: Pod logs
scopes:
- pods
command: kubectl
background: false
args:
- logs
- -f
- $NAME
- -n
- $NAMESPACE
- --context
- $CONTEXT
视图
编辑/root/.config/k9s/views.yaml
文件,自定义资源的显示列和先后顺序:
k9s:
views:
v1/pods:
columns:
- AGE
- NAMESPACE
- NAME
- IP
- NODE
- STATUS
- READY
v1/services:
columns:
- AGE
- NAMESPACE
- NAME
- TYPE
- CLUSTER-IP
nodeShell
通过在给定集群上启用nodeShell功能门,k9s允许您在集群节点中进行shell:
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: demo-demo:01
namespace: test-namespace
limits:
cpu: 100m
memory: 100Mi
XRay
Pulses
集群事务状态的顶级仪表板。
RBAC
查看您的集群上的授权的用户、方式等。