常用的几款工具让 Kubernetes 集群上的工作更容易
作者?|?Addo Zhang
來源 |?云原生指北
其實日常工作中在集群上的操作也非常多,今天就來介紹我所使用的工具。
kubectl-alias
使用頻率最高的工具,我自己稍微修改了一下,加入了?StatefulSet?的支持。
這個是我的 https://github.com/addozhang/kubectl-aliases,基于 https://github.com/ahmetb/kubectl-aliases。
比如輸出某個 pod 的 json,kgpoojson xxx?等同于?kubectl get pod xxx -o json。
結合 jq 使用效果更好 😂。
語法解讀
?k=kubectl?sys=--namespace kube-system?commands:?g=get?d=describe?rm=delete?a:apply -f?ak:apply -k?k:kustomize?ex:?exec -i -t?lo:?logs -f?resources:?po=pod,?dep=deployment,?ing=ingress,?svc=service,?cm=configmap,?sec=secret,ns=namespace,?no=node?flags:?output format:?oyaml,?ojson,?owide?all:?--all?or?--all-namespaces?depending on the command?sl:?--show-labels?w=-w/--watch?value flags (should be at the end):?n=-n/--namespace?f=-f/--filename?l=-l/--selector
kubectx + kubens
安裝看這里
kubectx?用于在不同的集群間進行快速切換。假如用?kubectl,你需要:
# context 列表 kubectl config current-context # 設置 context kubectl config use-context coffeekubectx-demokubens?就是在不同 namespace 間快速切換的工具。用?kubectl?的話,需要:
# namespace 列表 kbuectl get ns # kubectl config set-context --current --namespace=kube-systemkubens-demok9s
沒錯,只比 k8s 多了個 1 😂。
k9s 提供了終端 UI 與 Kubernetes 集群進行編輯交互。本人常用的比如:
?F?配置端口轉發?l?輸出 pod 日志?e?修改資源對象?s?pod 終端交互模式?y?yaml 方式輸出資源對象?d?describe 資源對象?ctrl+d?刪除 pod
啟動方式
# 指定 namespace 運行 k9s -n mycoolns # 指定 context 運行 k9s --context coolCtx # 只讀模式運行 k9s --readonlyk9s-pod鍵入問號“?” 就可以打開快捷操作指引。
helpstern
stern 可以用來?tail?集群上的多個 pod 和 pod 中多個容器的日志。不同的 pod 和容器以不同的顏色區分,方便 debug。
比如使用命令?stern -l tier=control-plane -n kube-system?可以輸出?kube-system?命名空間下控制平面(label?為?tier=control-plane) pod 的日志。
stern-control-plane命令行選項
Tail multiple pods and containers from Kubernetes Usage:stern pod-query [flags] Flags:-A, --all-namespaces If present, tail across all namespaces. A specific namespace is ignored even if specified with --namespace.--color string Color output. Can be 'always', 'never', or 'auto' (default "auto")--completion string Outputs stern command-line completion code for the specified shell. Can be 'bash' or 'zsh'-c, --container string Container name when multiple containers in pod (default ".*")--container-state string If present, tail containers with status in running, waiting or terminated. Default to running. (default "running")--context string Kubernetes context to use. Default to current context configured in kubeconfig.-e, --exclude strings Regex of log lines to exclude-E, --exclude-container string Exclude a Container name--field-selector string Selector (field query) to filter on. If present, default to ".*" for the pod-query.-h, --help help for stern-i, --include strings Regex of log lines to include--init-containers Include or exclude init containers (default true)--kubeconfig string Path to kubeconfig file to use-n, --namespace strings Kubernetes namespace to use. Default to namespace configured in Kubernetes context. To specify multiple namespaces, repeat this or set comma-separated value.-o, --output string Specify predefined template. Currently support: [default, raw, json] (default "default")-l, --selector string Selector (label query) to filter on. If present, default to ".*" for the pod-query.-s, --since duration Return logs newer than a relative duration like 5s, 2m, or 3h. Defaults to 48h.--tail int The number of lines from the end of the logs to show. Defaults to -1, showing all logs. (default -1)--template string Template to use for log lines, leave empty to use --output flag-t, --timestamps Print timestamps--timezone string Set timestamps to specific timezone (default "Local")-v, --version Print the version and exitLens
Lens 是用來控制 Kubernetes 的 IDE,開源且免費。
消除了集群操作的復雜性、提供了實時的可觀察性、方便故障排查、支持多系統的桌面客戶端、兼容多種集群。
LensInfra App
Infra App 跟 Lens 差不多,UI 較 Lens 好些,但是功能就弱很多,類似 Lens 的只讀模式。
免費版比收費版的區別只在于支持的集群數量,免費版只支持一個集群。
Infrakubefwd
kubefwd,這個一直有安裝但是使用次數寥寥,因為應用之間的訪問沒有走 service,不過偶爾做些實驗的時候會用的上。
kubefwd 是一個用于端口轉發Kubernetes中指定namespace下的全部或者部分pod的命令行工具。kubefwd 使用本地的環回IP地址轉發需要訪問的service,并且使用與service相同的端口。kubefwd 會臨時將service的域條目添加到 /etc/hosts 文件中。
啟動kubefwd后,在本地就能像在Kubernetes集群中一樣使用service名字與端口訪問對應的應用程序。
kubefwd_ani總結
善用工具可以提升效率,但并不是不可或缺的。
如果你有其他的工具,歡迎留言提出。
往期推薦
蘋果將于2025年推出的Apple Car長什么樣?
長跑11年,騰訊開源的變與不變
邊緣應用增長800%,聽聽Akamai邊緣部署的經驗
CSDN云原生Meet up深圳站與你不見不散!
點分享
點收藏
點點贊
點在看
總結
以上是生活随笔為你收集整理的常用的几款工具让 Kubernetes 集群上的工作更容易的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 苹果:iOS 比 Android 更安全
- 下一篇: 不用网关或代理的单点远程办公如何实现,A
