使用k9s簡化Kubernetes集群的管理!
譯文【51CTO.com快譯】我寫的Kubernetes管理文章通常提到管理集群的kubectl命令。不過最近有人指出k9s項目可以快速地查看和解決Kubernetes中的日常問題。本文介紹了如何入門。
可以在Mac、Windows和Linux上進行安裝??梢栽?a >此處針對每款操作系統(tǒng)的說明。完成安裝后繼續(xù)下一步。
我將使用Linux和Minikube,后者是在個人計算機上運行Kubernetes的輕量級方法。按照本教程用說明文檔裝它。
設置k9s配置文件
一旦安裝了k9s應用程序,最好從help命令入手。
- $ 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.
如你所見,我們可以用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
- 要添加文件,創(chuàng)建目錄(如果之前沒有),然后添加一個。
- $ mkdir -p ~/.k9s/
- $ touch ~/.k9s/config.yml
要添加文件,創(chuàng)建目錄(如果之前沒有),然后添加一個。
- $ 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已聯(lián)機、隨時可用。
- $ minikube status
- host: Running
- kubelet: Running
- apiserver: Running
- kubeconfig: Configured
運行k9s以探究Kubernetes集群
由于配置文件已設定、指向本地集群,我們現(xiàn)在可以運行k9s命令。
- $ k9s
一旦你啟動了k9s,k9s文本型用戶界面(UI)會彈出。沒有命名空間的flag,它會顯示默認命名空間中的pod。
如果你在有許多pod的環(huán)境中運行,默認視圖可能讓人不知所措。另外,我們可以專注于特定的命名空間。退出該應用程序,運行k9s -n ,其中是現(xiàn)有的命名空間。在下圖中,我運行k9s -n minecraft,它顯示我那個出故障的pod。
所以一旦k9s啟動起來,你可以迅速處理諸多事務。
可通過快捷鍵來瀏覽操控k9s。我們總是可以使用箭頭鍵和回車鍵選擇所列的項。有幾個另外的通用鍵來查看不同的視圖:
0—顯示所有命名空間中的所有pod。
d—描述所選擇的pod。
l—顯示所選擇的pod的日志。
你可能注意到k9s已設置為使用Vim命令鍵,包括使用J鍵和K鍵上下移動。
快速查看不同的Kubernetes資源
是否需要查看不是pod的資源?輸入冒號(“:”)鍵后有許多快捷方式可用。然后,你可以使用以下命令來瀏覽。
:svc——跳轉(zhuǎn)到服務視圖。
:deploy——跳轉(zhuǎn)到部署視圖。
:rb——跳轉(zhuǎn)到角色綁定視圖,面向基于角色的訪問控制(RBAC)管理。
:namespace——跳回到命名空間視圖。
:cj——跳轉(zhuǎn)到計劃任務視圖,查看集群中的計劃任務。
對該應用程序而言最常用的工具是鍵盤;想在任何頁面上向上或向下,使用箭頭鍵。如果需要退出,記得使用Vim鍵綁定。輸入:q,按回車鍵離開。
使用k9s對Kubernetes排除故障的示例
出現(xiàn)問題時,k9s有何幫助?為了舉例說明,我讓幾個pod因配置錯誤而失效。下面你可以看到崩潰的Hello部署。一旦高亮顯示,按d運行describe命令,查看導致故障的原因。
略讀事件并不能告訴我們故障的原因。接下來,我按esc鍵,通過高亮顯示pod并輸入來查看日志。
遺憾的是,日志也沒有提供任何幫助(可能是由于從未正確配置部署),pod不會啟動。
然后我按esc退出,查看刪除pod會不會解決此問題。為此,我高亮顯示pod并使用。k9s在刪除之前提示用戶。
雖然我確實刪除了pod,但部署資源仍存在,因此新的pod會出現(xiàn)。無論出于何種原因(我們尚不知道),它也會繼續(xù)重啟、崩潰。
我將重復查看日志、描述資源,并使用e快捷方式來編輯運行中的pod來解決問題。在這種特定情況下,出故障的pod未配置成在該環(huán)境下運行。因此,不妨刪除部署以停止我們遇到的崩潰后重新啟動循環(huán)。
我們可以通過輸入:deploy并點擊回車鍵來進入部署。之后,我們高亮顯示,并按刪除。
這證明部署不見了!僅需敲幾下鍵即可清理這個出故障的部署。
k9s可靈活定制
因此,該應用程序有大量定制選項,連UI的配色方案都可以定制。以下是你可能感興趣的幾個可編輯選項:
- 調(diào)整放置config.yml文件的位置
- 將自定義別名添加到alias.yml文件
- 在hotkey.yml文件中創(chuàng)建自定義熱鍵
- 探究可用的插件或自行編寫插件
整個應用程序在YAML文件中加以配置,因此定制對任何Kubernetes管理員來說會很熟悉。
原文標題:Speed up administration of Kubernetes clusters with k9s,作者:Jessica Cherry
【51CTO譯稿,合作站點轉(zhuǎn)載請注明原文譯者和出處為51CTO.com】