k8s常用取证命令

显示和查找资源

列出所有 namespace 中的所有 service

$ kubectl get services

列出所有 namespace 中的所有 pod

$ kubectl get pods --all-namespaces

列出所有 pod 并显示详细信息

$ kubectl get pods -o wide

列出指定 deployment

$ kubectl get deployment my-dep

列出该 namespace 中的所有 pod 包括未初始化的

$ kubectl get pods --include-uninitialized

使用详细输出来描述命令

$ kubectl describe nodes my-node
$ kubectl describe pods my-pod

List Services Sorted by Name

$ kubectl get services --sort-by=.metadata.name

根据重启次数排序列出 pod

$ kubectl get pods --sort-by='.status.containerStatuses[0].restartCount'

获取所有具有 app=cassandra 的 pod 中的 version 标签

$ kubectl get pods --selector=app=cassandra rc -o
jsonpath='{.items[*].metadata.labels.version}'

获取所有节点的 ExternalIP

$ kubectl get nodes -o jsonpath='{.items[*].status.addresses[?(@.type=="ExternalIP")].address}'

列出属于某个 PC 的 Pod 的名字

“jq”命令用于转换复杂的 jsonpath,参考 https://stedolan.github.io/jq/

$ sel=\({\)(kubectl get rc my-rc --output=json | jq -j '.spec.selector | to_entries | .[] | "(.key)=(.value),"')%?}
$ echo \((kubectl get pods --selector=\)sel --output=jsonpath={.items..metadata.name})

查看哪些节点已就绪

$ JSONPATH='{range .items[]}{@.metadata.name}:{range @.status.conditions[]}{@.type}={@.status};{end}{end}'
&& kubectl get nodes -o jsonpath="$JSONPATH" | grep "Ready=True"

列出当前 Pod 中使用的 Secret

$ kubectl get pods -o json | jq '.items[].spec.containers[].env[]?.valueFrom.secretKeyRef.name' | grep -v null | sort | uniq

posted @ 2024-09-25 19:09  SiSensor  阅读(37)  评论(0编辑  收藏  举报