如何从 kubectl get nodes 的输出中排除污染节点?

How to exclude taint nodes from outputs in kubectl get nodes?

我尝试检查有多少节点就绪(不包括被污染的节点NoSchedule ) 并将数字写入文本文件output.txt.

你能给我什么建议吗?

kubectl get nodes | grep Ready | grep -v NotReady | grep -v NoSchedule \
  | wc -l  > output.txt

这条命令可以为您完成这项工作:

Notes:

  • While grep includes lines, grep -v excludes lines
  • wc -l counts the number of lines.
  • number of output's lines is the same number of nodes with criteria you described

我相信 kubectl get nodes 不会显示污点,因此您不能只使用 grep 进行过滤。在这种情况下,您可以将输出设置为 json 并使用 jq (or yaml and use yq) 来处理它:

kubectl get nodes -o json | jq -c '.items[].spec.taints' | grep -v NoSchedule | wc -l > output.txt

-c jq 中的选项是在一行中输出每个元素,而不是漂亮地打印它,以防你有多个污点。其余的已经在

中解释过了

可以在没有jq或未安装jq的情况下使用以下命令。

kubectl get nodes --selector='!node-role.kubernetes.io/master' --no-headers | grep -v SchedulingDisabled | wc -l > output.txt

获取节点的完整证明查询,除了节点对其有污点影响 NoSchedule

kubectl get node -o=jsonpath='{range .items[*]}{.metadata.name}{"\t"}{.spec.taints[*].effect}{"\n"}{end}' | grep -v NoSchedule | wc -l