K8S 資源可視化利器:Kubectl-Graph

{"type":"doc","content":[{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"前言","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"最近接手了一個規模比較大的集羣,光是整理集羣中的資源就使人頭昏眼花,雖然我自認 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"kubectl","attrs":{}}],"attrs":{}},{"type":"text","text":" 使用的已經十分熟練,但是上千個 kubernetes resource 看下來還是不堪重負。在不能爲集羣安裝任何其他工具的情況下,可以改造的就只有我自己的 client 端,也就是 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"kubectl","attrs":{}}],"attrs":{}},{"type":"text","text":" 了。本文就介紹一個有趣的 kubectl 插件:","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"kubectl-graph","attrs":{}}],"attrs":{}},{"type":"text","text":"。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"krew","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"要介紹 kubectl 的 plugin 機制,首先要介紹的就是 ","attrs":{}},{"type":"link","attrs":{"href":"https://krew.sigs.k8s.io/","title":""},"content":[{"type":"text","text":"krew","attrs":{}}]},{"type":"text","text":" 。 krew 是 ","attrs":{}},{"type":"link","attrs":{"href":"https://github.com/kubernetes/community/blob/master/sig-cli/README.md#cli-special-interest-group","title":""},"content":[{"type":"text","text":"kubernetes CLI SIG","attrs":{}}]},{"type":"text","text":" 項目,是用來管理 kubectl 插件的工具,作用類似於 yum 和 brew,可以用來搜索、安裝和管理 kubectl 插件。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"kubectl-graph","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"kubectl-graph 是一款可視化 kubernetes resource 及資源間關係的 kubectl 插件,可以將集羣中的資源以關係圖的方式進行展示。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"目前支持兩種展示方法:","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://graphviz.org/","title":""},"content":[{"type":"text","text":"Graphviz","attrs":{}}]}]}],"attrs":{}},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://neo4j.com/","title":""},"content":[{"type":"text","text":"Neo4j","attrs":{}}]}]}],"attrs":{}}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"前期準備","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"除了 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"kubectl","attrs":{}}],"attrs":{}},{"type":"text","text":",由於需要進行繪圖,所以還需安裝上面兩種展示方式的依賴。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"Graphviz","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"安裝 Graphviz 用來生成關係圖,需要使用 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"dot","attrs":{}}],"attrs":{}},{"type":"text","text":" CLI 工具,並將圖像輸出爲 SVG 格式:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"shell"},"content":[{"type":"text","text":"$ brew install graphviz","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"Neo4j","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Neo4j 是一個高性能的 NoSQL 圖形數據庫,它將結構化數據存儲在網絡上而不是表中,很適合用來展示 kubernetes resource 之間的關係,但 Neo4j 的依賴較多,需要一點時間來安裝。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"安裝 Java","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Neo4j 依賴 Java 環境,如果本機上沒有安裝 Java,請先前往 ","attrs":{}},{"type":"link","attrs":{"href":"http://www.java.com","title":""},"content":[{"type":"text","text":"http://www.java.com","attrs":{}}]},{"type":"text","text":" 下載並安裝。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"安裝 cypher-shell","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"因爲需要連接到 Neo4j 數據庫,所以要安裝 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"cypher-shell","attrs":{}}],"attrs":{}},{"type":"text","text":" CLI:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"shell"},"content":[{"type":"text","text":"$ brew install cypher-shell","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"安裝 Neo4j Desktop(可選)","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"接下來就是 Neo4j 本身的安裝,我這裏使用了 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Neo4j Desktop","attrs":{}}],"attrs":{}},{"type":"text","text":",使用和管理起來比較方便,也是使用 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"brew","attrs":{}}],"attrs":{}},{"type":"text","text":" 安裝:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"shell"},"content":[{"type":"text","text":"$ brew install --cask neo4j","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"安裝好後,運行 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Neo4j Desktop","attrs":{}}],"attrs":{}},{"type":"text","text":",完成設置即可","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/40/400f1b490460beece14d140454cbaf43.jpeg","alt":null,"title":"設置 neo4j","style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"boxShadow"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"使用 docker 運行 Neo4j(可選)","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"當然,如果你感覺安裝 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Neo4j Desktop","attrs":{}}],"attrs":{}},{"type":"text","text":" 比較麻煩,也可以使用 docker 運行 Neo4j:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"shell"},"content":[{"type":"text","text":"$ docker run --rm -p 7474:7474 -p 7687:7687 -e NEO4J_AUTH=none neo4j","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"只不過後續查看關係圖時,需要使用瀏覽器訪問 http://localhost:7474 來查看結果。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"安裝 kubectl-graph","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"插件的安裝方式比較簡單,如果你使用的是 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"kubectl 1.19","attrs":{}}],"attrs":{}},{"type":"text","text":" 之前的版本:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"shell"},"content":[{"type":"text","text":"$ kubectl-krew install graph","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"使用 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"kubectl 1.19","attrs":{}}],"attrs":{}},{"type":"text","text":" 之後的版本:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"shell"},"content":[{"type":"text","text":"$ kubectl krew install graph","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"使用方式","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"安裝完成後,就可以開始繪製 kubernetes resource 關係圖了。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"Graphviz","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"使用 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"kubectl graph","attrs":{}}],"attrs":{}},{"type":"text","text":" 命令獲取 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"kubec-system","attrs":{}}],"attrs":{}},{"type":"text","text":" 中正在運行的 pod,並通過管道傳遞給 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"dot","attrs":{}}],"attrs":{}},{"type":"text","text":":","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"shell"},"content":[{"type":"text","text":"$ kubectl graph pods --field-selector status.phase=Running -n kube-system | dot -T svg -o pods.svg","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"查看 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"pods.svg","attrs":{}}],"attrs":{}},{"type":"text","text":" ,資源果然很多:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/3e/3e4b752ca94c3c084d8b1abdeb0d3e75.jpeg","alt":null,"title":"pods.svg","style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"boxShadow"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"Neo4j","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Neo4j 可以展示更爲豐富且美觀的關係圖。在導入 kubernetes resource 之前,需要創建一個 Neo4j 數據庫:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/44/44c64002dfa52771033c177c7b638d06.jpeg","alt":null,"title":"創建 neo4j 數據庫","style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"boxShadow"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"數據庫創建好後,點擊 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Start","attrs":{}}],"attrs":{}},{"type":"text","text":" 運行並點擊 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Open","attrs":{}}],"attrs":{}},{"type":"text","text":" 打開 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Neo4j Browser","attrs":{}}],"attrs":{}},{"type":"text","text":":","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/04/043067072cef765798ebbeff5437e2ad.jpeg","alt":null,"title":"打開數據庫","style":[{"key":"width","value":"50%"},{"key":"bordertype","value":"boxShadow"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"執行命令將 kubernetes resource 導入 Neo4j:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"shell"},"content":[{"type":"text","text":"kubectl graph all -n kube-system -o cypher | cypher-shell -u neo4j -p ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這裏的 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"-u","attrs":{}}],"attrs":{}},{"type":"text","text":" 需要輸入 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"neo4j","attrs":{}}],"attrs":{}},{"type":"text","text":" 而不是你創建的數據庫名稱,","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Neo4j Browser","attrs":{}}],"attrs":{}},{"type":"text","text":" 上也有提示:","attrs":{}}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/66/66ae9d6ac81b0f1ec5cb687cf4a6047a.jpeg","alt":null,"title":"","style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"boxShadow"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"之後就可以在 Neo4j 上查看了,輸入查詢語句:","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"MATCH (n) RETURN n","attrs":{}}],"attrs":{}},{"type":"text","text":":","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/85/85664aadbf04e7881c3b3e5ac4fc8e7e.jpeg","alt":null,"title":"關係圖","style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"boxShadow"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這時一個美觀的 kubernetes resource 關係圖就出現了。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"結語","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"kubectl 還有很多好用且有趣的 plugin,後續筆者會介紹如何開發一個 kubectl plugin 並分享更多有趣的 plugin。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/53/53794fc50bd8decf129d72daee533574.gif","alt":null,"title":"","style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"boxShadow"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章