常用的幾款工具讓 Kubernetes 集群上的工作更容易

tools

之前寫過(guò)一篇 介紹了工具加速云原生 Java 開(kāi)發(fā)

其實(shí)日常工作中在集群上的操作也非常多,今天就來(lái)介紹我所使用的工具。

kubectl-alias

使用頻率最高的工具,我自己稍微修改了一下,加入了 StatefulSet 的支持。

這個(gè)是我的 https://github.com/addozhang/kubectl-aliases,基于 https://github.com/ahmetb/kubectl-aliases。

比如輸出某個(gè) pod 的 json,kgpoojson xxx 等同于 kubectl get pod xxx -o json。

結(jié)合 jq 使用效果更好 ??。

語(yǔ)法解讀

  • 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 用于在不同的集群間進(jìn)行快速切換。假如用 kubectl,你需要:

# context 列表
kubectl config current-context 
# 設(shè)置 context
kubectl config use-context coffee
kubectx-demo

kubens 就是在不同 namespace 間快速切換的工具。用 kubectl 的話,需要:

# namespace 列表
kbuectl get ns
# kubectl config set-context --current --namespace=kube-system
kubens-demo

k9s

沒(méi)錯(cuò),只比 k8s 多了個(gè) 1 ??。

k9s 提供了終端 UI 與 Kubernetes 集群進(jìn)行編輯交互。本人常用的比如:

  • F 配置端口轉(zhuǎn)發(fā)
  • l 輸出 pod 日志
  • e 修改資源對(duì)象
  • s pod 終端交互模式
  • y yaml 方式輸出資源對(duì)象
  • d describe 資源對(duì)象
  • ctrl+d 刪除 pod

啟動(dòng)方式

# 指定 namespace 運(yùn)行
k9s -n mycoolns
# 指定 context 運(yùn)行
k9s --context coolCtx
# 只讀模式運(yùn)行
k9s --readonly
k9s-pod

鍵入問(wèn)號(hào)“?” 就可以打開(kāi)快捷操作指引。

help

stern

stern 可以用來(lái) tail 集群上的多個(gè) pod 和 pod 中多個(gè)容器的日志。不同的 pod 和容器以不同的顏色區(qū)分,方便 debug。

比如使用命令 stern -l tier=control-plane -n kube-system 可以輸出 kube-system 命名空間下控制平面(labeltier=control-plane) pod 的日志。

stern-control-plane

命令行選項(xiàng)

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 exit

Lens

Lens 是用來(lái)控制 Kubernetes 的 IDE,開(kāi)源且免費(fèi)。

消除了集群操作的復(fù)雜性、提供了實(shí)時(shí)的可觀察性、方便故障排查、支持多系統(tǒng)的桌面客戶端、兼容多種集群。

Lens

Infra App

Infra App 跟 Lens 差不多,UI 較 Lens 好些,但是功能就弱很多,類似 Lens 的只讀模式。

免費(fèi)版比收費(fèi)版的區(qū)別只在于支持的集群數(shù)量,免費(fèi)版只支持一個(gè)集群。

Infra

kubefwd

kubefwd,這個(gè)一直有安裝但是使用次數(shù)寥寥,因?yàn)閼?yīng)用之間的訪問(wèn)沒(méi)有走 service,不過(guò)偶爾做些實(shí)驗(yàn)的時(shí)候會(huì)用的上。

kubefwd 是一個(gè)用于端口轉(zhuǎn)發(fā)Kubernetes中指定namespace下的全部或者部分pod的命令行工具。 kubefwd 使用本地的環(huán)回IP地址轉(zhuǎn)發(fā)需要訪問(wèn)的service,并且使用與service相同的端口。 kubefwd 會(huì)臨時(shí)將service的域條目添加到 /etc/hosts 文件中。

啟動(dòng)kubefwd后,在本地就能像在Kubernetes集群中一樣使用service名字與端口訪問(wèn)對(duì)應(yīng)的應(yīng)用程序。

kubefwd_ani

總結(jié)

善用工具可以提升效率,但并不是不可或缺的。

如果你有其他的工具,歡迎留言提出。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容