一般狀況下,咱們自行部署 k8s 集羣以後,要麼使用 kubectl 命令作集羣管理,要麼使用 bashbroad 的 UI 管理界面來管理集羣。最近,發現了一個基於終端且比較好用的項目,可讓咱們快速查看、導航、觀察並解決平常咱們使用 Kubernetes 中的一些問題,這就是 k9s 項目(以前的文章中有過相關的一些介紹:輕鬆管理 Kubernetes 集羣的7個工具)。html
平常使用終端的你,可謂是一件利器!node
k9s 是一個基於 curses 的全屏終端 UI 管理工具,能夠與 Kubernetes 集羣進行交互,能夠觀察系統資源,在各類資源之間切換,檢查清單、日誌、監控事件並執行 Pod 等,從而確保桌面空間不至於被大量終端窗格所佔據。git
k9s 會以特定時間間隔監控 Kubernetes 資源,默認爲 2 秒,並容許查看本身集羣中的內容。它能夠一目瞭然地提供了運行中 Pod、日誌和部署的可視化視圖,以及對 Shell 的快速訪問。如下是該工具的主要特性:github
Pulses-集羣事務狀態的頂級儀表板docker
XRay-挖掘羣集資源並查看其依賴性shell
Pods -列出你的 Pods 狀態和資源消耗vim
Logs-查看容器日誌並與之交互bash
RBAC-查看您的集羣上的受權的用戶、方式等less
安裝起來雖然比較簡單,可是有點不怎麼給力!運維
K9s 採用 Go 語言開發,可運行於 Linux,macOS 和 Windows 平臺。K9s 安裝很是的簡單,官方已經爲咱們提供了各平臺的二進制安裝包,只需下載對應版本便可。
MacOS
# Homebrew $ brew install derailed/k9s/k9s
Linux
# LinuxBrew $ brew install derailed/k9s/k9s # PacMan $ pacman -S k9s
Build
# Clone the repo $ git clone https://github.com/derailed/k9s.git # Build and run the executable $ 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
k9s 和 dashboard 能夠理解爲 vim 和 idea 之間的關係!
K9s 自帶有一些參數,可使用該參數以不一樣的配置啓動該工具,好比你須要看 Pod 相關的信息。
命令行啓動參數
# 列出全部可用的CLI選項 $ k9s help # 獲取有關K9s運行時的信息 $ k9s info $ k9s info logs $ k9s info configs # 在給定的名稱空間中運行 $ k9s -n mycoolns # 運行K9s在pod視圖中啓動 $ k9s -c pod # 以只讀模式啓動K9s;該模式禁用全部修改命令 $ k9s --readonly
操做快捷鍵
主要演示一些經常使用的命令參數的效果!
啓動後,會彈出 k9s 的基於文本的用戶界面。在沒有指定命名空間標誌的狀況下,它會向你顯示默認命名空間中的 Pod。經過快捷鍵來導航 k9s,能夠隨時使用方向鍵和回車鍵來選擇列出的項目。可能會注意到 k9s 設置爲使用 Vim 命令鍵,包括使用 J 和 K 鍵上下移動等。
0:#顯示在全部命名空間中的全部 Pod d:#描述所選的 Pod l:#顯示所選的 Pod 的日誌 y:#查看 YAML 文件 e:#編輯 YAML 文件 d:#查看鍵映射描述 :svc:#跳轉到服務視圖 :deploy:#跳轉到部署視圖 :namespace:#跳轉到命名空間視圖 :cj:#跳轉到 cronjob 視圖,查看集羣中計劃了哪些做業 :rb:#跳轉到角色綁定視圖,用於基於角色的訪問控制(RBAC)管理
運行該命令,會告訴咱們該應用程序要在哪裏找它的配置文件。
$ k9s info ____ __.________ | |/ _/ __ ______ | < ____ / ___/ | | / /___ |____|__ /____//____ > / / Configuration: /Users/jess/.k9s/config.yml Logs: /var/folders/5l/xxx/T/k9s-jess.log Screen Dumps: /var/folders/5l/xxx/T/k9s-screens-jess # 查看k9s的日誌 $ tail -f /var/folders/5l/xxx/T/k9s-jess.log # 進入k9s的調試模式 $ k9s -l debug
經過指定 namespace 命名空間,咱們能夠將注意力集中在給定的命名空間上。
配置文件中主要定義一些使用上面的默認方式
K9s 將其配置保存在主目錄 $HOME/.k9s 中的 .k9s 目錄中。主要配置文件名爲config.yml 並存儲各類 K9s 特定位。k9s 將更新此文件以存儲當前視圖和名稱空間信息。
# config.yml k9s: # Represents ui poll intervals. Default 2secs refreshRate: 2 # Set to true to hide K9s header. Default false headless: false # Indicates whether modification commands like delete/kill/edit are disabled. Default is false readOnly: false # Toggles icons display as not all terminal support these chars. noIcons: 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. 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: cooln: namespace: active: coolio 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 minikube: namespace: active: all favorites: - all - kube-system - default view: active: dp
對於平常常常須要操做 k8s 的運維人員來講倒是頗有幫助!
除了上述的功能之外,k9s 還有其餘比較實用的功能,好比支持自建命令別名、自定義熱鍵方式、支持自定義插件、還支持自定義終端界面顯示以及資源顯示順序和組合。這些在官網中,給出了一些簡單的示例說明和配置方式,基本能夠作到,看了以後就能夠自行 DIY 了。
瞭解你的工具並掌握作事情的「硬道理」很重要。還有一點很重要的是要記住,就管理而言,重要的是要更聰明地工做,而不是更努力。使用 k9s,就是我踐行這個目標的方法。
做者: Escape
連接: https://www.escapelife.site/p...