當集群數量比較多的時候,管理起來比較麻煩。如果是使用云廠商的集群,可以在控制臺界面進行管理,但如果是自己搭建的集群,想要在命令行界面管理的話,可以使用kubecm和k9s工具。
kubecm
kubecm可以用來將所有kubeconfig匯集到一起,并方便地進行上下文切換。
安裝
可以參考release下載二進制安裝。
常用命令
kubecm add --context-name <name> -f <your-kubeconfig>
將kubeconfig添加到~/.kube/config
文件中。-
kubecm list
查看所有的kubeconfig,效果如下。
image.png kubecm switch <context-name>
切換到指定的context。kubecm delete <context-name>
刪除指定的context。kubecm merge config1.yam config2.yaml
合并多個kubeconfig到~/.kube/config
中。
其他命令參考:https://kubecm.cloud/en-us/README
k9s
安裝
- 可以選擇命令行安裝,也可以直接從release下載編譯好的二進制。
- 配置文件:執行
k9s info
,可以看到配置文件路徑,通常位于以下路徑。
image.png - 編輯配置文件(如果配置文件不存在或內容為空),參考。
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
用法
直接命令行輸入k9s
,會自動用~/.kube/config
中當前context的集群。
:
用于切換資源。如輸入:namespace
就會切換到namespace視圖,輸入:deploy
會進入deploy視圖,輸入:context
可以切換到不同的集群。
/
用于查找資源。如namespace視圖輸入/namespace1
用于查找名為namespace1的命名空間,其他資源同理。
esc
返回上一個視圖
:q!
退出k9s。
j/k
上下移動。
空格
選擇該資源。
enter
進入該資源。如在命名空間界面按空格選擇一個命名空間,然后按enter查看該命名空間下的pod。
tab
自動補全。非常強大,使用起來很方便。
其他命令用法可以參考界面提示。如l
可以查看pod的日志,d
可以describe資源。