跳到主要内容

Node 相关

表格输出节点 IP 相关属性

kubectl get no -o=custom-columns=NAME:.metadata.name,INTERNAL-IP:.status.addresses[0].address,EXTERNAL-IP:.status.addresses[1].address,CIDR:.spec.podCIDR

表格输出各节点总可用资源 (Allocatable)

kubectl get no -o=custom-columns="NODE:.metadata.name,ALLOCATABLE CPU:.status.allocatable.cpu,ALLOCATABLE MEMORY:.status.allocatable.memory"

输出各节点已分配资源的情况

所有种类的资源已分配情况概览

kubectl get nodes --no-headers | awk '{print $1}' | xargs -I {} sh -c "echo {} ; kubectl describe node {} | grep Allocated -A 5 | grep -ve Event -ve Allocated -ve percent -ve --;"

表格输出 cpu 已分配情况

kubectl get nodes --no-headers | grep -v eklet- | awk '{print $1}' | xargs -I {} sh -c 'node="{}"; cpu=$(kubectl describe node "$node" | grep "Allocated resources:" -A5 | grep -ve Event -ve Allocated -ve percent -ve -- | grep cpu | awk '\''{print $2$3}'\''); printf "%s\t%s\n" "$node" "$cpu"'

表格输出 memory 已分配情况

kubectl get nodes --no-headers | grep -v eklet- | awk '{print $1}' | xargs -I {} sh -c 'node="{}"; memory=$(kubectl describe node "$node" | grep "Allocated resources:" -A6 | grep -ve Event -ve Allocated -ve percent -ve -- | grep memory | awk '\''{print $2$3}'\''); printf "%s\t%s\n" "$node" "$memory"'

表格输出节点可用区分布情况

kubectl get node -o custom-columns=NAME:.metadata.name,ZONE:".metadata.labels.topology\.kubernetes\.io/zone"

使用 kubectl debug 登录节点

kubectl debug node/10.10.12.77 --image=ubuntu:24.04

节点根目录会被挂到容器的 /host 目录下,可自定义容器镜像(比如指定有特殊调试工具的容器镜像),即使节点上没有 bash, sh 的命令也可以登录,更多参考 用 Kubectl 调试 Kubernetes 节点

使用 kubectl node-shell 登录节点

kubectl node-shell 10.10.12.77

登录进去后,根目录就是节点的根目录,可执行的命令完全依赖节点上的命令,如果节点上没有 bash, sh 之类的命令就无法登录。